|
| 1 | +import { Table, Input, InputNumber, Popconfirm, Form } from 'antd'; |
| 2 | + |
| 3 | +import "./editable-row.less" |
| 4 | + |
| 5 | +const data = []; |
| 6 | +for (let i = 0; i < 100; i++) { |
| 7 | + data.push({ |
| 8 | + key: i.toString(), |
| 9 | + name: `Edrward ${i}`, |
| 10 | + age: 32, |
| 11 | + address: `London Park no. ${i}`, |
| 12 | + }); |
| 13 | +} |
| 14 | +const EditableContext = React.createContext(); |
| 15 | + |
| 16 | +class EditableCell extends React.Component { |
| 17 | + getInput = () => { |
| 18 | + if (this.props.inputType === 'number') { |
| 19 | + return <InputNumber />; |
| 20 | + } |
| 21 | + return <Input />; |
| 22 | + }; |
| 23 | + |
| 24 | + renderCell = ({ getFieldDecorator }) => { |
| 25 | + const { |
| 26 | + editing, |
| 27 | + dataIndex, |
| 28 | + title, |
| 29 | + inputType, |
| 30 | + record, |
| 31 | + index, |
| 32 | + children, |
| 33 | + ...restProps |
| 34 | + } = this.props; |
| 35 | + return ( |
| 36 | + <td {...restProps}> |
| 37 | + {editing ? ( |
| 38 | + <Form.Item style={{ margin: 0 }}> |
| 39 | + {getFieldDecorator(dataIndex, { |
| 40 | + rules: [ |
| 41 | + { |
| 42 | + required: true, |
| 43 | + message: `Please Input ${title}!`, |
| 44 | + }, |
| 45 | + ], |
| 46 | + initialValue: record[dataIndex], |
| 47 | + })(this.getInput())} |
| 48 | + </Form.Item> |
| 49 | + ) : ( |
| 50 | + children |
| 51 | + )} |
| 52 | + </td> |
| 53 | + ); |
| 54 | + }; |
| 55 | + |
| 56 | + render() { |
| 57 | + return <EditableContext.Consumer>{this.renderCell}</EditableContext.Consumer>; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class EditableTable extends React.Component { |
| 62 | + constructor(props) { |
| 63 | + super(props); |
| 64 | + this.state = { data, editingKey: '' }; |
| 65 | + this.columns = [ |
| 66 | + { |
| 67 | + title: 'name', |
| 68 | + dataIndex: 'name', |
| 69 | + width: '25%', |
| 70 | + editable: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + title: 'age', |
| 74 | + dataIndex: 'age', |
| 75 | + width: '15%', |
| 76 | + editable: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + title: 'address', |
| 80 | + dataIndex: 'address', |
| 81 | + width: '40%', |
| 82 | + editable: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + title: 'operation', |
| 86 | + dataIndex: 'operation', |
| 87 | + render: (text, record) => { |
| 88 | + const { editingKey } = this.state; |
| 89 | + const editable = this.isEditing(record); |
| 90 | + return editable ? ( |
| 91 | + <span> |
| 92 | + <EditableContext.Consumer> |
| 93 | + {form => ( |
| 94 | + <a |
| 95 | + onClick={() => this.save(form, record.key)} |
| 96 | + style={{ marginRight: 8 }} |
| 97 | + > |
| 98 | + Save |
| 99 | + </a> |
| 100 | + )} |
| 101 | + </EditableContext.Consumer> |
| 102 | + <Popconfirm title="Sure to cancel?" onConfirm={() => this.cancel(record.key)}> |
| 103 | + <a>Cancel</a> |
| 104 | + </Popconfirm> |
| 105 | + </span> |
| 106 | + ) : ( |
| 107 | + <a disabled={editingKey !== ''} onClick={() => this.edit(record.key)}> |
| 108 | + Edit |
| 109 | + </a> |
| 110 | + ); |
| 111 | + }, |
| 112 | + }, |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
| 116 | + isEditing = record => record.key === this.state.editingKey; |
| 117 | + |
| 118 | + cancel = () => { |
| 119 | + this.setState({ editingKey: '' }); |
| 120 | + }; |
| 121 | + |
| 122 | + save(form, key) { |
| 123 | + form.validateFields((error, row) => { |
| 124 | + if (error) { |
| 125 | + return; |
| 126 | + } |
| 127 | + const newData = [...this.state.data]; |
| 128 | + const index = newData.findIndex(item => key === item.key); |
| 129 | + if (index > -1) { |
| 130 | + const item = newData[index]; |
| 131 | + newData.splice(index, 1, { |
| 132 | + ...item, |
| 133 | + ...row, |
| 134 | + }); |
| 135 | + this.setState({ data: newData, editingKey: '' }); |
| 136 | + } else { |
| 137 | + newData.push(row); |
| 138 | + this.setState({ data: newData, editingKey: '' }); |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + edit(key) { |
| 144 | + this.setState({ editingKey: key }); |
| 145 | + } |
| 146 | + |
| 147 | + render() { |
| 148 | + const components = { |
| 149 | + body: { |
| 150 | + cell: EditableCell, |
| 151 | + }, |
| 152 | + }; |
| 153 | + |
| 154 | + const columns = this.columns.map(col => { |
| 155 | + if (!col.editable) { |
| 156 | + return col; |
| 157 | + } |
| 158 | + return { |
| 159 | + ...col, |
| 160 | + onCell: record => ({ |
| 161 | + record, |
| 162 | + inputType: col.dataIndex === 'age' ? 'number' : 'text', |
| 163 | + dataIndex: col.dataIndex, |
| 164 | + title: col.title, |
| 165 | + editing: this.isEditing(record), |
| 166 | + }), |
| 167 | + }; |
| 168 | + }); |
| 169 | + |
| 170 | + return ( |
| 171 | + <EditableContext.Provider value={this.props.form}> |
| 172 | + <Table |
| 173 | + components={components} |
| 174 | + bordered |
| 175 | + dataSource={this.state.data} |
| 176 | + columns={columns} |
| 177 | + rowClassName="editable-row" |
| 178 | + pagination={{ |
| 179 | + onChange: this.cancel, |
| 180 | + }} |
| 181 | + /> |
| 182 | + </EditableContext.Provider> |
| 183 | + ); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +const EditableFormTable = Form.create()(EditableTable); |
| 188 | + |
| 189 | +ReactDOM.render(<EditableFormTable />, mountNode); |
| 190 | + |
0 commit comments