TableForm.js 6.32 KB
Newer Older
1
import React, { PureComponent } from 'react';
afc163's avatar
afc163 committed
2
import { Table, Button, Input, message, Popconfirm, Divider } from 'antd';
3 4 5 6 7 8 9 10
import styles from './style.less';

export default class TableForm extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      data: props.value,
jim chen's avatar
jim chen committed
11
      loading: false,
12 13 14 15 16 17 18 19 20
    };
  }
  componentWillReceiveProps(nextProps) {
    if ('value' in nextProps) {
      this.setState({
        data: nextProps.value,
      });
    }
  }
afc163's avatar
afc163 committed
21 22
  getRowByKey(key, newData) {
    return (newData || this.state.data).filter(item => item.key === key)[0];
23 24 25
  }
  index = 0;
  cacheOriginData = {};
jim chen's avatar
jim chen committed
26
  toggleEditable=(e, key) => {
27
    e.preventDefault();
afc163's avatar
afc163 committed
28 29
    const newData = this.state.data.map(item => ({ ...item }));
    const target = this.getRowByKey(key, newData);
30 31 32 33 34 35
    if (target) {
      // 进入编辑状态时保存原始数据
      if (!target.editable) {
        this.cacheOriginData[key] = { ...target };
      }
      target.editable = !target.editable;
afc163's avatar
afc163 committed
36
      this.setState({ data: newData });
37 38
    }
  }
afc163's avatar
afc163 committed
39
  remove(key) {
40 41 42 43 44
    const newData = this.state.data.filter(item => item.key !== key);
    this.setState({ data: newData });
    this.props.onChange(newData);
  }
  newMember = () => {
afc163's avatar
afc163 committed
45
    const newData = this.state.data.map(item => ({ ...item }));
46 47 48 49 50 51
    newData.push({
      key: `NEW_TEMP_ID_${this.index}`,
      workId: '',
      name: '',
      department: '',
      editable: true,
afc163's avatar
afc163 committed
52
      isNew: true,
53 54 55 56
    });
    this.index += 1;
    this.setState({ data: newData });
  }
afc163's avatar
afc163 committed
57 58 59 60 61
  handleKeyPress(e, key) {
    if (e.key === 'Enter') {
      this.saveRow(e, key);
    }
  }
62
  handleFieldChange(e, fieldName, key) {
afc163's avatar
afc163 committed
63 64
    const newData = this.state.data.map(item => ({ ...item }));
    const target = this.getRowByKey(key, newData);
65 66 67 68 69 70
    if (target) {
      target[fieldName] = e.target.value;
      this.setState({ data: newData });
    }
  }
  saveRow(e, key) {
afc163's avatar
afc163 committed
71
    e.persist();
jim chen's avatar
jim chen committed
72 73 74
    this.setState({
      loading: true,
    });
afc163's avatar
afc163 committed
75 76 77 78 79 80 81 82 83 84
    // save field when blur input
    setTimeout(() => {
      if (document.activeElement.tagName === 'INPUT' &&
          document.activeElement !== e.target) {
        return;
      }
      if (this.clickedCancel) {
        this.clickedCancel = false;
        return;
      }
afc163's avatar
afc163 committed
85
      const target = this.getRowByKey(key) || {};
afc163's avatar
afc163 committed
86 87
      if (!target.workId || !target.name || !target.department) {
        message.error('请填写完整成员信息。');
afc163's avatar
afc163 committed
88
        e.target.focus();
89 90 91
        this.setState({
          loading: false,
        });
afc163's avatar
afc163 committed
92 93
        return;
      }
afc163's avatar
afc163 committed
94
      delete target.isNew;
afc163's avatar
afc163 committed
95 96
      this.toggleEditable(e, key);
      this.props.onChange(this.state.data);
jim chen's avatar
jim chen committed
97 98 99 100
      this.setState({
        loading: false,
      });
    }, 500);
101 102
  }
  cancel(e, key) {
afc163's avatar
afc163 committed
103
    this.clickedCancel = true;
104
    e.preventDefault();
afc163's avatar
afc163 committed
105 106
    const newData = this.state.data.map(item => ({ ...item }));
    const target = this.getRowByKey(key, newData);
107 108 109 110 111
    if (this.cacheOriginData[key]) {
      Object.assign(target, this.cacheOriginData[key]);
      target.editable = false;
      delete this.cacheOriginData[key];
    }
afc163's avatar
afc163 committed
112
    this.setState({ data: newData });
113 114 115 116 117 118 119 120 121 122 123 124 125 126
  }
  render() {
    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)}
afc163's avatar
afc163 committed
127 128
              onBlur={e => this.saveRow(e, record.key)}
              onKeyPress={e => this.handleKeyPress(e, record.key)}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
              placeholder="成员姓名"
            />
          );
        }
        return text;
      },
    }, {
      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)}
afc163's avatar
afc163 committed
146 147
              onBlur={e => this.saveRow(e, record.key)}
              onKeyPress={e => this.handleKeyPress(e, record.key)}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
              placeholder="工号"
            />
          );
        }
        return text;
      },
    }, {
      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)}
afc163's avatar
afc163 committed
165 166
              onBlur={e => this.saveRow(e, record.key)}
              onKeyPress={e => this.handleKeyPress(e, record.key)}
167 168 169 170 171 172 173 174 175 176
              placeholder="所属部门"
            />
          );
        }
        return text;
      },
    }, {
      title: '操作',
      key: 'action',
      render: (text, record) => {
jim chen's avatar
jim chen committed
177 178 179
        if (!!record.editable && this.state.loading) {
          return null;
        }
180
        if (record.editable) {
afc163's avatar
afc163 committed
181
          if (record.isNew) {
182 183
            return (
              <span>
afc163's avatar
afc163 committed
184
                <a>保存</a>
afc163's avatar
afc163 committed
185
                <Divider type="vertical" />
afc163's avatar
afc163 committed
186 187 188
                <Popconfirm title="是否要删除此行?" onConfirm={() => this.remove(record.key)}>
                  <a>删除</a>
                </Popconfirm>
189 190 191 192 193
              </span>
            );
          }
          return (
            <span>
afc163's avatar
afc163 committed
194
              <a>保存</a>
afc163's avatar
afc163 committed
195
              <Divider type="vertical" />
196 197 198 199 200 201 202
              <a onClick={e => this.cancel(e, record.key)}>取消</a>
            </span>
          );
        }
        return (
          <span>
            <a onClick={e => this.toggleEditable(e, record.key)}>编辑</a>
afc163's avatar
afc163 committed
203
            <Divider type="vertical" />
afc163's avatar
afc163 committed
204 205 206
            <Popconfirm title="是否要删除此行?" onConfirm={() => this.remove(record.key)}>
              <a>删除</a>
            </Popconfirm>
207 208 209 210 211 212 213 214
          </span>
        );
      },
    }];

    return (
      <div>
        <Table
jim chen's avatar
jim chen committed
215
          loading={this.state.loading}
216 217 218 219 220 221 222 223
          columns={columns}
          dataSource={this.state.data}
          pagination={false}
          rowClassName={(record) => {
            return record.editable ? styles.editable : '';
          }}
        />
        <Button
224
          style={{ width: '100%', marginTop: 16, marginBottom: 8 }}
225 226 227 228 229 230 231 232 233 234
          type="dashed"
          onClick={this.newMember}
          icon="plus"
        >
          新增成员
        </Button>
      </div>
    );
  }
}