Skip to content

Commit 4634d99

Browse files
committed
Static search + throttle
1 parent e09ac0a commit 4634d99

2 files changed

Lines changed: 55 additions & 29 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ Class of the Component (in addition of `search-input`).
8282

8383
Function called when the search term is changed (will be passed as an argument).
8484

85+
##### filterKeys
86+
87+
Either an `[String]` or a `String`. Will be use by the `filter` method if no argument is passed there.
88+
89+
##### throttle
90+
91+
Reduce call frequency to the `onChange` function (in ms). Default is 200.
92+
8593
### Methods
8694

8795
##### filter([keys])
@@ -90,6 +98,14 @@ Return a function which can be used to filter an array. `keys` can be `String`,
9098

9199
If an array `keys` is an array, the function will return true if at least one of the keys of the item matches the serch term.
92100

101+
### Static Methods
102+
103+
##### filter(searchTerm, [keys])
104+
105+
Return a function which can be used to filter an array. `searchTerm` can be a `regex` or a `String`. `keys` can be `String`, `[String]` or `null`.
106+
107+
If an array `keys` is an array, the function will return true if at least one of the keys of the item matches the serch term.
108+
93109
## Styles
94110

95111
Look at [react-search-input.css](https://github.com/mathieudutour/react-search-input/blob/master/react-search-input.css) for an idea on how to style this component.

react-search-input.js

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,56 @@
3939
this.setState({
4040
searchTerm: searchTerm
4141
}, function() {
42-
if (this.props.onChange) {
43-
this.props.onChange(searchTerm);
42+
if (this._throttleTimeout) {
43+
clearTimeout(this._throttleTimeout);
4444
}
45+
this._throttleTimeout = setTimeout(function() {
46+
if (this.props.onChange) {
47+
this.props.onChange(searchTerm);
48+
}
49+
}.bind(this), this.props.throttle);
4550
}.bind(this));
46-
4751
},
4852

4953
filter: function(keys) {
50-
return function(item) {
51-
var term = this.state.searchTerm;
52-
if (term === '') {return true;}
53-
// escape special symbols to ensure `term` is a valid regex
54-
term = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
54+
return Search.filter(this.state.searchTerm,
55+
keys || this.props.filterKeys);
56+
},
5557

56-
var name = item;
58+
statics: {
59+
filter: function(term, keys) {
60+
return function(item) {
61+
if (term === '') {return true;}
62+
// escape special symbols to ensure `term` is a valid regex
63+
term = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
5764

58-
var _getNameForKey = function(_key, _name) {
59-
var keys = _key.split('.');
60-
keys.forEach(function(__key) {
61-
_name = _name[__key];
62-
});
65+
var _getValueForKey = function(key, _item) {
66+
var keys = key.split('.');
67+
var result = _item;
68+
keys.forEach(function(_key) {
69+
result = result && result[_key];
70+
});
6371

64-
return _name.toLowerCase();
65-
};
72+
return result && result.toLowerCase();
73+
};
6674

67-
if (keys) {
68-
if( typeof keys === 'string' ) {
69-
keys = [keys];
70-
}
71-
for (var i = 0; i < keys.length; i++) {
72-
if (_getNameForKey(keys[i], name).search(term) !== -1) {
73-
return true;
75+
if (keys) {
76+
if( typeof keys === 'string' ) {
77+
keys = [keys];
78+
}
79+
for (var i = 0; i < keys.length; i++) {
80+
var value = _getValueForKey(keys[i], item);
81+
if (value && value.search(term) !== -1) {
82+
return true;
83+
}
7484
}
85+
return false;
86+
} else {
87+
var stringValue = item.toLowerCase();
88+
return (stringValue.search(term) !== -1);
7589
}
76-
return false;
77-
} else {
78-
name = name.toLowerCase();
79-
return (name.search(term) !== -1);
80-
}
81-
}.bind(this);
90+
}.bind(this);
91+
}
8292
}
8393
});
8494

0 commit comments

Comments
 (0)