Skip to content

Commit f999cf1

Browse files
jakezateckydpellierjalalazimiPhamTienThanhCong
committed
Fix issue with filtering setting empty children arrays
Supersedes #202, #217, #445 Resolves #153, #196, #216 Co-authored-by: Damien Pellier <dpellier@users.noreply.github.com> Co-authored-by: Jalal azimi <jalalazimi@gmail.com> Co-authored-by: Phạm Tiến Thành Công <cong.pttc@gmail.com>
1 parent dfcfc06 commit f999cf1

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#### Other
2929

3030
* Drop support for React before v16.8
31+
* Fix various issues with the filtering example improperly rendering parents and child nodes (#153, #196, #216)
3132

3233
### Added
3334

examples/src/js/FilterExample.jsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,28 @@ class FilterExample extends Component {
5757
}
5858

5959
filterNodes(filtered, node) {
60-
const { filterText } = this.state;
61-
const children = (node.children || []).reduce(this.filterNodes, []);
62-
63-
if (
60+
if (this.nodeMatchesSearchString(node)) {
6461
// Node's label matches the search string
65-
node.label.toLocaleLowerCase().indexOf(filterText.toLocaleLowerCase()) > -1 ||
66-
// Or a children has a matching node
67-
children.length
68-
) {
69-
filtered.push({ ...node, children });
62+
filtered.push(node);
63+
} else {
64+
// Find if any children match the search string or have descendants who do
65+
const filteredChildren = (node.children || []).reduce(this.filterNodes, []);
66+
67+
// If so, render these children
68+
if (filteredChildren.length > 0) {
69+
filtered.push({ ...node, children: filteredChildren });
70+
}
7071
}
7172

7273
return filtered;
7374
}
7475

76+
nodeMatchesSearchString({ label }) {
77+
const { filterText } = this.state;
78+
79+
return label.toLocaleLowerCase().indexOf(filterText.toLocaleLowerCase()) > -1;
80+
}
81+
7582
render() {
7683
const {
7784
checked,

0 commit comments

Comments
 (0)