提交react-data-grid
由EditorContainer处理。提交逻辑很简单。编辑器在以下情况下提交值:
- 编辑器卸载
- 输入被按下
- 选项卡被按下
- 在某些情况下,当按下箭头时(将跳过这部分,您可能不需要,您可以在 EditorContainer 上查看此逻辑)
基于此,我建议进行自动保存的方式是:
创建一个 EditorWrapper (HOC) 您希望打开自动保存的编辑器
const editorWrapper(WrappedEditor) => {
return class EditorWrapper extends Component {
constructor(props) {
base(props);
this._changeCommitted = false;
this.handleKeyDown.bind(this);
}
handleKeyDown({ key, stopPropagation }) {
if (key === 'Tab' || key === 'Enter') {
stopPropagation();
this.save();
this.props.onCommit({ key });
this._changeCommitted = true;
}
// If you need the logic for the arrows too, check the editorContainer
}
save() {
// Save logic.
}
hasEscapeBeenPressed() {
let pressed = false;
let escapeKey = 27;
if (window.event) {
if (window.event.keyCode === escapeKey) {
pressed = true;
} else if (window.event.which === escapeKey) {
pressed = true;
}
}
return pressed;
}
componentWillUnmount() {
if (!this._changeCommitted && !this.hasEscapeBeenPressed()) {
this.save();
}
}
render() {
return (
<div onKeyDown={this.handleKeyDown}>
<WrappedComponent {...this.props} />
</div>);
}
}
}
导出编辑器时,只需用 EditorWrapper 包装它们
const Editor = ({ name }) => <div>{ name }</div>
export default EditorWrapper(Editor);