TableForm.js 6.61 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { PureComponent, Fragment } from 'react';
afc163's avatar
afc163 committed
2
import { Table, Button, Input, message, Popconfirm, Divider } from 'antd';
3 4 5
import styles from './style.less';

export default class TableForm extends PureComponent {
6 7 8 9
  index = 0;

  cacheOriginData = {};

10 11 12 13 14
  constructor(props) {
    super(props);

    this.state = {
      data: props.value,
jim chen's avatar
jim chen committed
15
      loading: false,
16 17
    };
  }
陈帅's avatar
陈帅 committed
18

19 20 21 22 23 24 25
  componentWillReceiveProps(nextProps) {
    if ('value' in nextProps) {
      this.setState({
        data: nextProps.value,
      });
    }
  }
陈帅's avatar
陈帅 committed
26

afc163's avatar
afc163 committed
27
  getRowByKey(key, newData) {
28 29
    const { data } = this.state;
    return (newData || data).filter(item => item.key === key)[0];
30
  }
陈帅's avatar
陈帅 committed
31

jim's avatar
jim committed
32
  toggleEditable = (e, key) => {
33
    e.preventDefault();
34 35
    const { data } = this.state;
    const newData = data.map(item => ({ ...item }));
afc163's avatar
afc163 committed
36
    const target = this.getRowByKey(key, newData);
37 38 39 40 41 42
    if (target) {
      // 进入编辑状态时保存原始数据
      if (!target.editable) {
        this.cacheOriginData[key] = { ...target };
      }
      target.editable = !target.editable;
afc163's avatar
afc163 committed
43
      this.setState({ data: newData });
44
    }
jim's avatar
jim committed
45
  };
陈帅's avatar
陈帅 committed
46

47
  newMember = () => {
48 49
    const { data } = this.state;
    const newData = data.map(item => ({ ...item }));
50 51 52 53 54 55
    newData.push({
      key: `NEW_TEMP_ID_${this.index}`,
      workId: '',
      name: '',
      department: '',
      editable: true,
afc163's avatar
afc163 committed
56
      isNew: true,
57 58 59
    });
    this.index += 1;
    this.setState({ data: newData });
jim's avatar
jim committed
60
  };
陈帅's avatar
陈帅 committed
61

62 63 64 65 66 67 68 69
  remove(key) {
    const { data } = this.state;
    const { onChange } = this.props;
    const newData = data.filter(item => item.key !== key);
    this.setState({ data: newData });
    onChange(newData);
  }

afc163's avatar
afc163 committed
70 71 72 73 74
  handleKeyPress(e, key) {
    if (e.key === 'Enter') {
      this.saveRow(e, key);
    }
  }
陈帅's avatar
陈帅 committed
75

76
  handleFieldChange(e, fieldName, key) {
77 78
    const { data } = this.state;
    const newData = data.map(item => ({ ...item }));
afc163's avatar
afc163 committed
79
    const target = this.getRowByKey(key, newData);
80 81 82 83 84
    if (target) {
      target[fieldName] = e.target.value;
      this.setState({ data: newData });
    }
  }
陈帅's avatar
陈帅 committed
85

86
  saveRow(e, key) {
afc163's avatar
afc163 committed
87
    e.persist();
jim chen's avatar
jim chen committed
88 89 90
    this.setState({
      loading: true,
    });
afc163's avatar
afc163 committed
91 92 93 94 95
    setTimeout(() => {
      if (this.clickedCancel) {
        this.clickedCancel = false;
        return;
      }
afc163's avatar
afc163 committed
96
      const target = this.getRowByKey(key) || {};
afc163's avatar
afc163 committed
97 98
      if (!target.workId || !target.name || !target.department) {
        message.error('请填写完整成员信息。');
afc163's avatar
afc163 committed
99
        e.target.focus();
100 101 102
        this.setState({
          loading: false,
        });
afc163's avatar
afc163 committed
103 104
        return;
      }
105 106
      const { data } = this.state;
      const { onChange } = this.props;
afc163's avatar
afc163 committed
107
      delete target.isNew;
afc163's avatar
afc163 committed
108
      this.toggleEditable(e, key);
109
      onChange(data);
jim chen's avatar
jim chen committed
110 111 112 113
      this.setState({
        loading: false,
      });
    }, 500);
114
  }
陈帅's avatar
陈帅 committed
115

116
  cancel(e, key) {
afc163's avatar
afc163 committed
117
    this.clickedCancel = true;
118
    e.preventDefault();
119 120
    const { data } = this.state;
    const newData = data.map(item => ({ ...item }));
afc163's avatar
afc163 committed
121
    const target = this.getRowByKey(key, newData);
122 123 124 125 126
    if (this.cacheOriginData[key]) {
      Object.assign(target, this.cacheOriginData[key]);
      target.editable = false;
      delete this.cacheOriginData[key];
    }
afc163's avatar
afc163 committed
127
    this.setState({ data: newData });
128
    this.clickedCancel = false;
129
  }
陈帅's avatar
陈帅 committed
130

131
  render() {
jim's avatar
jim committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    const columns = [
      {
        title: '成员姓名',
        dataIndex: 'name',
        key: 'name',
        width: '20%',
        render: (text, record) => {
          if (record.editable) {
            return (
              <Input
                value={text}
                autoFocus
                onChange={e => this.handleFieldChange(e, 'name', record.key)}
                onKeyPress={e => this.handleKeyPress(e, record.key)}
                placeholder="成员姓名"
              />
            );
          }
          return text;
        },
152
      },
jim's avatar
jim committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      {
        title: '工号',
        dataIndex: 'workId',
        key: 'workId',
        width: '20%',
        render: (text, record) => {
          if (record.editable) {
            return (
              <Input
                value={text}
                onChange={e => this.handleFieldChange(e, 'workId', record.key)}
                onKeyPress={e => this.handleKeyPress(e, record.key)}
                placeholder="工号"
              />
            );
          }
          return text;
        },
171
      },
jim's avatar
jim committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      {
        title: '所属部门',
        dataIndex: 'department',
        key: 'department',
        width: '40%',
        render: (text, record) => {
          if (record.editable) {
            return (
              <Input
                value={text}
                onChange={e => this.handleFieldChange(e, 'department', record.key)}
                onKeyPress={e => this.handleKeyPress(e, record.key)}
                placeholder="所属部门"
              />
            );
          }
          return text;
        },
190
      },
jim's avatar
jim committed
191 192 193 194
      {
        title: '操作',
        key: 'action',
        render: (text, record) => {
195 196
          const { loading } = this.state;
          if (!!record.editable && loading) {
jim's avatar
jim committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210
            return null;
          }
          if (record.editable) {
            if (record.isNew) {
              return (
                <span>
                  <a onClick={e => this.saveRow(e, record.key)}>添加</a>
                  <Divider type="vertical" />
                  <Popconfirm title="是否要删除此行?" onConfirm={() => this.remove(record.key)}>
                    <a>删除</a>
                  </Popconfirm>
                </span>
              );
            }
211 212
            return (
              <span>
jim's avatar
jim committed
213
                <a onClick={e => this.saveRow(e, record.key)}>保存</a>
afc163's avatar
afc163 committed
214
                <Divider type="vertical" />
jim's avatar
jim committed
215
                <a onClick={e => this.cancel(e, record.key)}>取消</a>
216 217 218 219 220
              </span>
            );
          }
          return (
            <span>
jim's avatar
jim committed
221
              <a onClick={e => this.toggleEditable(e, record.key)}>编辑</a>
afc163's avatar
afc163 committed
222
              <Divider type="vertical" />
jim's avatar
jim committed
223 224 225
              <Popconfirm title="是否要删除此行?" onConfirm={() => this.remove(record.key)}>
                <a>删除</a>
              </Popconfirm>
226 227
            </span>
          );
jim's avatar
jim committed
228
        },
229
      },
jim's avatar
jim committed
230
    ];
231

232 233
    const { loading, data } = this.state;

234
    return (
陈帅's avatar
陈帅 committed
235
      <Fragment>
236
        <Table
237
          loading={loading}
238
          columns={columns}
239
          dataSource={data}
240
          pagination={false}
jim's avatar
jim committed
241
          rowClassName={record => {
242 243 244 245
            return record.editable ? styles.editable : '';
          }}
        />
        <Button
246
          style={{ width: '100%', marginTop: 16, marginBottom: 8 }}
247 248 249 250 251 252
          type="dashed"
          onClick={this.newMember}
          icon="plus"
        >
          新增成员
        </Button>
陈帅's avatar
陈帅 committed
253
      </Fragment>
254 255 256
    );
  }
}