-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathOptionGroupCheckBox.tsx
91 lines (87 loc) · 2.72 KB
/
OptionGroupCheckBox.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
import { cx } from "emotion";
import * as React from "react";
import { getSelectedCheckboxes } from "./utils/getSelectedCheckboxes";
import Button from "./Button";
import OptionGroup from "./shared/OptionGroup";
import { OptionGroupCheckBoxProps } from "./typings/OptionGroupCheckBox";
import * as styles from "../components/styles/OptionGroupCheckBox.styles";
import { OptionProps } from "./typings/Option";
export default class OptionGroupCheckBox<
OptionType
> extends React.PureComponent<OptionGroupCheckBoxProps<OptionType>> {
isSelected = (value: OptionType) => {
const { selected } = this.props;
return !!selected && selected.includes(value);
};
handleChange: OptionGroup<OptionType>["props"]["handleChange"] = (
{ value },
event
) => {
this.props.onChange(
getSelectedCheckboxes<OptionType>(value, this.props.selected),
{
props: this.props,
event
}
);
};
selectVisible = () => {
const { children } = this.props;
const _values = (
React.Children.map(
children,
child => child as React.ReactElement<OptionProps<OptionType>>
) || []
)
.filter(_child => _child && _child.props && _child.props.value)
.map(_child => _child && _child.props && _child.props.value);
this.props.onChange(_values, { props: this.props });
};
clearVisible = () => {
this.props.onChange([], { props: this.props, event });
};
onApply = () => {
const { onApply, selected } = this.props;
if (onApply) onApply(selected || [], this.props);
};
render() {
const {
wrapClassName,
onApply,
onClear,
isSelected,
onChange,
...rest
} = this.props;
const advancedOptionsProps = {
selectVisible: this.selectVisible,
clearVisible: this.clearVisible,
...this.props.advancedOptionsProps
};
const isSearchMessagePresent = !!rest.searchBoxProps?.message;
const _optionGroupCheckboxWrap = isSearchMessagePresent
? styles.optionGroupCheckBoxWrapWithMessage
: styles.optionGroupCheckBoxWrap;
return (
<div className={cx(_optionGroupCheckboxWrap, wrapClassName)}>
<OptionGroup<OptionType>
{...rest}
advancedOptionsProps={advancedOptionsProps}
isSelected={isSelected || this.isSelected}
handleChange={this.handleChange}
multiSelect
/>
{(onApply || onClear) && (
<div className={styles.optionGroupCheckBoxButtonWrap}>
{onClear && (
<Button type="secondary" onClick={onClear}>
Clear
</Button>
)}
{onApply && <Button onClick={this.onApply}>Apply</Button>}
</div>
)}
</div>
);
}
}