-
Notifications
You must be signed in to change notification settings - Fork 3
/
CheckboxFilter.tsx
146 lines (138 loc) · 3.32 KB
/
CheckboxFilter.tsx
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as React from "react";
import {
Autocomplete,
Box,
Checkbox,
TextField,
Typography,
autocompleteClasses
} from "@mui/material";
import { CheckBox, CheckBoxOutlineBlank } from "@mui/icons-material";
import AlwaysOpenAutocomplete from "../AlwaysOpenAutocomplete";
import { CheckboxFilterProps } from "./CheckboxFilter.types";
import { HTMLAttributes } from "react";
import { sortFilterOptions } from "../sortFilterOptions";
/**
* A checkbox filter allows the user to select multiple options from a list.
*/
export default function CheckboxFilter({
variant = "popper",
limitTags = -1,
options = [],
value = [],
...props
}: CheckboxFilterProps) {
// create default values object and merge with other props
const defaults = {
limitTags,
options,
value
};
const allProps = { ...props, ...defaults };
// return popper components
return variant === "popper" ? (
<CheckboxFilterPopper {...allProps} />
) : (
<CheckboxFilterAlwaysOpen {...allProps} />
);
}
/**
* An inline checkbox filter is always open and the popper does not sit above other elements.
*/
function CheckboxFilterAlwaysOpen({
label,
limitTags,
name,
onChange,
options,
value,
disabled
}: Omit<CheckboxFilterProps, "variant">) {
// ensure the onChange value is always an array because we are using a multi-select
const handleOnChange = (newValue: string | string[] | null) => {
if (newValue === null) {
onChange([]);
} else {
onChange(Array.isArray(newValue) ? newValue : [newValue]);
}
};
return (
<AlwaysOpenAutocomplete
limitTags={limitTags}
multiple
onChange={(_e, newValue) => handleOnChange(newValue)}
options={sortFilterOptions(options)}
renderInput={params => {
return (
<TextField
{...params}
label={label}
name={name}
sx={{
[`& .${autocompleteClasses.popupIndicator}`]: { display: "none" }
}}
/>
);
}}
renderOption={Option}
value={value}
disabled={disabled}
/>
);
}
/**
* A checkbox filter that opens a popper above other elements.
*/
function CheckboxFilterPopper({
label,
limitTags,
name,
onChange,
options,
value,
disabled
}: Omit<CheckboxFilterProps, "variant">) {
return (
<Autocomplete
limitTags={limitTags}
multiple
onChange={(_e, newValue) => onChange(newValue)}
options={sortFilterOptions(options)}
renderInput={params => (
<TextField {...params} label={label} name={name} />
)}
renderOption={Option}
value={value}
disabled={disabled}
/>
);
}
// renderer for a checkbox option
function Option(
props: HTMLAttributes<HTMLLIElement>,
option: string,
{ selected }: { selected: boolean },
{ disabled }: Pick<CheckboxFilterProps, "disabled">
) {
return (
<Box
component="li"
{...props}
sx={{
...(disabled && {
opacity: 0.5,
pointerEvents: "none"
})
}}
>
<Checkbox
checkedIcon={<CheckBox fontSize="small" />}
checked={selected}
disabled={disabled}
icon={<CheckBoxOutlineBlank fontSize="small" />}
value={option}
/>
<Typography data-testid="filter-option-label">{option}</Typography>
</Box>
);
}