TableForm.js 6.34 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 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's avatar
jim 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
    }
jim's avatar
jim committed
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
    });
    this.index += 1;
    this.setState({ data: newData });
jim's avatar
jim committed
56
  };
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
    setTimeout(() => {
      if (this.clickedCancel) {
        this.clickedCancel = false;
        return;
      }
afc163's avatar
afc163 committed
80
      const target = this.getRowByKey(key) || {};
afc163's avatar
afc163 committed
81 82
      if (!target.workId || !target.name || !target.department) {
        message.error('请填写完整成员信息。');
afc163's avatar
afc163 committed
83
        e.target.focus();
84 85 86
        this.setState({
          loading: false,
        });
afc163's avatar
afc163 committed
87 88
        return;
      }
afc163's avatar
afc163 committed
89
      delete target.isNew;
afc163's avatar
afc163 committed
90 91
      this.toggleEditable(e, key);
      this.props.onChange(this.state.data);
jim chen's avatar
jim chen committed
92 93 94 95
      this.setState({
        loading: false,
      });
    }, 500);
96 97
  }
  cancel(e, key) {
afc163's avatar
afc163 committed
98
    this.clickedCancel = true;
99
    e.preventDefault();
afc163's avatar
afc163 committed
100 101
    const newData = this.state.data.map(item => ({ ...item }));
    const target = this.getRowByKey(key, newData);
102 103 104 105 106
    if (this.cacheOriginData[key]) {
      Object.assign(target, this.cacheOriginData[key]);
      target.editable = false;
      delete this.cacheOriginData[key];
    }
afc163's avatar
afc163 committed
107
    this.setState({ data: newData });
108
    this.clickedCancel = false;
109 110
  }
  render() {
jim's avatar
jim committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    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;
        },
131
      },
jim's avatar
jim committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      {
        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;
        },
150
      },
jim's avatar
jim committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      {
        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;
        },
169
      },
jim's avatar
jim committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      {
        title: '操作',
        key: 'action',
        render: (text, record) => {
          if (!!record.editable && this.state.loading) {
            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>
              );
            }
189 190
            return (
              <span>
jim's avatar
jim committed
191
                <a onClick={e => this.saveRow(e, record.key)}>保存</a>
afc163's avatar
afc163 committed
192
                <Divider type="vertical" />
jim's avatar
jim committed
193
                <a onClick={e => this.cancel(e, record.key)}>取消</a>
194 195 196 197 198
              </span>
            );
          }
          return (
            <span>
jim's avatar
jim committed
199
              <a onClick={e => this.toggleEditable(e, record.key)}>编辑</a>
afc163's avatar
afc163 committed
200
              <Divider type="vertical" />
jim's avatar
jim committed
201 202 203
              <Popconfirm title="是否要删除此行?" onConfirm={() => this.remove(record.key)}>
                <a>删除</a>
              </Popconfirm>
204 205
            </span>
          );
jim's avatar
jim committed
206
        },
207
      },
jim's avatar
jim committed
208
    ];
209 210

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