antd3以上的写法乍一看还挺复杂,自己写了个精简版
没用EditableRow+Cell的结构,也不使用Context、高阶组件等,不使用form验证
最终效果:
class EditableCell extends React.Component {
state = {
editing: false
};
toggleEdit = () => {
const editing = !this.state.editing
this.setState({ editing }, () => {
if (editing) {
this.input.focus()
}
})
};
save = e => {
const { record, handleSave } = this.props;
this.toggleEdit();
handleSave(record, e.target.value)
}; // save主要处理两件事,一是切换editing状态,二是提交更新的数据
render() {
const { children } = this.props
const { editing } = this.state;
return editing ? (
<Input defaultValue={children} ref={node=>(this.input=node)} onPressEnter={this.save} onBlur={this.save} />
) : (
<span>{children}<Icon type="edit" theme='twoTone' style={
{marginLeft: 10}} onCl