import { ChangeEvent, Component } from 'react'; import { observer } from 'mobx-react'; import classnames from 'classnames'; import { debounce } from 'lodash'; import { mdiCloseCircleOutline, mdiMagnify } from '@mdi/js'; import { Icon } from './icon'; type Props = { value: string; placeholder: string; className: string; onChange: (e: ChangeEvent) => void; onReset: () => void; name: string; throttle: boolean; throttleDelay: number; autoFocus: boolean; }; // Should this file be converted into the coding style similar to './toggle/index.tsx'? class SearchInput extends Component { static defaultProps = { value: '', placeholder: '', className: '', name: 'searchInput', throttle: false, throttleDelay: 250, onChange: () => null, onReset: () => null, autoFocus: false, }; input = null; constructor(props: Props) { super(props); this.state = { value: props.value, }; this.throttledOnChange = debounce( this.throttledOnChange, this.props.throttleDelay, ); } componentDidMount() { const { autoFocus } = this.props; if (autoFocus) { // @ts-expect-error Object is possibly 'null'. this.input.focus(); } } onChange(e: ChangeEvent) { const { throttle, onChange } = this.props; const { value } = e.target; this.setState({ value }); if (throttle) { e.persist(); // @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'ChangeEvent'. this.throttledOnChange(value); } else { // @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'ChangeEvent'. onChange(value); } } throttledOnChange(e: ChangeEvent) { const { onChange } = this.props; onChange(e); } reset() { const { onReset } = this.props; this.setState({ value: '' }); onReset(); } render() { const { className, name, placeholder } = this.props; // @ts-expect-error Property 'value' does not exist on type 'Readonly<{}>'. const { value } = this.state; return (
{value.length > 0 && ( this.reset()}> )}
); } } export default observer(SearchInput);