summaryrefslogtreecommitdiff
path: root/src/lib/ServiceSearch.tsx
blob: b2509d3b5e030351806b0b87d2d51303d3db8dc7 (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
import React, { useContext } from 'react';
import _ from 'lodash';
import { InputBase } from '../components/Input';
import ServiceContext from './ServiceContext';
import useQuery from '../hooks/useQuery';

const ServiceSearch: React.FC = () => {
  const service = useContext(ServiceContext);
  const { query, setQuery } = useQuery();

  const placeholder = `Искать ${service.name.toLowerCase()}`;

  const handleChange = _.debounce((event: React.ChangeEvent<HTMLInputElement>) => {
    if (service.searchBy) {
      const newQuery = service.searchBy.reduce((acc, key) => {
        acc[`${key}[$regex]`] = event.target.value;
        return acc;
      }, query);

      setQuery(newQuery);
    }
  }, 250);

  return (
    <div className="mr-6 flex items-center">
      <InputBase placeholder={placeholder} onChange={handleChange} />
    </div>
  );
};

export default ServiceSearch;