Skip to content

Commit ee4a9f4

Browse files
committed
allow to search in an array
1 parent 7c71f2a commit ee4a9f4

4 files changed

Lines changed: 78 additions & 15 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.0",
7+
"version": "0.2.1",
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: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,57 @@
5151
job: 'Software Engineer',
5252
company: 'Enki'
5353
},
54-
subject: 'Hi!'
54+
subject: 'Hi!',
55+
dest: [
56+
{
57+
name: 'Bruno',
58+
job: 'CTO',
59+
company: 'Enki'
60+
},
61+
{
62+
name: 'Arseny',
63+
job: 'Software Engineer',
64+
company: 'Enki'
65+
}
66+
]
5567
}, {
5668
user: {
5769
name: 'foo'
5870
},
59-
subject: 'bar'
71+
subject: 'bar',
72+
dest: [
73+
{
74+
name: 'Bruno',
75+
job: 'CTO',
76+
company: 'Enki'
77+
},
78+
{
79+
name: 'Arseny',
80+
job: 'Software Engineer',
81+
company: 'Enki'
82+
}
83+
]
6084
}, {
6185
user: {
6286
name: 'bar'
6387
},
64-
subject: 'foo'
88+
subject: 'foo',
89+
dest: [
90+
{
91+
name: 'Bruno',
92+
job: 'CTO',
93+
company: 'Enki'
94+
},
95+
{
96+
name: 'Arseny',
97+
job: 'Software Engineer',
98+
company: 'Enki'
99+
}
100+
]
65101
}];
66102

67103
if (this.refs.search) {
68-
var filters = ['user.name', 'subject'];
104+
var filters = ['user.name', 'subject', 'dest.name'];
69105
mails = mails.filter(this.refs.search.filter(filters));
70106
}
71107

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.0",
3+
"version": "0.2.1",
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: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,57 @@
6262
// escape special symbols to ensure `term` is a valid regex
6363
term = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
6464

65-
var _getValueForKey = function(key, _item) {
65+
var _getValuesForKey = function(key, _item) {
6666
var keys = key.split('.');
67-
var result = _item;
67+
var results = [_item];
6868
keys.forEach(function(_key) {
69-
result = result && result[_key];
69+
var tmp = [];
70+
results.forEach(function(result) {
71+
if (result) {
72+
if(result instanceof Array) {
73+
result.forEach(function(res) {
74+
tmp.push(res[_key]);
75+
});
76+
} else {
77+
tmp.push(result[_key]);
78+
}
79+
}
80+
});
81+
results = tmp;
82+
});
83+
return results.map(function(result) {
84+
if (typeof result === 'string') {
85+
return result.toLowerCase();
86+
} else {
87+
return null;
88+
}
7089
});
71-
72-
return result && result.toLowerCase();
7390
};
7491

7592
if (keys) {
7693
if( typeof keys === 'string' ) {
7794
keys = [keys];
7895
}
7996
for (var i = 0; i < keys.length; i++) {
80-
var value = _getValueForKey(keys[i], item);
81-
if (value && value.search(term) !== -1) {
97+
var values = _getValuesForKey(keys[i], item);
98+
var found = false;
99+
values.forEach(function(value) {
100+
if (value && value.search(term) !== -1) {
101+
found = true;
102+
}
103+
});
104+
if (found) {
82105
return true;
83106
}
84107
}
85108
return false;
86109
} else {
87-
var stringValue = item.toLowerCase();
88-
return (stringValue.search(term) !== -1);
110+
if( typeof item === 'string' ) {
111+
var stringValue = item.toLowerCase();
112+
return (stringValue.search(term) !== -1);
113+
} else {
114+
return false;
115+
}
89116
}
90117
}.bind(this);
91118
}

0 commit comments

Comments
 (0)