const AllFilterExample = () => {
const [states, setStates] = React.useState({
sort: 1,
category: [],
});
const sortOptions = [
{
text: 'Newest',
value: 0,
},
{
text: 'Oldest',
value: 1,
},
{
text: 'Name: A to Z',
value: 2,
},
{
text: 'Name: Z to A',
value: 3,
},
{
text: 'Price: Low to High',
value: 4,
},
{
text: 'Price: High to Low',
value: 5,
},
];
const categoryOptions = [
{
text: 'Category 0',
value: 0,
},
{
text: 'Category 1',
value: 1,
},
{
text: 'Category 2',
value: 2,
},
{
text: 'Category 3',
value: 3,
},
{
text: 'Category 4',
value: 4,
},
{
text: 'Category 5',
value: 5,
},
];
const SortFilter = React.useCallback(() => {
const [open, setOpen] = React.useState(false);
React.useEffect(() => console.log('SortFilter effect'), []);
const onChangeHandler = (e) => {
setStates((prevState) => ({ ...prevState, sort: e }));
setOpen(false);
};
return (
<Popover
el="span"
direction="br"
padding="s"
onClickOutside={() => setOpen(false)}
open={open}
portal
trigger={
<FilterButton
onClick={() => setOpen(!open)}
value={
states.sort !== 0 && sortOptions[states.sort].text
}
label="Sort"
expandIcon={open}
/>
}
>
<OptionList options={sortOptions} onChange={onChangeHandler} />
</Popover>
);
}, [states]);
const CategoryFilter = () => {
const [value, setValue] = React.useState(states.category);
const [open, setOpen] = React.useState(false);
React.useEffect(() => console.log('CategoryFilter effect'), []);
const onChangeHandler = (data, checked) => {
if (checked) {
setValue((prevState) => [...prevState, data]);
} else {
setValue((prevState) =>
[...prevState].filter((item) => item !== data)
);
}
};
const onSaveHandler = () => {
setStates((prevState) => ({ ...prevState, category: value }));
setOpen(false);
};
const onCancelHandler = () => {
setValue(states.category);
setOpen(false);
};
return (
<Popover
el="span"
direction="br"
padding="s"
onClickOutside={() => setOpen(false)}
open={open}
portal
footer={
<Stack
direction="row-reverse"
spacing="1"
className="w-100"
>
<Button
size="small"
primary
onClick={onSaveHandler}
disabled={value === states.category}
>
Apply Filter
</Button>
<Button size="small" onClick={onCancelHandler}>
Cancel
</Button>
</Stack>
}
trigger={
<FilterButton
onClick={() => setOpen(!open)}
value={
states.category.length > 0 && states.category.length
}
label="Category"
expandIcon={open}
/>
}
>
<OptionList
multiple
options={categoryOptions}
onChange={onChangeHandler}
value={value}
/>
</Popover>
);
};
const AllFilter = () => {
const countFilter = () => {
if (states.sort !== 0 && states.category.length !== 0) return 2;
if (states.sort !== 0 || states.category.length !== 0) return 1;
return undefined;
};
const [IStates, setIStates] = React.useState(states);
const [open, setOpen] = React.useState(false);
const [counter, setCounter] = React.useState(countFilter);
React.useEffect(() => setCounter(countFilter), [IStates]);
const close = () => {
setIStates(states);
setOpen(false);
};
return (
<>
<FilterButton
label="all filters"
icon="funnel"
value={counter}
onClick={() => setOpen(true)}
/>
<Modal
open={open}
title="All Filters"
footer={
<Stack
direction="row-reverse"
justifyContent="space-between"
className="w-100"
>
<Stack direction="row-reverse" spacing={2}>
<Button
small
color="primary"
onClick={() => {
setStates(IStates);
close();
}}
>
Apply
</Button>
<Button small fill="subtle" onClick={close}>
Cancel
</Button>
</Stack>
<Link
primary
className="fs-2"
onClick={() =>
setIStates({ sort: 0, category: [] })
}
>
Clear
</Link>
</Stack>
}
>
<Form.AnvilSelect
value={IStates.sort}
options={sortOptions}
onChange={(e) =>
setIStates((prevState) => ({
...prevState,
sort: e.value,
}))
}
trigger={{ clearable: false }}
label="Sort by"
/>
<Form.AnvilSelect
multiple={{ selectAll: true }}
footer={{
actionName: 'Done',
onActionClick: () => null,
}}
value={IStates.category}
options={categoryOptions}
onChange={(e) => {
setIStates((prevState) => ({
...prevState,
category: e.map((a) => a.value),
}));
}}
label="Category"
trigger={{ placeholder: 'Select categories to filter' }}
/>
</Modal>
</>
);
};
return (
<Stack spacing={1}>
<AllFilter />
<SortFilter />
<CategoryFilter />
</Stack>
);
};
render (AllFilterExample)