Skip to content

Commit 5d4c4a6

Browse files
committed
allow to search in specific field using the syntax
1 parent 32d97aa commit 5d4c4a6

4 files changed

Lines changed: 54 additions & 35 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"react-search-input.js",
55
"react-search-input.css"
66
],
7-
"version": "0.2.5",
7+
"version": "0.2.6",
88
"homepage": "https://github.com/enki-com/react-search-input",
99
"description": "Simple react.js component for a search input, providing a filter function.",
1010
"keywords": [

example/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
render() {
4848
var mails = [{
4949
user: {
50-
name: 'java',
50+
name: 'Mathieu',
5151
job: 'Software Engineer',
5252
company: 'Enki'
5353
},
@@ -66,12 +66,12 @@
6666
]
6767
}, {
6868
user: {
69-
name: 'javascript'
69+
name: 'Bruno'
7070
},
71-
subject: 'bar',
71+
subject: 'javascript',
7272
dest: [
7373
{
74-
name: 'Bruno',
74+
name: 'Mathieu',
7575
job: 'CTO',
7676
company: 'Enki'
7777
},
@@ -83,9 +83,9 @@
8383
]
8484
}, {
8585
user: {
86-
name: 'bar'
86+
name: 'you can search for me using a regex : `java$`'
8787
},
88-
subject: 'foo',
88+
subject: 'java',
8989
dest: [
9090
{
9191
name: 'Bruno',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-search-input",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "Simple react.js component for a search input, providing a filter function.",
55
"main": "react-search-input.js",
66
"dependencies": {

react-search-input.js

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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);
66
} else {
77
root.SearchInput = factory(root.React);
88
}
99
})(this, function (React) {
10-
"use strict";
10+
'use strict';
1111

1212
var Search = React.createClass({
1313
propTypes: {
@@ -28,7 +28,7 @@
2828
onChange: function() {},
2929
caseSensitive: false,
3030
throttle: 200
31-
}
31+
};
3232
},
3333

3434
getInitialState: function() {
@@ -39,12 +39,12 @@
3939

4040
render: function() {
4141
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'})
4848
)
4949
)
5050
);
@@ -58,6 +58,7 @@
5858
if (this._throttleTimeout) {
5959
clearTimeout(this._throttleTimeout);
6060
}
61+
6162
this._throttleTimeout = setTimeout(this.props.onChange, this.props.throttle, searchTerm);
6263
}.bind(this));
6364
},
@@ -71,30 +72,43 @@
7172
filter: function(term, keys, caseSensitive) {
7273
return function(item) {
7374
if (term === '') {return true;}
74-
// escape special symbols to ensure `term` is a valid regex
75-
//var escapedTerm = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
7675

7776
if (!caseSensitive) {
7877
term = term.toLowerCase();
7978
}
8079

8180
var terms = term.split(' ');
8281
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;
8799
}
100+
88101
var found = false;
89-
for (var i = 0; i < keys.length; i++) {
102+
for (var i = 0; i < currentKeys.length; i++) {
90103
var values = _getValuesForKey(keys[i], item);
91104
values.forEach(function(value) {
92105
try {
93106
if (value && value.search(term) !== -1) {
94107
found = true;
95108
}
96-
} catch(e) {}
109+
} catch (e) {}
97110
});
111+
98112
if (found) {
99113
break;
100114
}
@@ -103,16 +117,19 @@
103117
if (found) {
104118
foundCount++;
105119
}
106-
} else {
120+
});
121+
} else {
122+
terms.forEach(function(term) {
107123
try {
108124
var stringValue = item.toLowerCase();
109125
if (stringValue.search(term) !== -1) {
110126
foundCount++;
111127
}
112-
} catch(e) {}
113-
}
114-
});
115-
return foundCount === terms.length;;
128+
} catch (e) {}
129+
});
130+
}
131+
132+
return foundCount === terms.length;
116133
};
117134
}
118135
}
@@ -125,7 +142,7 @@
125142
var tmp = [];
126143
results.forEach(function(result) {
127144
if (result) {
128-
if(result instanceof Array) {
145+
if (result instanceof Array) {
129146
result.forEach(function(res) {
130147
tmp.push(res[_key]);
131148
});
@@ -134,8 +151,10 @@
134151
}
135152
}
136153
});
154+
137155
results = tmp;
138156
});
157+
139158
return results.map(function(result) {
140159
if (typeof result === 'string') {
141160
return result.toLowerCase();

0 commit comments

Comments
 (0)