Skip to content

Commit b0e15a5

Browse files
another option for how active filters could be displayed
1 parent 08055c7 commit b0e15a5

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/client/V2/FilterAccordion.tsx

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
1-
import { Accordion, AccordionSummary, AccordionDetails, Typography } from '@mui/material'
1+
import { Accordion, AccordionSummary, AccordionDetails, Typography, Chip, Stack, Box } from '@mui/material'
22
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'
33
import { ReactNode } from 'react'
4+
import { useFilterContext, filterConfigMap, getFilterVariant } from './filterContext'
5+
6+
interface ActiveFilterChipsProps {
7+
filterId: string
8+
}
9+
10+
const ActiveFilterChips = ({ filterId }: ActiveFilterChipsProps) => {
11+
const filterContext = useFilterContext()
12+
const cfg = filterConfigMap(filterContext).get(filterId)
13+
const variant = getFilterVariant(filterContext, filterId)
14+
15+
if (!cfg) return null
16+
17+
const activeChips: { id: string; label: string }[] = []
18+
if (Array.isArray(cfg.state)) {
19+
cfg.state.forEach((valueId: string) => {
20+
const option = variant?.options?.find((o: any) => o.id === valueId)
21+
activeChips.push({ id: valueId, label: option?.name || valueId })
22+
})
23+
} else if (cfg.state !== '') {
24+
const option = variant?.options?.find((o: any) => o.id === cfg.state)
25+
activeChips.push({ id: cfg.state, label: option?.name || cfg.state })
26+
}
27+
28+
if (activeChips.length === 0) return null
29+
30+
const handleDelete = (e: React.MouseEvent, valueId: string) => {
31+
e.stopPropagation()
32+
if (Array.isArray(cfg.state)) {
33+
cfg.setState(cfg.state.filter((id: string) => id !== valueId))
34+
} else {
35+
cfg.setState('')
36+
}
37+
}
38+
39+
return (
40+
<Stack direction="row" spacing={0.5} flexWrap="wrap" onClick={(e) => e.stopPropagation()} sx={{ ml: 'auto' }}>
41+
{activeChips.map((chip) => (
42+
<Chip
43+
key={chip.id}
44+
label={chip.label}
45+
size="small"
46+
onDelete={(e) => handleDelete(e, chip.id)}
47+
sx={{ pointerEvents: 'all' }}
48+
/>
49+
))}
50+
</Stack>
51+
)
52+
}
453

554
interface FilterAccordionProps {
655
title: string
756
children: ReactNode
57+
filterId?: string
858
}
959

10-
const FilterAccordion = ({ title, children }: FilterAccordionProps) => {
60+
const FilterAccordion = ({ title, children, filterId }: FilterAccordionProps) => {
1161
return (
1262
<Accordion>
1363
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
14-
<Typography>{title}</Typography>
64+
<Box sx={{ display: 'flex', alignItems: 'center', width: '100%', flexWrap: 'wrap', gap: 0.5 }}>
65+
<Typography sx={{ mr: 1 }}>{title}</Typography>
66+
{filterId && <ActiveFilterChips filterId={filterId} />}
67+
</Box>
1568
</AccordionSummary>
1669
<AccordionDetails>
1770
{children}

src/client/V2/FilterRenderer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const FilterRenderer = ({ filter }: { filter: any }) => {
3232
<FilterAccordion
3333
key={filter.id}
3434
title={filterToRender.shortName}
35+
filterId={filter.id}
3536
>
3637
<Filter variant={variant} filter={filterToRender} />
3738
</FilterAccordion>

0 commit comments

Comments
 (0)