EditableCell.js 1.11 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import React from 'react';
import { Input, Form } from 'antd';

const FormItem = Form.Item;

export const EditableContext = React.createContext();

export class EditableCell extends React.Component {
  render() {
    const {
      editing,
      dataIndex,
      title,
      renderEditing,
      record,
      rules,
      index,
      ...restProps
    } = this.props;
    return (
      <EditableContext.Consumer>
        {form => {
          const { getFieldDecorator } = form;
          let formItem = null;
          if (editing) {
            if (renderEditing instanceof Function) {
              formItem = renderEditing({ form, dataIndex, record, rules });
            } else {
              formItem = (
                <FormItem style={{ margin: 0 }}>
                  {getFieldDecorator(dataIndex, {
                    rules,
                    initialValue: record[dataIndex],
                  })(<Input />)}
                </FormItem>
              );
            }
          }
          return <td {...restProps}>{editing ? formItem : restProps.children}</td>;
        }}
      </EditableContext.Consumer>
    );
  }
}