aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui/SearchInput.tsx
blob: d3146301062a2bfea6509702c0013a5fcdda2c87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { mdiCloseCircleOutline, mdiMagnify } from '@mdi/js';
import classnames from 'classnames';
import { debounce, noop } from 'lodash';
import { observer } from 'mobx-react';
import { type ChangeEvent, Component, type ReactElement } from 'react';
import Icon from './icon';

interface IProps {
  value?: string;
  placeholder: string;
  className?: string;
  onChange?: (e: string) => void;
  onReset: () => void;
  name?: string;
  throttle?: boolean;
  throttleDelay?: number;
  autoFocus?: boolean;
}

interface IState {
  value: string;
}

@observer
class SearchInput extends Component<IProps, IState> {
  input: HTMLInputElement | null = null;

  constructor(props: IProps) {
    super(props);

    this.state = {
      value: props.value || '',
    };

    this.throttledOnChange = debounce(
      this.throttledOnChange,
      this.props.throttleDelay,
    );
  }

  componentDidMount(): void {
    const { autoFocus = false } = this.props;

    if (autoFocus && this.input) {
      this.input.focus();
    }
  }

  onChange(e: ChangeEvent<HTMLInputElement>): void {
    const { throttle = false, onChange = noop } = this.props;
    const { value } = e.target;
    this.setState({ value });

    if (throttle) {
      e.persist();
      this.throttledOnChange(value);
    } else {
      onChange(value);
    }
  }

  throttledOnChange(e: string): void {
    const { onChange = noop } = this.props;

    onChange(e);
  }

  reset(): void {
    const { onReset = noop } = this.props;
    this.setState({ value: '' });

    onReset();
  }

  render(): ReactElement {
    const {
      className = '',
      name = 'searchInput',
      placeholder = '',
    } = this.props;
    const { value = '' } = this.state;

    return (
      <div className={classnames([className, 'search-input'])}>
        {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
        <label htmlFor={name}>
          <Icon icon={mdiMagnify} />
          <input
            name={name}
            id={name}
            type="text"
            placeholder={placeholder}
            value={value}
            onChange={e => this.onChange(e)}
            ref={ref => {
              this.input = ref;
            }}
          />
        </label>
        {value.length > 0 && (
          // eslint-disable-next-line jsx-a11y/no-static-element-interactions
          <span onClick={() => this.reset()} onKeyDown={noop}>
            <Icon icon={mdiCloseCircleOutline} />
          </span>
        )}
      </div>
    );
  }
}

export default SearchInput;