Skip to content

Commit 0e86e09

Browse files
committed
Update FilterExample
Fix case insensitive detection, use Array.reduce instead of Array.map + Array.filter, and fix some formatting.
1 parent aa30601 commit 0e86e09

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

examples/src/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ <h2>Large Data Example</h2>
9494
<div id="large-data-example"></div>
9595

9696
<h2>Filter Example</h2>
97-
<p>Filtering of nodes is also easily possible</p>
97+
<p>Filtering of nodes is also easily possible.</p>
9898
<div id="filter-example"></div>
99-
99+
100100
<footer class="site-footer">
101101
<span class="site-footer-owner">
102102
<a href="https://github.com/jakezatecky/react-checkbox-tree">React Checkbox Tree</a> is maintained by <a href="https://github.com/jakezatecky">jakezatecky</a>.

examples/src/js/FilterExample.js

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,30 @@ const nodes = [
8181
];
8282

8383
class FilterExample extends React.Component {
84-
constructor() {
85-
super();
86-
87-
this.state = {
88-
checked: [
89-
'/app/Http/Controllers/WelcomeController.js',
90-
'/app/Http/routes.js',
91-
'/public/assets/style.css',
92-
'/public/index.html',
93-
'/.gitignore',
94-
],
95-
expanded: [
96-
'/app',
97-
],
98-
filterText: '',
99-
nodesFiltered: nodes,
100-
nodes,
101-
};
84+
state = {
85+
checked: [
86+
'/app/Http/Controllers/WelcomeController.js',
87+
'/app/Http/routes.js',
88+
'/public/assets/style.css',
89+
'/public/index.html',
90+
'/.gitignore',
91+
],
92+
expanded: [
93+
'/app',
94+
],
95+
filterText: '',
96+
nodesFiltered: nodes,
97+
nodes,
98+
};
99+
100+
constructor(props) {
101+
super(props);
102102

103103
this.onCheck = this.onCheck.bind(this);
104104
this.onExpand = this.onExpand.bind(this);
105-
this.setFilterText = this.setFilterText.bind(this);
105+
this.onFilterChange = this.onFilterChange.bind(this);
106106
this.filterTree = this.filterTree.bind(this);
107-
this.nodeFilter = this.nodeFilter.bind(this);
107+
this.filterNodes = this.filterNodes.bind(this);
108108
}
109109

110110
onCheck(checked) {
@@ -115,11 +115,12 @@ class FilterExample extends React.Component {
115115
this.setState({ expanded });
116116
}
117117

118-
setFilterText(e) {
118+
onFilterChange(e) {
119119
this.setState({ filterText: e.target.value }, this.filterTree);
120120
}
121121

122122
filterTree() {
123+
// Reset nodes back to unfiltered state
123124
if (!this.state.filterText) {
124125
this.setState(prevState => ({
125126
nodesFiltered: prevState.nodes,
@@ -129,30 +130,48 @@ class FilterExample extends React.Component {
129130
}
130131

131132
const nodesFiltered = prevState => (
132-
{ nodesFiltered: prevState.nodes.map(this.nodeFilter).filter(n => n !== null) }
133+
{ nodesFiltered: prevState.nodes.reduce(this.filterNodes, []) }
133134
);
134135

135136
this.setState(nodesFiltered);
136137
}
137138

138-
nodeFilter(node) {
139-
const children = (node.children || []).map(this.nodeFilter).filter(c => c !== null);
139+
filterNodes(filtered, node) {
140+
const { filterText } = this.state;
141+
const children = (node.children || []).reduce(this.filterNodes, []);
142+
143+
if (
144+
// Node's label matches the search string
145+
node.label.toLocaleLowerCase().indexOf(filterText.toLocaleLowerCase()) > -1 ||
146+
// Or a children has a matching node
147+
children.length
148+
) {
149+
filtered.push({ ...node, children });
150+
}
140151

141-
return node.label.indexOf(this.state.filterText) !== -1 || children.length ?
142-
{ ...node, children } :
143-
null;
152+
return filtered;
144153
}
145154

146155
render() {
147-
const { checked, expanded } = this.state;
156+
const {
157+
checked,
158+
expanded,
159+
filterText,
160+
nodesFiltered,
161+
} = this.state;
148162

149163
return (
150-
<div>
151-
<input type='text' placeholder='Search' value={this.state.filterText} onChange={this.setFilterText} />
164+
<div className="filter-container">
165+
<input
166+
type="text"
167+
placeholder="Search"
168+
value={filterText}
169+
onChange={this.onFilterChange}
170+
/>
152171
<CheckboxTree
153172
checked={checked}
154173
expanded={expanded}
155-
nodes={this.state.nodesFiltered}
174+
nodes={nodesFiltered}
156175
onCheck={this.onCheck}
157176
onExpand={this.onExpand}
158177
/>

0 commit comments

Comments
 (0)