-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from icflorescu:feature/filterable_columns
Feature/filterable_columns
- Loading branch information
Showing
6 changed files
with
185 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,122 @@ | ||
import { Box, Checkbox, Grid, TextInput } from '@mantine/core'; | ||
import { Box, Button, MultiSelect, Stack, TextInput } from '@mantine/core'; | ||
import { DatePicker, type DatesRangeValue } from '@mantine/dates'; | ||
import { useDebouncedValue } from '@mantine/hooks'; | ||
import { IconSearch } from '@tabler/icons-react'; | ||
import dayjs from 'dayjs'; | ||
import { DataTable } from 'mantine-datatable'; | ||
import { useEffect, useState } from 'react'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { employees } from '~/data'; | ||
|
||
const initialRecords = employees.slice(0, 100); | ||
|
||
export default function SearchingAndFilteringExample() { | ||
const [records, setRecords] = useState(initialRecords); | ||
|
||
const departments = useMemo(() => { | ||
const departments = new Set(employees.map((e) => e.department.name)); | ||
return [...departments]; | ||
}, []); | ||
|
||
const [query, setQuery] = useState(''); | ||
const [veteransOnly, setVeteransOnly] = useState(false); | ||
const [selectedDepartments, setSelectedDepartments] = useState<string[]>([]); | ||
const [birthdaySearchRange, setBirthdaySearchRange] = useState<DatesRangeValue>(); | ||
const [debouncedQuery] = useDebouncedValue(query, 200); | ||
|
||
useEffect(() => { | ||
const now = dayjs(); | ||
setRecords( | ||
initialRecords.filter(({ firstName, lastName, department, birthDate }) => { | ||
if (veteransOnly && now.diff(birthDate, 'years') < 40) { | ||
if ( | ||
debouncedQuery !== '' && | ||
!`${firstName} ${lastName}`.toLowerCase().includes(debouncedQuery.trim().toLowerCase()) | ||
) { | ||
return false; | ||
} | ||
|
||
if ( | ||
debouncedQuery !== '' && | ||
!`${firstName} ${lastName} ${department.name} ${department.company.name}` | ||
.toLowerCase() | ||
.includes(debouncedQuery.trim().toLowerCase()) | ||
birthdaySearchRange && | ||
birthdaySearchRange[0] && | ||
birthdaySearchRange[1] && | ||
(dayjs(birthdaySearchRange[0]).isAfter(birthDate, 'day') || | ||
dayjs(birthdaySearchRange[1]).isBefore(birthDate, 'day')) | ||
) { | ||
return false; | ||
} | ||
|
||
if (selectedDepartments.length && !selectedDepartments.some((d) => d === department.name)) { | ||
return false; | ||
} | ||
return true; | ||
}) | ||
); | ||
}, [debouncedQuery, veteransOnly]); | ||
}, [debouncedQuery, birthdaySearchRange, selectedDepartments]); | ||
|
||
return ( | ||
<> | ||
<Grid align="center" mb="md"> | ||
<Grid.Col xs={8} sm={9}> | ||
<TextInput | ||
sx={{ flexBasis: '60%' }} | ||
placeholder="Search employees..." | ||
icon={<IconSearch size={16} />} | ||
value={query} | ||
onChange={(e) => setQuery(e.currentTarget.value)} | ||
/> | ||
</Grid.Col> | ||
<Grid.Col xs={4} sm={3}> | ||
<Checkbox | ||
label="Over 40 years old" | ||
checked={veteransOnly} | ||
onChange={(e) => setVeteransOnly(e.currentTarget.checked)} | ||
/> | ||
</Grid.Col> | ||
</Grid> | ||
<Box sx={{ height: 300 }}> | ||
<DataTable | ||
withBorder | ||
records={records} | ||
columns={[ | ||
{ accessor: 'name', render: ({ firstName, lastName }) => `${firstName} ${lastName}` }, | ||
{ accessor: 'department.name' }, | ||
{ accessor: 'department.company.name' }, | ||
{ accessor: 'birthDate', render: ({ birthDate }) => dayjs(birthDate).format('MMM DD YYYY') }, | ||
{ accessor: 'age', render: ({ birthDate }) => dayjs().diff(birthDate, 'y') }, | ||
]} | ||
/> | ||
</Box> | ||
</> | ||
<Box sx={{ height: 300 }}> | ||
<DataTable | ||
withBorder | ||
records={records} | ||
columns={[ | ||
{ | ||
accessor: 'name', | ||
render: ({ firstName, lastName }) => `${firstName} ${lastName}`, | ||
filter: ( | ||
<TextInput | ||
label="Employees" | ||
description="Show employees whose names include the specified text" | ||
placeholder="Search employees..." | ||
icon={<IconSearch size={16} />} | ||
value={query} | ||
onChange={(e) => setQuery(e.currentTarget.value)} | ||
/> | ||
), | ||
filtering: query !== '', | ||
}, | ||
{ | ||
accessor: 'department.name', | ||
filter: ( | ||
<MultiSelect | ||
label="Departments " | ||
description="Show all employees working at the selected departments" | ||
data={departments} | ||
value={selectedDepartments} | ||
placeholder="Search departments…" | ||
onChange={setSelectedDepartments} | ||
icon={<IconSearch size={16} />} | ||
clearable | ||
searchable | ||
/> | ||
), | ||
filtering: selectedDepartments.length > 0, | ||
}, | ||
{ accessor: 'department.company.name' }, | ||
{ | ||
accessor: 'birthDate', | ||
render: ({ birthDate }) => dayjs(birthDate).format('MMM DD YYYY'), | ||
filter: ({ close }) => ( | ||
<Stack> | ||
<DatePicker | ||
maxDate={new Date()} | ||
type="range" | ||
value={birthdaySearchRange} | ||
onChange={setBirthdaySearchRange} | ||
/> | ||
<Button | ||
disabled={!birthdaySearchRange} | ||
color="red" | ||
onClick={() => { | ||
setBirthdaySearchRange(undefined); | ||
close(); | ||
}} | ||
> | ||
Reset | ||
</Button> | ||
</Stack> | ||
), | ||
filtering: Boolean(birthdaySearchRange), | ||
}, | ||
{ accessor: 'age', render: ({ birthDate }) => dayjs().diff(birthDate, 'y') }, | ||
]} | ||
/> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters