index.js 862 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. export default class Input extends Component {
  4. constructor(props) {
  5. super(props);
  6. this.defaultValue = props.value;
  7. this.state = { value: props.value };
  8. }
  9. componentWillReceiveProps(nextProps) {
  10. if (this.defaultValue !== nextProps.value) {
  11. this.defaultValue = nextProps.value;
  12. this.setState({ value: nextProps.value });
  13. }
  14. }
  15. onChange(value) {
  16. this.setState({ value });
  17. }
  18. onSave(value) {
  19. const { onChange } = this.props;
  20. if (onChange) onChange(value);
  21. }
  22. render() {
  23. return (
  24. <input
  25. readOnly={this.props.readOnly}
  26. value={this.state.value}
  27. onChange={e => this.onChange(e.target.value)}
  28. onBlur={e => this.onSave(e.target.value)}
  29. onFocus={e => this.props.onFocus(e)}
  30. />
  31. );
  32. }
  33. }