| 69 | } |
| 70 | |
| 71 | export class Input extends React.Component<IInputProps, InputState> { |
| 72 | static TextArea: typeof TextArea; |
| 73 | |
| 74 | static defaultProps = { |
| 75 | type: 'text', |
| 76 | }; |
| 77 | |
| 78 | input: HTMLInputElement | HTMLTextAreaElement | null = null; |
| 79 | |
| 80 | constructor(props: IInputProps) { |
| 81 | super(props); |
| 82 | const value = |
| 83 | typeof props.value === 'undefined' |
| 84 | ? props.defaultValue |
| 85 | : props.value; |
| 86 | |
| 87 | this.state = { |
| 88 | value, |
| 89 | prevValue: props.value, |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | static getDerivedStateFromProps( |
| 94 | nextProps: IInputProps, |
| 95 | { prevValue }: InputState |
| 96 | ) { |
| 97 | const newState: Partial<InputState> = { prevValue: nextProps.value }; |
| 98 | |
| 99 | if (nextProps.value !== undefined || prevValue !== nextProps.value) { |
| 100 | newState.value = nextProps.value; |
| 101 | } |
| 102 | return newState; |
| 103 | } |
| 104 | |
| 105 | saveInput = (input: HTMLInputElement) => { |
| 106 | this.input = input; |
| 107 | }; |
| 108 | |
| 109 | setValue(value: string) { |
| 110 | if (this.props.value === undefined) { |
| 111 | this.setState({ value }); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 116 | const { onChange } = this.props; |
| 117 | |
| 118 | this.setValue(e.target.value); |
| 119 | if (typeof onChange === 'function') |
| 120 | resolveOnChange(this.input, e, onChange); |
| 121 | }; |
| 122 | |
| 123 | handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 124 | const { onPressEnter, onKeyDown } = this.props; |
| 125 | |
| 126 | if (e.key === KeyCodes.ENTER) { |
| 127 | onPressEnter?.(e); |
| 128 | } |
nothing calls this directly
no test coverage detected