I have an onBlur on input text fields which auto-saves the record; The data structure is one document which contains an array of items. If i change an item's first column and then the second column quickly, the first column's save would cause an update which would reload both input fields.
How can i prevent the first save from reloading the entire row so both column values get saved?
Here's the code snippet (I mimiced a server save with a setTimeout
):
class Store {
static doc = {title: "test title", items: [{id: 1, one: "aa", two: "bbb"}, {id: 2, one: "yyy", two: "zzz"}]};
static getDocument() {
return this.doc;
}
static saveDocument(doc) {
this.doc = doc;
}
static updateItemInDocument(item, callback) {
var foundIndex;
let updatedEntry = this.doc.items.find( (s, index) => {
foundIndex = index;
return s.id === item.id;
});
this.doc.items[foundIndex] = item;
setTimeout(() => { console.log("updated"); callback(); }, 1000);
}
};
const Row = React.createClass({
getInitialState() {
return {item: this.props.item};
},
update() {
let document = Store.getDocument();
let updatedEntry = document.items.find( (s) => {
return s.id === this.props.item.id;
} );
this.setState({ item: updatedEntry});
},
handleEdit() {
this.setState({item: {
id: this.props.item.id,
one: this.refs.one.value,
two: this.refs.two.value
}
});
},
handleSave() {
Store.updateItemInDocument(this.state.item, this.update);
},
render() {
let item = this.state.item;
console.log(item);
return <tr> <p>Hello</p>
<input ref="one" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.one} />
<input ref="two" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.two} />
</tr>;
}
});
const App = React.createClass({
render() {
let rows = Store.getDocument().items.map( (item, i) => {
return <Row key={i} item={item} />;
});
return <table>
{rows}
</table>;
}
});
ReactDOM.render(
<App />,
document.getElementById("app")
);
I also have the code as a codepen:
I have an onBlur on input text fields which auto-saves the record; The data structure is one document which contains an array of items. If i change an item's first column and then the second column quickly, the first column's save would cause an update which would reload both input fields.
How can i prevent the first save from reloading the entire row so both column values get saved?
Here's the code snippet (I mimiced a server save with a setTimeout
):
class Store {
static doc = {title: "test title", items: [{id: 1, one: "aa", two: "bbb"}, {id: 2, one: "yyy", two: "zzz"}]};
static getDocument() {
return this.doc;
}
static saveDocument(doc) {
this.doc = doc;
}
static updateItemInDocument(item, callback) {
var foundIndex;
let updatedEntry = this.doc.items.find( (s, index) => {
foundIndex = index;
return s.id === item.id;
});
this.doc.items[foundIndex] = item;
setTimeout(() => { console.log("updated"); callback(); }, 1000);
}
};
const Row = React.createClass({
getInitialState() {
return {item: this.props.item};
},
update() {
let document = Store.getDocument();
let updatedEntry = document.items.find( (s) => {
return s.id === this.props.item.id;
} );
this.setState({ item: updatedEntry});
},
handleEdit() {
this.setState({item: {
id: this.props.item.id,
one: this.refs.one.value,
two: this.refs.two.value
}
});
},
handleSave() {
Store.updateItemInDocument(this.state.item, this.update);
},
render() {
let item = this.state.item;
console.log(item);
return <tr> <p>Hello</p>
<input ref="one" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.one} />
<input ref="two" type="text" onChange={this.handleEdit} onBlur={this.handleSave} value={item.two} />
</tr>;
}
});
const App = React.createClass({
render() {
let rows = Store.getDocument().items.map( (item, i) => {
return <Row key={i} item={item} />;
});
return <table>
{rows}
</table>;
}
});
ReactDOM.render(
<App />,
document.getElementById("app")
);
I also have the code as a codepen: http://codepen.io/tommychheng/pen/zBNxeW?editors=1010
Share Improve this question edited Jun 24, 2016 at 22:29 tommy chheng asked Jun 24, 2016 at 21:52 tommy chhengtommy chheng 9,22810 gold badges57 silver badges75 bronze badges 2-
item
always and only has the propertiesone
andtwo
? Or canitem
have more or less than those properties? – Ashitaka Commented Jun 24, 2016 at 22:42 - item has a fixed number of properties(just one and two) and input fields. – tommy chheng Commented Jun 26, 2016 at 23:57
1 Answer
Reset to default 3The issue stems from the fact that tabbing out of an input field fires both the onChange
and onBlur
handlers.
Maybe I'm misunderstanding your use case, but here's how I'd fix this issue:
const Row = React.createClass({
getInitialState() {
return { item: this.props.item };
},
handleSave() {
let item = {
id: this.state.item.id,
one: this.refs.one.value,
two: this.refs.two.value
}
Store.updateItemInDocument(item); // Is it really necessary to use the callback here?
},
render() {
let item = this.state.item;
return (
<tr>
<p>Hello</p>
<input ref="one" type="text" onBlur={this.handleSave} defaultValue={item.one} />
<input ref="two" type="text" onBlur={this.handleSave} defaultValue={item.two} />
</tr>
);
}
});
I switched from using the onChange
handler to using a defaultValue.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745428086a4627270.html
评论列表(0条)