summaryrefslogtreecommitdiff
path: root/src/lib/ServiceSearch.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-04-17 19:56:30 +0300
committereug-vs <eug-vs@keemail.me>2021-04-17 19:56:30 +0300
commitc222bc06f9c123d3e2fa4a428d74032f54887485 (patch)
treee7c809251516442a773b7886020bec9914437662 /src/lib/ServiceSearch.tsx
parentf3d3471e697d6ebede4fa584c8671fb686028381 (diff)
downloadcommercel-ui-c222bc06f9c123d3e2fa4a428d74032f54887485.tar.gz
feat: add ServiceSearch
Diffstat (limited to 'src/lib/ServiceSearch.tsx')
-rw-r--r--src/lib/ServiceSearch.tsx31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/lib/ServiceSearch.tsx b/src/lib/ServiceSearch.tsx
new file mode 100644
index 0000000..b2509d3
--- /dev/null
+++ b/src/lib/ServiceSearch.tsx
@@ -0,0 +1,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;