Skip to content

Commit 00a775d

Browse files
committed
Reduce duplication in TreeNode handlers
1 parent 2224a86 commit 00a775d

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

src/js/TreeNode.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,50 +56,25 @@ class TreeNode extends React.Component {
5656
}
5757

5858
onCheck() {
59-
let isChecked = false;
59+
const { value, onCheck } = this.props;
6060

61-
// Toggle off state to checked
62-
if (this.props.checked === 0) {
63-
isChecked = true;
64-
}
65-
66-
// Toggle partial state based on cascade model
67-
if (this.props.checked === 2) {
68-
isChecked = this.props.optimisticToggle;
69-
}
70-
71-
this.props.onCheck({
72-
value: this.props.value,
73-
checked: isChecked,
74-
});
61+
onCheck({ value, checked: this.getCheckState({ toggle: true }) });
7562
}
7663

7764
onClick() {
7865
const {
79-
checked,
8066
expandOnClick,
8167
isParent,
82-
optimisticToggle,
8368
value,
8469
onClick,
8570
} = this.props;
86-
let isChecked = false;
87-
88-
if (checked === 1) {
89-
isChecked = true;
90-
}
91-
92-
// Get partial state based on cascade model
93-
if (checked === 2) {
94-
isChecked = optimisticToggle;
95-
}
9671

9772
// Auto expand if enabled
9873
if (isParent && expandOnClick) {
9974
this.onExpand();
10075
}
10176

102-
onClick({ value, checked: isChecked });
77+
onClick({ value, checked: this.getCheckState({ toggle: false }) });
10378
}
10479

10580
onExpand() {
@@ -108,6 +83,27 @@ class TreeNode extends React.Component {
10883
onExpand({ value, expanded: !expanded });
10984
}
11085

86+
getCheckState({ toggle }) {
87+
const { checked, optimisticToggle } = this.props;
88+
89+
// Toggle off state to checked
90+
if (checked === 0 && toggle) {
91+
return true;
92+
}
93+
94+
// Node is already checked and we are not toggling
95+
if (checked === 1 && !toggle) {
96+
return true;
97+
}
98+
99+
// Get/toggle partial state based on cascade model
100+
if (checked === 2) {
101+
return optimisticToggle;
102+
}
103+
104+
return false;
105+
}
106+
111107
renderCollapseButton() {
112108
const { expandDisabled, isLeaf, lang } = this.props;
113109

0 commit comments

Comments
 (0)