-
-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathCheckboxList.jsx
More file actions
111 lines (95 loc) · 3.09 KB
/
CheckboxList.jsx
File metadata and controls
111 lines (95 loc) · 3.09 KB
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
import PropTypes from "prop-types";
import PureComponent from "../lib/PureComponent.jsx";
import * as styles from "./CheckboxList.css";
import CheckboxListItem from "./CheckboxListItem.jsx";
import { ViewerDataType } from "./types.js";
const ALL_ITEM = Symbol("ALL_ITEM");
export default class CheckboxList extends PureComponent {
static propTypes = {
label: PropTypes.string.isRequired,
renderLabel: PropTypes.func.isRequired,
items: PropTypes.oneOfType([ViewerDataType, PropTypes.symbol]),
checkedItems: PropTypes.oneOfType([ViewerDataType, PropTypes.symbol]),
onChange: PropTypes.func.isRequired,
};
static ALL_ITEM = ALL_ITEM;
constructor(props) {
super(props);
this.state = {
checkedItems: props.checkedItems || props.items,
};
}
componentWillReceiveProps(newProps) {
if (newProps.items !== this.props.items) {
if (this.isAllChecked()) {
// Preserving `all checked` state
this.setState({ checkedItems: newProps.items });
this.informAboutChange(newProps.items);
} else if (this.state.checkedItems.length) {
// Checking only items that are in the new `items` array
const checkedItems = newProps.items.filter((item) =>
this.state.checkedItems.find(
(checkedItem) => checkedItem.label === item.label,
),
);
this.setState({ checkedItems });
this.informAboutChange(checkedItems);
}
} else if (newProps.checkedItems !== this.props.checkedItems) {
this.setState({ checkedItems: newProps.checkedItems });
}
}
render() {
const { label, items, renderLabel } = this.props;
return (
<div className={styles.container}>
<div className={styles.label}>{label}:</div>
<div>
<CheckboxListItem
item={ALL_ITEM}
checked={this.isAllChecked()}
onChange={this.handleToggleAllCheck}
>
{renderLabel}
</CheckboxListItem>
{items.map((item) => (
<CheckboxListItem
key={item.label}
item={item}
checked={this.isItemChecked(item)}
onChange={this.handleItemCheck}
>
{renderLabel}
</CheckboxListItem>
))}
</div>
</div>
);
}
handleToggleAllCheck = () => {
const checkedItems = this.isAllChecked() ? [] : this.props.items;
this.setState({ checkedItems });
this.informAboutChange(checkedItems);
};
handleItemCheck = (item) => {
let checkedItems;
if (this.isItemChecked(item)) {
checkedItems = this.state.checkedItems.filter(
(checkedItem) => checkedItem !== item,
);
} else {
checkedItems = [...this.state.checkedItems, item];
}
this.setState({ checkedItems });
this.informAboutChange(checkedItems);
};
isItemChecked(item) {
return this.state.checkedItems.includes(item);
}
isAllChecked() {
return this.props.items.length === this.state.checkedItems.length;
}
informAboutChange(checkedItems) {
setTimeout(() => this.props.onChange(checkedItems));
}
}