-
Notifications
You must be signed in to change notification settings - Fork 12
/
GenericFilterChip.jsx
65 lines (62 loc) · 1.94 KB
/
GenericFilterChip.jsx
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 {
Chip, Tooltip, Menu, MenuItem, MenuList
} from '@mui/material';
import {
ExpandLess as ExpandLessIcon,
ExpandMore as ExpandMoreIcon,
FilterList as FilterIcon,
Cancel as CancelIcon,
} from '@mui/icons-material';
import { startCase, map } from 'lodash';
const GenericFilterChip = ({id, name, options, tooltip, onChange, value, size}) => {
const anchorRef = React.useRef(null);
const [open, setOpen] = React.useState(false);
const label = name || startCase(id)
const labelWithValue = `${label}${value ? '=' + value : ''}`
const tooltipTitle = tooltip || `Filter by ${label}`
const onValueChange = option => {
setOpen(false)
onChange(id, option)
return false
}
return (
<React.Fragment key={id}>
<Tooltip arrow title={tooltipTitle}>
<Chip
icon={<FilterIcon fontSize='inherit' />}
color={value ? 'primary' : 'secondary'}
label={labelWithValue}
size={size || 'medium'}
deleteIcon={
value ? <CancelIcon fontSize='inherit' /> : (open ? <ExpandLessIcon fontSize='inherit' /> : <ExpandMoreIcon fontSize='inherit' />)
}
onDelete={() => value ? onValueChange(null) : setOpen(!open)}
onClick={() => setOpen(!open)}
ref={anchorRef}
variant="outlined"
/>
</Tooltip>
{
open &&
<Menu open={open} anchorEl={anchorRef.current} onClose={() => setOpen(false)}>
<MenuList id="split-button-menu">
{
map(options, (option, index) => (
<MenuItem
id={option}
key={index}
selected={option === value}
onClick={() => onValueChange(option)}
>
{option}
</MenuItem>
))
}
</MenuList>
</Menu>
}
</React.Fragment>
)
}
export default GenericFilterChip;