Skip to content

Commit 46550c9

Browse files
committed
Merge pull request #215 from wesrage/#196
Support case-insensitive filtering when matchPos="start"
2 parents b251baf + 1767375 commit 46550c9

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,17 @@ You can control how options are filtered with the following properties:
123123

124124
* `matchPos`: `"start"` or `"any"`: whether to match the text entered at the start or any position in the option value
125125
* `matchProp`: `"label"`, `"value"` or `"any"`: whether to match the value, label or both values of each option when filtering
126+
* `ignoreCase`: `Boolean`: whether to ignore case or match the text exactly when filtering
126127

127-
Both properties default to `"any"`.
128+
`matchProp` and `matchPos` both default to `"any"`.
129+
`ignoreCase` defaults to `true`.
128130

129131
#### Advanced filters
130132

131133
You can also completely replace the method used to filter either a single option, or the entire options array (allowing custom sort mechanisms, etc.)
132134

133-
* `filterOption`: `function(Object option, String filter)` returns `Boolean`. Will override `matchPos` and `matchProp` options.
134-
* `filterOptions`: `function(Array options, String filter, Array currentValues)` returns `Array filteredOptions`. Will override `filterOption`, `matchPos` and `matchProp` options.
135+
* `filterOption`: `function(Object option, String filter)` returns `Boolean`. Will override `matchPos`, `matchProp` and `ignoreCase` options.
136+
* `filterOptions`: `function(Array options, String filter, Array currentValues)` returns `Array filteredOptions`. Will override `filterOption`, `matchPos`, `matchProp` and `ignoreCase` options.
135137

136138
For multi-select inputs, when providing a custom `filterOptions` method, remember to exclude current values from the returned array of options.
137139

@@ -163,6 +165,7 @@ For multi-select inputs, when providing a custom `filterOptions` method, remembe
163165
filterOptions | func | method to filter the options array: function([options], filterString, [values])
164166
matchPos | string | (any, start) match the start or entire string when filtering
165167
matchProp | string | (any, label, value) which option property to filter on
168+
ignoreCase | bool | whether to perform case-insensitive filtering
166169
inputProps | object | custom attributes for the Input (in the Select-control) e.g: {'data-foo': 'bar'}
167170

168171

src/Select.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var Select = React.createClass({
3333
filterOptions: React.PropTypes.func, // method to filter the options array: function([options], filterString, [values])
3434
matchPos: React.PropTypes.string, // (any|start) match the start or entire string when filtering
3535
matchProp: React.PropTypes.string, // (any|label|value) which option property to filter on
36+
ignoreCase: React.PropTypes.bool, // whether to perform case-insensitive filtering
3637
inputProps: React.PropTypes.object, // custom attributes for the Input (in the Select-control) e.g: {'data-foo': 'bar'}
3738
allowCreate: React.PropTypes.bool, // wether to allow creation of new entries
3839
/*
@@ -65,6 +66,7 @@ var Select = React.createClass({
6566
className: undefined,
6667
matchPos: 'any',
6768
matchProp: 'any',
69+
ignoreCase: true,
6870
inputProps: {},
6971
allowCreate: false,
7072

@@ -525,12 +527,17 @@ var Select = React.createClass({
525527
if (this.props.multi && exclude.indexOf(op.value) > -1) return false;
526528
if (this.props.filterOption) return this.props.filterOption.call(this, op, filterValue);
527529
var valueTest = String(op.value), labelTest = String(op.label);
530+
if (this.props.ignoreCase) {
531+
valueTest = valueTest.toLowerCase();
532+
labelTest = labelTest.toLowerCase();
533+
filterValue = filterValue.toLowerCase();
534+
}
528535
return !filterValue || (this.props.matchPos === 'start') ? (
529-
(this.props.matchProp !== 'label' && valueTest.toLowerCase().substr(0, filterValue.length) === filterValue) ||
530-
(this.props.matchProp !== 'value' && labelTest.toLowerCase().substr(0, filterValue.length) === filterValue)
536+
(this.props.matchProp !== 'label' && valueTest.substr(0, filterValue.length) === filterValue) ||
537+
(this.props.matchProp !== 'value' && labelTest.substr(0, filterValue.length) === filterValue)
531538
) : (
532-
(this.props.matchProp !== 'label' && valueTest.toLowerCase().indexOf(filterValue.toLowerCase()) >= 0) ||
533-
(this.props.matchProp !== 'value' && labelTest.toLowerCase().indexOf(filterValue.toLowerCase()) >= 0)
539+
(this.props.matchProp !== 'label' && valueTest.indexOf(filterValue) >= 0) ||
540+
(this.props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0)
534541
);
535542
};
536543
return (options || []).filter(filterOption, this);

0 commit comments

Comments
 (0)