| 26 | } |
| 27 | |
| 28 | export default class CellInput extends PureComponent<ICellProps, ICellState> { |
| 29 | textInputRef: React.RefObject<any>; |
| 30 | constructor(props: ICellProps) { |
| 31 | super(props); |
| 32 | |
| 33 | this.state = { |
| 34 | value: props.value |
| 35 | }; |
| 36 | |
| 37 | this.textInputRef = createRef(); |
| 38 | } |
| 39 | |
| 40 | render() { |
| 41 | const {className, onMouseUp, onPaste, value} = this.props; |
| 42 | |
| 43 | // input does not handle `null` correct (causes console error) |
| 44 | const sanitizedValue = |
| 45 | this.state.value === null ? undefined : this.state.value; |
| 46 | |
| 47 | return ( |
| 48 | <div className='dash-input-cell-value-container dash-cell-value-container'> |
| 49 | <div className='input-cell-value-shadow cell-value-shadow'> |
| 50 | {value} |
| 51 | </div> |
| 52 | <input |
| 53 | ref={this.textInputRef} |
| 54 | type='text' |
| 55 | className={className} |
| 56 | onBlur={this.propagateChange} |
| 57 | onChange={this.handleChange} |
| 58 | onKeyDown={this.handleKeyDown} |
| 59 | onMouseUp={onMouseUp} |
| 60 | onPaste={onPaste} |
| 61 | value={sanitizedValue} |
| 62 | /> |
| 63 | </div> |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | propagateChange = () => { |
| 68 | if (this.state.value === this.props.value) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | const {onChange} = this.props; |
| 73 | |
| 74 | onChange(this.state.value); |
| 75 | }; |
| 76 | |
| 77 | handleChange = (e: any) => { |
| 78 | this.setState({value: e.target.value}); |
| 79 | }; |
| 80 | |
| 81 | handleKeyDown = (e: KeyboardEvent) => { |
| 82 | const is_focused = this.props.focused; |
| 83 | |
| 84 | if ( |
| 85 | is_focused && |