|
1 | | -import { Accordion, AccordionSummary, AccordionDetails, Typography } from '@mui/material' |
| 1 | +import { Accordion, AccordionSummary, AccordionDetails, Typography, Chip, Stack, Box } from '@mui/material' |
2 | 2 | import ExpandMoreIcon from '@mui/icons-material/ExpandMore' |
3 | 3 | 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 | +} |
4 | 53 |
|
5 | 54 | interface FilterAccordionProps { |
6 | 55 | title: string |
7 | 56 | children: ReactNode |
| 57 | + filterId?: string |
8 | 58 | } |
9 | 59 |
|
10 | | -const FilterAccordion = ({ title, children }: FilterAccordionProps) => { |
| 60 | +const FilterAccordion = ({ title, children, filterId }: FilterAccordionProps) => { |
11 | 61 | return ( |
12 | 62 | <Accordion> |
13 | 63 | <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> |
15 | 68 | </AccordionSummary> |
16 | 69 | <AccordionDetails> |
17 | 70 | {children} |
|
0 commit comments