|
1 | | -;(function (root, factory) { |
2 | | - if (typeof module !== "undefined" && module.exports) { |
3 | | - module.exports = factory(require("react")); |
4 | | - } else if (typeof define === "function" && define.amd) { |
5 | | - define(["react"], factory); |
| 1 | +;(function(root, factory) { |
| 2 | + if (typeof module !== 'undefined' && module.exports) { |
| 3 | + module.exports = factory(require('react')); |
| 4 | + } else if (typeof define === 'function' && define.amd) { |
| 5 | + define(['react'], factory); |
6 | 6 | } else { |
7 | 7 | root.SearchInput = factory(root.React); |
8 | 8 | } |
9 | 9 | })(this, function (React) { |
10 | | - "use strict"; |
| 10 | + 'use strict'; |
11 | 11 |
|
12 | 12 | var Search = React.createClass({ |
13 | 13 | propTypes: { |
|
28 | 28 | onChange: function() {}, |
29 | 29 | caseSensitive: false, |
30 | 30 | throttle: 200 |
31 | | - } |
| 31 | + }; |
32 | 32 | }, |
33 | 33 |
|
34 | 34 | getInitialState: function() { |
|
39 | 39 |
|
40 | 40 | render: function() { |
41 | 41 | return ( |
42 | | - React.createElement("div", {className: "search-input " + this.props.className}, |
43 | | - React.createElement("div", {className: "search-wrapper"}, |
44 | | - React.createElement("span", {className: "search-icon"}, String.fromCharCode(9906)), |
45 | | - React.createElement("input", {type: "search", value: this.state.searchTerm, |
46 | | - onChange: this.updateSearch, className: "search-field", |
47 | | - placeholder: "Search"}) |
| 42 | + React.createElement('div', {className: 'search-input ' + this.props.className}, |
| 43 | + React.createElement('div', {className: 'search-wrapper'}, |
| 44 | + React.createElement('span', {className: 'search-icon'}, String.fromCharCode(9906)), |
| 45 | + React.createElement('input', {type: 'search', value: this.state.searchTerm, |
| 46 | + onChange: this.updateSearch, className: 'search-field', |
| 47 | + placeholder: 'Search'}) |
48 | 48 | ) |
49 | 49 | ) |
50 | 50 | ); |
|
58 | 58 | if (this._throttleTimeout) { |
59 | 59 | clearTimeout(this._throttleTimeout); |
60 | 60 | } |
| 61 | + |
61 | 62 | this._throttleTimeout = setTimeout(this.props.onChange, this.props.throttle, searchTerm); |
62 | 63 | }.bind(this)); |
63 | 64 | }, |
|
71 | 72 | filter: function(term, keys, caseSensitive) { |
72 | 73 | return function(item) { |
73 | 74 | if (term === '') {return true;} |
74 | | - // escape special symbols to ensure `term` is a valid regex |
75 | | - //var escapedTerm = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); |
76 | 75 |
|
77 | 76 | if (!caseSensitive) { |
78 | 77 | term = term.toLowerCase(); |
79 | 78 | } |
80 | 79 |
|
81 | 80 | var terms = term.split(' '); |
82 | 81 | var foundCount = 0; |
83 | | - terms.forEach(function(term) { |
84 | | - if (keys) { |
85 | | - if (typeof keys === 'string') { |
86 | | - keys = [keys]; |
| 82 | + if (keys) { |
| 83 | + if (typeof keys === 'string') { |
| 84 | + keys = [keys]; |
| 85 | + } |
| 86 | + |
| 87 | + terms.forEach(function(term) { |
| 88 | + |
| 89 | + // allow search in specific fields with the syntax `field:search` |
| 90 | + var currentKeys; |
| 91 | + if (term.indexOf(':') > -1) { |
| 92 | + var searchedField = term.split(':')[0]; |
| 93 | + term = term.split(':')[1]; |
| 94 | + currentKeys = keys.filter(function(key) { |
| 95 | + return key.indexOf(searchedField) > -1; |
| 96 | + }); |
| 97 | + } else { |
| 98 | + currentKeys = keys; |
87 | 99 | } |
| 100 | + |
88 | 101 | var found = false; |
89 | | - for (var i = 0; i < keys.length; i++) { |
| 102 | + for (var i = 0; i < currentKeys.length; i++) { |
90 | 103 | var values = _getValuesForKey(keys[i], item); |
91 | 104 | values.forEach(function(value) { |
92 | 105 | try { |
93 | 106 | if (value && value.search(term) !== -1) { |
94 | 107 | found = true; |
95 | 108 | } |
96 | | - } catch(e) {} |
| 109 | + } catch (e) {} |
97 | 110 | }); |
| 111 | + |
98 | 112 | if (found) { |
99 | 113 | break; |
100 | 114 | } |
|
103 | 117 | if (found) { |
104 | 118 | foundCount++; |
105 | 119 | } |
106 | | - } else { |
| 120 | + }); |
| 121 | + } else { |
| 122 | + terms.forEach(function(term) { |
107 | 123 | try { |
108 | 124 | var stringValue = item.toLowerCase(); |
109 | 125 | if (stringValue.search(term) !== -1) { |
110 | 126 | foundCount++; |
111 | 127 | } |
112 | | - } catch(e) {} |
113 | | - } |
114 | | - }); |
115 | | - return foundCount === terms.length;; |
| 128 | + } catch (e) {} |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + return foundCount === terms.length; |
116 | 133 | }; |
117 | 134 | } |
118 | 135 | } |
|
125 | 142 | var tmp = []; |
126 | 143 | results.forEach(function(result) { |
127 | 144 | if (result) { |
128 | | - if(result instanceof Array) { |
| 145 | + if (result instanceof Array) { |
129 | 146 | result.forEach(function(res) { |
130 | 147 | tmp.push(res[_key]); |
131 | 148 | }); |
|
134 | 151 | } |
135 | 152 | } |
136 | 153 | }); |
| 154 | + |
137 | 155 | results = tmp; |
138 | 156 | }); |
| 157 | + |
139 | 158 | return results.map(function(result) { |
140 | 159 | if (typeof result === 'string') { |
141 | 160 | return result.toLowerCase(); |
|
0 commit comments