summaryrefslogtreecommitdiff
path: root/src/components/RuleSearch.js
blob: 80d42d7c0dc1907bdea3035ef2687940786ae668 (plain)
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
import React, { useState, useMemo } from 'react';
import Icon from 'c/Icon';

import search from 's/search.svg';
import { useActions, useStoreState } from 'm/store';

import debounce from 'lodash-es/debounce';

import s0 from './RuleSearch.module.scss';

import { getSearchText, updateSearchText } from 'd/rules';

const mapStateToProps = s => ({
  searchText: getSearchText(s)
});

const actions = { updateSearchText };

export default React.memo(function RuleSearch() {
  const { updateSearchText } = useActions(actions);
  const updateSearchTextDebounced = useMemo(
    () => debounce(updateSearchText, 300),
    [updateSearchText]
  );
  const { searchText } = useStoreState(mapStateToProps);
  const [text, setText] = useState(searchText);
  const onChange = e => {
    setText(e.target.value);
    updateSearchTextDebounced(e.target.value);
  };

  return (
    <div className={s0.RuleSearch}>
      <div className={s0.RuleSearchContainer}>
        <div className={s0.inputWrapper}>
          <input
            type="text"
            value={text}
            onChange={onChange}
            className={s0.input}
          />
        </div>
        <div className={s0.iconWrapper}>
          <Icon id={search.id} />
        </div>
      </div>
    </div>
  );
});