| 29 | type PropsWithDefaults = IProps & IDefaultProps; |
| 30 | |
| 31 | export default class IsolatedInput extends PureComponent<IProps, IState> { |
| 32 | public static readonly defaultProps = { |
| 33 | stopPropagation: false, |
| 34 | updateOnEnter: true, |
| 35 | updateOnBlur: true, |
| 36 | updateOnSubmit: true |
| 37 | }; |
| 38 | |
| 39 | private get propsWithDefaults() { |
| 40 | return this.props as PropsWithDefaults; |
| 41 | } |
| 42 | |
| 43 | UNSAFE_componentWillReceiveProps(nextProps: IProps) { |
| 44 | const {value} = this.props; |
| 45 | const {value: nextValue} = nextProps; |
| 46 | |
| 47 | if (value !== nextValue) { |
| 48 | this.setState({ |
| 49 | value: nextValue |
| 50 | }); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | constructor(props: PropsWithDefaults) { |
| 55 | super(props); |
| 56 | |
| 57 | this.state = { |
| 58 | value: props.value |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | handleKeyDown = (e: React.KeyboardEvent) => { |
| 63 | const {stopPropagation, updateOnEnter} = this.propsWithDefaults; |
| 64 | |
| 65 | if (stopPropagation) { |
| 66 | e.stopPropagation(); |
| 67 | } |
| 68 | |
| 69 | if (updateOnEnter && e.keyCode === KEY_CODES.ENTER) { |
| 70 | this.submit(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | handleChange = (ev: any) => { |
| 75 | this.setState({ |
| 76 | value: ev.target.value |
| 77 | }); |
| 78 | }; |
| 79 | |
| 80 | submit = () => |
| 81 | this.state.value !== this.props.value && |
| 82 | this.props.submit(this.state.value); |
| 83 | |
| 84 | render() { |
| 85 | const {onCopy, onPaste, placeholder, updateOnBlur, updateOnSubmit} = |
| 86 | this.propsWithDefaults; |
| 87 | |
| 88 | const props = { |
nothing calls this directly
no test coverage detected
searching dependent graphs…