|
39 | 39 | this.setState({ |
40 | 40 | searchTerm: searchTerm |
41 | 41 | }, function() { |
42 | | - if (this.props.onChange) { |
43 | | - this.props.onChange(searchTerm); |
| 42 | + if (this._throttleTimeout) { |
| 43 | + clearTimeout(this._throttleTimeout); |
44 | 44 | } |
| 45 | + this._throttleTimeout = setTimeout(function() { |
| 46 | + if (this.props.onChange) { |
| 47 | + this.props.onChange(searchTerm); |
| 48 | + } |
| 49 | + }.bind(this), this.props.throttle); |
45 | 50 | }.bind(this)); |
46 | | - |
47 | 51 | }, |
48 | 52 |
|
49 | 53 | 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 | + }, |
55 | 57 |
|
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, "\\$&"); |
57 | 64 |
|
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 | + }); |
63 | 71 |
|
64 | | - return _name.toLowerCase(); |
65 | | - }; |
| 72 | + return result && result.toLowerCase(); |
| 73 | + }; |
66 | 74 |
|
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 | + } |
74 | 84 | } |
| 85 | + return false; |
| 86 | + } else { |
| 87 | + var stringValue = item.toLowerCase(); |
| 88 | + return (stringValue.search(term) !== -1); |
75 | 89 | } |
76 | | - return false; |
77 | | - } else { |
78 | | - name = name.toLowerCase(); |
79 | | - return (name.search(term) !== -1); |
80 | | - } |
81 | | - }.bind(this); |
| 90 | + }.bind(this); |
| 91 | + } |
82 | 92 | } |
83 | 93 | }); |
84 | 94 |
|
|
0 commit comments