-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiselect.tsx
130 lines (115 loc) · 3.58 KB
/
Multiselect.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
import React, { useState } from "react";
import lodash from "lodash";
const COMP_PREFIX = "multi-select-comp";
interface Option {
label: string;
value: string;
selected?: boolean;
default?: boolean;
}
interface MultiselectProps {
options: Option[];
label: string;
handleOnchange: Function;
}
const getSelectedOptions = (options: Option[]): Option[] => {
return options.filter(opt => opt.selected === true);
};
const getUnselectedOptions = (options: Option[]): Option[] => {
return options.filter(opt => !opt.selected);
};
const updateOptionsUtil = (
option: Option,
isSelected: boolean,
options: Option[]
): Option[] => {
let copyOptions: Option[] = lodash.cloneDeep(options);
copyOptions = copyOptions.map(opt => {
if (option.value === opt.value) {
opt.selected = isSelected;
}
return opt;
});
return copyOptions;
};
const clearOptions = (options: Option[]): Option[] => {
let copyOptions = options.map(opt => {
opt.selected = false;
return opt;
});
return copyOptions;
};
const Multiselect: React.FunctionComponent<MultiselectProps> = (
props: MultiselectProps
) => {
const { options, label, handleOnchange } = props;
const [openState, setOpenState] = useState(false);
const [opts, updateOptions] = useState(options);
const selectedOptions = getSelectedOptions(opts);
const unselectedOptions = getUnselectedOptions(opts);
return (
<div className={COMP_PREFIX}>
<div
className={`${COMP_PREFIX}-container`}
onClick={() => setOpenState(!openState)}
>
<div>
<span className={`${COMP_PREFIX}-label`}>{label}</span>
<span className={`${COMP_PREFIX}-selected`}>
{`${selectedOptions.length} selected`}
</span>
</div>
<div className={`${COMP_PREFIX}-arrow`} />
</div>
{openState && (
<div className={`${COMP_PREFIX}-options-container`}>
<div className={`${COMP_PREFIX}-label-container`}>
<span className={`${COMP_PREFIX}-selected-label`}>Selected</span>
<span
className={`${COMP_PREFIX}-clear-label`}
onClick={() => updateOptions(clearOptions(opts))}
>
Clear
</span>
</div>
{getSelectedOptions(opts).map(opt => (
<div
className={`${COMP_PREFIX}-option ${COMP_PREFIX}-option--selected`}
key={opt.value}
>
<input
type="checkbox"
className={`${COMP_PREFIX}-option-input`}
checked={opt.selected}
onChange={e => {
updateOptions(updateOptionsUtil(opt, e.target.checked, opts));
handleOnchange(selectedOptions, unselectedOptions);
}}
/>
{opt.label}
</div>
))}
<div className={`${COMP_PREFIX}-options-separator`} />
{getUnselectedOptions(opts).map(opt => (
<div
className={`${COMP_PREFIX}-option ${COMP_PREFIX}-option--unselected`}
key={opt.value}
>
<input
type="checkbox"
className={`${COMP_PREFIX}-option-input`}
checked={opt.selected}
onChange={e => {
updateOptions(updateOptionsUtil(opt, e.target.checked, opts));
handleOnchange(selectedOptions, unselectedOptions);
}}
/>
{opt.label}
</div>
))}
</div>
)}
</div>
);
};
export default Multiselect;