Skip to content

Commit 208b59b

Browse files
committed
Update example to include search functionality example
1 parent e18168a commit 208b59b

File tree

1 file changed

+74
-2
lines changed
  • src/examples/basicExample

1 file changed

+74
-2
lines changed

src/examples/basicExample/app.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class App extends Component {
1515
const renderDepthTitle = ({ path }) => `Depth: ${path.length}`;
1616

1717
this.state = {
18+
searchString: '',
19+
searchFocusIndex: 0,
20+
searchFoundCount: null,
1821
treeData: [
1922
{
2023
title: '`title`',
@@ -155,6 +158,13 @@ class App extends Component {
155158
const authorUrl = 'https://github.com/fritz-c';
156159
const githubUrl = 'https://github.com/fritz-c/react-sortable-tree';
157160

161+
const {
162+
treeData,
163+
searchString,
164+
searchFocusIndex,
165+
searchFoundCount,
166+
} = this.state;
167+
158168
const alertNodeInfo = ({
159169
node,
160170
path,
@@ -173,6 +183,18 @@ class App extends Component {
173183
);
174184
};
175185

186+
const selectPrevMatch = () => this.setState({
187+
searchFocusIndex: searchFocusIndex !== null ?
188+
((searchFoundCount + searchFocusIndex - 1) % searchFoundCount) :
189+
searchFoundCount - 1,
190+
});
191+
192+
const selectNextMatch = () => this.setState({
193+
searchFocusIndex: searchFocusIndex !== null ?
194+
((searchFocusIndex + 1) % searchFoundCount) :
195+
0,
196+
});
197+
176198
return (
177199
<div>
178200
<section className={styles['page-header']}>
@@ -189,7 +211,7 @@ class App extends Component {
189211
</span>
190212
<h3>Demo</h3>
191213

192-
<div style={{ height: 785 }}>
214+
<div style={{ height: 450 }}>
193215
<button onClick={this.expandAll}>
194216
Expand All
195217
</button>
@@ -198,10 +220,60 @@ class App extends Component {
198220
Collapse All
199221
</button>
200222

223+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
224+
<form
225+
style={{ display: 'inline-block' }}
226+
onSubmit={(event) => {
227+
event.preventDefault();
228+
}}
229+
>
230+
<label htmlFor="find-box">
231+
Search:&nbsp;
232+
233+
<input
234+
id="find-box"
235+
type="text"
236+
value={searchString}
237+
onChange={event => this.setState({ searchString: event.target.value })}
238+
/>
239+
</label>
240+
241+
<button
242+
type="button"
243+
disabled={!searchFoundCount}
244+
onClick={selectPrevMatch}
245+
>
246+
&lt;
247+
</button>
248+
249+
<button
250+
type="submit"
251+
disabled={!searchFoundCount}
252+
onClick={selectNextMatch}
253+
>
254+
&gt;
255+
</button>
256+
257+
<span>
258+
&nbsp;
259+
{searchFoundCount > 0 ? (searchFocusIndex + 1) : 0}
260+
&nbsp;/&nbsp;
261+
{searchFoundCount || 0}
262+
</span>
263+
</form>
264+
201265
<SortableTree
202-
treeData={this.state.treeData}
266+
treeData={treeData}
203267
updateTreeData={this.updateTreeData}
204268
maxDepth={5}
269+
searchQuery={searchString}
270+
searchFocusOffset={searchFocusIndex}
271+
searchFinishCallback={matches =>
272+
this.setState({
273+
searchFoundCount: matches.length,
274+
searchFocusIndex: matches.length > 0 ? searchFocusIndex % matches.length : 0,
275+
})
276+
}
205277
generateNodeProps={rowInfo => ({
206278
buttons: [
207279
<button

0 commit comments

Comments
 (0)