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,
  };
jim's avatar
jim committed
10
  handleChange = e => {
afc163's avatar
afc163 committed
11
    const { value } = e.target;
ddcat1115's avatar
ddcat1115 committed
12
    this.setState({ value });
jim's avatar
jim committed
13
  };
ddcat1115's avatar
ddcat1115 committed
14 15 16 17 18
  check = () => {
    this.setState({ editable: false });
    if (this.props.onChange) {
      this.props.onChange(this.state.value);
    }
jim's avatar
jim committed
19
  };
ddcat1115's avatar
ddcat1115 committed
20 21
  edit = () => {
    this.setState({ editable: true });
jim's avatar
jim committed
22
  };
ddcat1115's avatar
ddcat1115 committed
23 24 25 26
  render() {
    const { value, editable } = this.state;
    return (
      <div className={styles.editableItem}>
jim's avatar
jim committed
27 28 29 30 31 32 33 34 35 36 37
        {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
38 39 40 41
      </div>
    );
  }
}