|
1 | 1 | import T from 'prop-types' |
2 | 2 | import Table from './table' |
3 | 3 | import { useFetchList } from '../../hooks/use-fetch-list' |
4 | | -import { useState } from 'react' |
| 4 | +import { useEffect, useRef, useState } from 'react' |
5 | 5 | import Pagination from '../pagination' |
6 | 6 | import qs from 'qs' |
7 | | -import { Field, Form, Formik } from 'formik' |
| 7 | +import { Field, Form, Formik, useFormikContext } from 'formik' |
8 | 8 | import Button from '../button' |
9 | 9 |
|
10 | | -const SearchInput = ({ onSearch, 'data-cy': dataCy }) => ( |
11 | | - <Formik |
12 | | - initialValues={{ search: '' }} |
13 | | - onSubmit={({ search }) => onSearch(search)} |
14 | | - > |
15 | | - <Form className='form-control justify-start'> |
16 | | - <Field |
17 | | - data-cy={`${dataCy}-search-input`} |
18 | | - type='search' |
19 | | - name='search' |
20 | | - id='search' |
21 | | - placeholder='Search username...' |
22 | | - style={{ width: '12rem' }} |
23 | | - /> |
24 | | - <Button |
25 | | - data-cy={`${dataCy}-search-submit`} |
26 | | - type='submit' |
27 | | - variant='submit' |
28 | | - > |
29 | | - Search |
30 | | - </Button> |
31 | | - </Form> |
32 | | - </Formik> |
33 | | -) |
| 10 | +/** |
| 11 | + * This is a helper component to auto-submit search values after a timeout |
| 12 | + */ |
| 13 | +const AutoSubmitSearch = () => { |
| 14 | + const timerRef = useRef(null) |
| 15 | + |
| 16 | + const { values, touched, submitForm } = useFormikContext() |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + // If search is touched |
| 20 | + if (touched.search) { |
| 21 | + // Clear previous timeout, if exists |
| 22 | + if (timerRef.current) { |
| 23 | + clearTimeout(timerRef.current) |
| 24 | + } |
| 25 | + // Define new timeout |
| 26 | + timerRef.current = setTimeout(submitForm, 1000) |
| 27 | + } |
| 28 | + |
| 29 | + // Clear timeout on unmount |
| 30 | + return () => timerRef.current && clearTimeout(timerRef.current) |
| 31 | + }, [values, touched, submitForm]) |
| 32 | + |
| 33 | + return null |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * The search input |
| 38 | + */ |
| 39 | +const SearchInput = ({ onSearch, 'data-cy': dataCy }) => { |
| 40 | + return ( |
| 41 | + <Formik |
| 42 | + initialValues={{ search: '' }} |
| 43 | + onSubmit={({ search }) => onSearch(search)} |
| 44 | + > |
| 45 | + <Form className='form-control justify-start'> |
| 46 | + <Field |
| 47 | + data-cy={`${dataCy}-search-input`} |
| 48 | + type='search' |
| 49 | + name='search' |
| 50 | + id='search' |
| 51 | + placeholder='Search username...' |
| 52 | + style={{ width: '12rem' }} |
| 53 | + /> |
| 54 | + <Button |
| 55 | + data-cy={`${dataCy}-search-submit`} |
| 56 | + type='submit' |
| 57 | + variant='submit' |
| 58 | + > |
| 59 | + Search |
| 60 | + </Button> |
| 61 | + <AutoSubmitSearch /> |
| 62 | + </Form> |
| 63 | + </Formik> |
| 64 | + ) |
| 65 | +} |
34 | 66 |
|
35 | 67 | function UsersTable({ type, orgId, onRowClick, isSearchable }) { |
36 | 68 | const [page, setPage] = useState(1) |
37 | | - |
38 | 69 | const [search, setSearch] = useState(null) |
39 | 70 |
|
40 | 71 | let apiBasePath |
@@ -70,6 +101,10 @@ function UsersTable({ type, orgId, onRowClick, isSearchable }) { |
70 | 101 | isLoading, |
71 | 102 | } = useFetchList(`${apiBasePath}?${querystring}`) |
72 | 103 |
|
| 104 | + if (!isLoading && search?.length > 0) { |
| 105 | + emptyMessage = 'Search returned no results.' |
| 106 | + } |
| 107 | + |
73 | 108 | return ( |
74 | 109 | <> |
75 | 110 | {isSearchable && ( |
|
0 commit comments