|
1 | 1 | import React, { PureComponent } from 'react';
|
2 | 2 | import Tree from './Tree';
|
3 | 3 | import Preview from './Preview';
|
| 4 | +import debounce from 'lodash.debounce'; |
4 | 5 |
|
5 | 6 | class App extends PureComponent {
|
6 | 7 | state = {
|
7 |
| - node: null |
| 8 | + node: null, |
| 9 | + filterText: '', |
| 10 | + caseSensitive: false, |
| 11 | + exactMatch: false, |
| 12 | + includeAncestors: true, |
| 13 | + includeDescendants: true |
8 | 14 | };
|
9 | 15 |
|
| 16 | + textFilter = null; |
| 17 | + tree = null; |
| 18 | + |
| 19 | + changeCheckedState = (key) => (event) => { |
| 20 | + const checked = event.target.checked; |
| 21 | + |
| 22 | + this.setState({ |
| 23 | + [key]: checked |
| 24 | + }, () => { |
| 25 | + this.filter(); |
| 26 | + }); |
| 27 | + }; |
10 | 28 | onUpdate = (node) => {
|
11 | 29 | this.setState({ node: node });
|
12 | 30 | };
|
| 31 | + filter = (keyword) => { |
| 32 | + if (!this.tree) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + keyword = keyword || this.textFilter.value || ''; |
| 37 | + |
| 38 | + if (!keyword) { |
| 39 | + this.tree.unfilter(); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const { |
| 44 | + caseSensitive, |
| 45 | + exactMatch, |
| 46 | + includeAncestors, |
| 47 | + includeDescendants |
| 48 | + } = this.state; |
| 49 | + |
| 50 | + this.tree.filter(keyword, { |
| 51 | + filterPath: 'name', |
| 52 | + caseSensitive: caseSensitive, |
| 53 | + exactMatch: exactMatch, |
| 54 | + includeAncestors: includeAncestors, |
| 55 | + includeDescendants: includeDescendants |
| 56 | + }); |
| 57 | + }; |
13 | 58 |
|
14 | 59 | render() {
|
15 | 60 | return (
|
16 | 61 | <div className="container-fluid">
|
17 | 62 | <div className="row">
|
18 | 63 | <div className="col-xs-6">
|
19 |
| - <Tree onUpdate={this.onUpdate} /> |
| 64 | + <h4>Filter</h4> |
| 65 | + <input |
| 66 | + ref={node => { |
| 67 | + this.textFilter = node; |
| 68 | + }} |
| 69 | + type="text" |
| 70 | + className="form-control" |
| 71 | + name="text-filter" |
| 72 | + placeholder="Type to filter by text" |
| 73 | + onKeyUp={(event) => { |
| 74 | + event.persist(); |
| 75 | + |
| 76 | + const { keyCode } = event; |
| 77 | + const BACKSPACE = 8; |
| 78 | + const DELETE = 46; |
| 79 | + const ENTER = 13; |
| 80 | + |
| 81 | + if ([BACKSPACE, DELETE, ENTER].includes(keyCode)) { |
| 82 | + this.filter(); |
| 83 | + } |
| 84 | + }} |
| 85 | + onKeyPress={debounce((event) => { |
| 86 | + this.filter(); |
| 87 | + }, 250)} |
| 88 | + /> |
| 89 | + <div className="row"> |
| 90 | + <div className="col-xs-6"> |
| 91 | + <div className="checkbox" style={{ margin: '5px 0' }}> |
| 92 | + <label> |
| 93 | + <input |
| 94 | + type="checkbox" |
| 95 | + name="case-sensitive" |
| 96 | + checked={this.state.caseSensitive} |
| 97 | + onChange={this.changeCheckedState('caseSensitive')} |
| 98 | + /> |
| 99 | + Case-sensitive |
| 100 | + </label> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + <div className="col-xs-6"> |
| 104 | + <div className="checkbox" style={{ margin: '5px 0' }}> |
| 105 | + <label> |
| 106 | + <input |
| 107 | + type="checkbox" |
| 108 | + name="exact-match" |
| 109 | + checked={this.state.exactMatch} |
| 110 | + onChange={this.changeCheckedState('exactMatch')} |
| 111 | + /> |
| 112 | + Exact match |
| 113 | + </label> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + <div className="col-xs-6"> |
| 117 | + <div className="checkbox" style={{ margin: '5px 0' }}> |
| 118 | + <label> |
| 119 | + <input |
| 120 | + type="checkbox" |
| 121 | + name="include-ancestors" |
| 122 | + checked={this.state.includeAncestors} |
| 123 | + onChange={this.changeCheckedState('includeAncestors')} |
| 124 | + /> |
| 125 | + Include ancestors |
| 126 | + </label> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + <div className="col-xs-6"> |
| 130 | + <div className="checkbox" style={{ margin: '5px 0' }}> |
| 131 | + <label> |
| 132 | + <input |
| 133 | + type="checkbox" |
| 134 | + name="include-descendants" |
| 135 | + checked={this.state.includeDescendants} |
| 136 | + onChange={this.changeCheckedState('includeDescendants')} |
| 137 | + /> |
| 138 | + Include descendants |
| 139 | + </label> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + <div className="row"> |
| 146 | + <div className="col-xs-6"> |
| 147 | + <Tree |
| 148 | + ref={c => { |
| 149 | + this.tree = c ? c.tree : null; |
| 150 | + }} |
| 151 | + onUpdate={this.onUpdate} |
| 152 | + /> |
20 | 153 | </div>
|
21 | 154 | <div className="col-xs-6">
|
22 | 155 | <Preview node={this.state.node} />
|
|
0 commit comments