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

export default class EditableItem extends PureComponent {
6 7 8 9 10 11 12
  constructor(props) {
    super(props);
    this.state = {
      value: props.value,
      editable: false,
    };
  }
陈帅's avatar
陈帅 committed
13

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

ddcat1115's avatar
ddcat1115 committed
19 20
  check = () => {
    this.setState({ editable: false });
21 22 23 24
    const { onChange } = this.props;
    const { value } = this.state;
    if (onChange) {
      onChange(value);
ddcat1115's avatar
ddcat1115 committed
25
    }
jim's avatar
jim committed
26
  };
陈帅's avatar
陈帅 committed
27

ddcat1115's avatar
ddcat1115 committed
28 29
  edit = () => {
    this.setState({ editable: true });
jim's avatar
jim committed
30
  };
陈帅's avatar
陈帅 committed
31

ddcat1115's avatar
ddcat1115 committed
32 33 34 35
  render() {
    const { value, editable } = this.state;
    return (
      <div className={styles.editableItem}>
jim's avatar
jim committed
36 37 38 39 40 41 42 43 44 45 46
        {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
47 48 49 50
      </div>
    );
  }
}