forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactTableGlobalFilter.js
More file actions
65 lines (61 loc) · 1.62 KB
/
ReactTableGlobalFilter.js
File metadata and controls
65 lines (61 loc) · 1.62 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from 'react'
import { useAsyncDebounce } from 'react-table'
import { any, string } from 'prop-types'
import {
Input,
InputGroup,
InputLeftElement,
Stack,
Text,
} from '@chakra-ui/react'
import { SearchIcon } from '@chakra-ui/icons'
import { t, Trans } from '@lingui/macro'
import { useLingui } from '@lingui/react'
export function ReactTableGlobalFilter({
title,
preGlobalFilteredRows,
globalFilter,
setGlobalFilter,
placeholder,
}) {
const { i18n } = useLingui()
const count = preGlobalFilteredRows.length
const [value, setValue] = React.useState(globalFilter)
const onChange = useAsyncDebounce((value) => {
setGlobalFilter(value || undefined)
}, 200)
return (
<Stack isInline align="center">
<Text
as="label"
htmlFor={`${title.replace(/\s+/g, '-')}-search-field`}
fontWeight="bold"
>
<Trans>Search:</Trans>
</Text>
<InputGroup w={{ sm: '100%', md: '20rem' }}>
<InputLeftElement aria-hidden="true">
<SearchIcon />
</InputLeftElement>
<Input
id={`${title.replace(/\s+/g, '-')}-search-field`}
value={value || ''}
onChange={(e) => {
setValue(e.target.value)
onChange(e.target.value)
}}
placeholder={placeholder || i18n._(t`${count} records...`)}
aria-label="Filter the table"
/>
</InputGroup>
</Stack>
)
}
ReactTableGlobalFilter.propTypes = {
// TODO: Add accurate prop types for these
title: string,
preGlobalFilteredRows: any,
globalFilter: any,
setGlobalFilter: any,
placeholder: string,
}