|
1 | | -import { Field, Form, Formik } from 'formik' |
| 1 | +import { useEffect, useRef } from 'react' |
| 2 | +import { Field, Form, Formik, useFormikContext } from 'formik' |
2 | 3 | import Button from '../button' |
3 | 4 |
|
4 | | -const SearchInput = ({ onSearch, placeholder, 'data-cy': dataCy }) => ( |
5 | | - <Formik |
6 | | - initialValues={{ search: '' }} |
7 | | - onSubmit={({ search }) => onSearch(search)} |
8 | | - > |
9 | | - <Form className='form-control'> |
10 | | - <Field |
11 | | - data-cy={`${dataCy}-search-input`} |
12 | | - type='text' |
13 | | - name='search' |
14 | | - id='search' |
15 | | - placeholder={placeholder} |
16 | | - /> |
17 | | - <Button |
18 | | - data-cy={`${dataCy}-search-submit`} |
19 | | - type='submit' |
20 | | - variant='submit' |
| 5 | +/** |
| 6 | + * This is a helper component to auto-submit search values after a timeout |
| 7 | + */ |
| 8 | +const AutoSubmitSearch = () => { |
| 9 | + const timerRef = useRef(null) |
| 10 | + |
| 11 | + const { values, touched, submitForm } = useFormikContext() |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + // Check if input was touched. Formik behavior is to update 'touched' |
| 15 | + // flag on input blur or submit, but we want to submit changes also on |
| 16 | + // key press. 'touched' is not a useEffect dependency because it is |
| 17 | + // constantly updated without value changes. |
| 18 | + const isTouched = touched.search || values?.search.length > 0 |
| 19 | + |
| 20 | + // If search is touched |
| 21 | + if (isTouched) { |
| 22 | + // Clear previous timeout, if exists |
| 23 | + if (timerRef.current) { |
| 24 | + clearTimeout(timerRef.current) |
| 25 | + } |
| 26 | + // Define new timeout |
| 27 | + timerRef.current = setTimeout(submitForm, 1000) |
| 28 | + } |
| 29 | + |
| 30 | + // Clear timeout on unmount |
| 31 | + return () => timerRef.current && clearTimeout(timerRef.current) |
| 32 | + }, [values]) |
| 33 | + |
| 34 | + return null |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * The search input |
| 39 | + */ |
| 40 | +const SearchInput = ({ onSearch, placeholder, 'data-cy': dataCy }) => { |
| 41 | + return ( |
| 42 | + <Formik |
| 43 | + initialValues={{ search: '' }} |
| 44 | + onSubmit={({ search }) => onSearch(search)} |
| 45 | + > |
| 46 | + <Form |
| 47 | + className='form-control justify-start' |
| 48 | + style={{ alignItems: 'stretch' }} |
21 | 49 | > |
22 | | - Search |
23 | | - </Button> |
24 | | - </Form> |
25 | | - </Formik> |
26 | | -) |
| 50 | + <Field |
| 51 | + data-cy={`${dataCy}-search-input`} |
| 52 | + type='search' |
| 53 | + name='search' |
| 54 | + id='search' |
| 55 | + placeholder={placeholder} |
| 56 | + style={{ width: '12rem' }} |
| 57 | + /> |
| 58 | + <Button |
| 59 | + data-cy={`${dataCy}-search-submit`} |
| 60 | + type='submit' |
| 61 | + variant='submit' |
| 62 | + useIcon='magnifier-left' |
| 63 | + flat |
| 64 | + /> |
| 65 | + <AutoSubmitSearch /> |
| 66 | + </Form> |
| 67 | + </Formik> |
| 68 | + ) |
| 69 | +} |
27 | 70 |
|
28 | 71 | export default SearchInput |
0 commit comments