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 {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
6 7 8 9 10 11 12 13
  constructor(props) {
    super(props);
    this.state = {
      value: props.value,
      editable: false,
    };
  }

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 });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21 22 23 24
    const { value } = this.state;
    const { onChange } = 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>
    );
  }
}