index.js 1.11 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1 2 3 4 5 6 7 8 9
import React, { PureComponent } from 'react';
import { Input, Icon } from 'antd';
import styles from './index.less';

export default class EditableItem extends PureComponent {
  state = {
    value: this.props.value,
    editable: false,
  };
陈帅's avatar
陈帅 committed
10

jim's avatar
jim committed
11
  handleChange = e => {
afc163's avatar
afc163 committed
12
    const { value } = e.target;
ddcat1115's avatar
ddcat1115 committed
13
    this.setState({ value });
jim's avatar
jim committed
14
  };
陈帅's avatar
陈帅 committed
15

ddcat1115's avatar
ddcat1115 committed
16 17 18 19 20
  check = () => {
    this.setState({ editable: false });
    if (this.props.onChange) {
      this.props.onChange(this.state.value);
    }
jim's avatar
jim committed
21
  };
陈帅's avatar
陈帅 committed
22

ddcat1115's avatar
ddcat1115 committed
23 24
  edit = () => {
    this.setState({ editable: true });
jim's avatar
jim committed
25
  };
陈帅's avatar
陈帅 committed
26

ddcat1115's avatar
ddcat1115 committed
27 28 29 30
  render() {
    const { value, editable } = this.state;
    return (
      <div className={styles.editableItem}>
jim's avatar
jim committed
31 32 33 34 35 36 37 38 39 40 41
        {editable ? (
          <div className={styles.wrapper}>
            <Input value={value} onChange={this.handleChange} onPressEnter={this.check} />
            <Icon type="check" className={styles.icon} onClick={this.check} />
          </div>
        ) : (
          <div className={styles.wrapper}>
            <span>{value || ' '}</span>
            <Icon type="edit" className={styles.icon} onClick={this.edit} />
          </div>
        )}
ddcat1115's avatar
ddcat1115 committed
42 43 44 45
      </div>
    );
  }
}