Skip to content

Commit

Permalink
Merge pull request #215 from wesrage/#196
Browse files Browse the repository at this point in the history
Support case-insensitive filtering when matchPos="start"
  • Loading branch information
dcousens committed May 29, 2015
2 parents b251baf + 1767375 commit 46550c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@ You can control how options are filtered with the following properties:

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

Both properties default to `"any"`.
`matchProp` and `matchPos` both default to `"any"`.
`ignoreCase` defaults to `true`.

#### Advanced filters

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

* `filterOption`: `function(Object option, String filter)` returns `Boolean`. Will override `matchPos` and `matchProp` options.
* `filterOptions`: `function(Array options, String filter, Array currentValues)` returns `Array filteredOptions`. Will override `filterOption`, `matchPos` and `matchProp` options.
* `filterOption`: `function(Object option, String filter)` returns `Boolean`. Will override `matchPos`, `matchProp` and `ignoreCase` options.
* `filterOptions`: `function(Array options, String filter, Array currentValues)` returns `Array filteredOptions`. Will override `filterOption`, `matchPos`, `matchProp` and `ignoreCase` options.

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

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


Expand Down
15 changes: 11 additions & 4 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var Select = React.createClass({
filterOptions: React.PropTypes.func, // method to filter the options array: function([options], filterString, [values])
matchPos: React.PropTypes.string, // (any|start) match the start or entire string when filtering
matchProp: React.PropTypes.string, // (any|label|value) which option property to filter on
ignoreCase: React.PropTypes.bool, // whether to perform case-insensitive filtering
inputProps: React.PropTypes.object, // custom attributes for the Input (in the Select-control) e.g: {'data-foo': 'bar'}
allowCreate: React.PropTypes.bool, // wether to allow creation of new entries
/*
Expand Down Expand Up @@ -65,6 +66,7 @@ var Select = React.createClass({
className: undefined,
matchPos: 'any',
matchProp: 'any',
ignoreCase: true,
inputProps: {},
allowCreate: false,

Expand Down Expand Up @@ -525,12 +527,17 @@ var Select = React.createClass({
if (this.props.multi && exclude.indexOf(op.value) > -1) return false;
if (this.props.filterOption) return this.props.filterOption.call(this, op, filterValue);
var valueTest = String(op.value), labelTest = String(op.label);
if (this.props.ignoreCase) {
valueTest = valueTest.toLowerCase();
labelTest = labelTest.toLowerCase();
filterValue = filterValue.toLowerCase();
}
return !filterValue || (this.props.matchPos === 'start') ? (
(this.props.matchProp !== 'label' && valueTest.toLowerCase().substr(0, filterValue.length) === filterValue) ||
(this.props.matchProp !== 'value' && labelTest.toLowerCase().substr(0, filterValue.length) === filterValue)
(this.props.matchProp !== 'label' && valueTest.substr(0, filterValue.length) === filterValue) ||
(this.props.matchProp !== 'value' && labelTest.substr(0, filterValue.length) === filterValue)
) : (
(this.props.matchProp !== 'label' && valueTest.toLowerCase().indexOf(filterValue.toLowerCase()) >= 0) ||
(this.props.matchProp !== 'value' && labelTest.toLowerCase().indexOf(filterValue.toLowerCase()) >= 0)
(this.props.matchProp !== 'label' && valueTest.indexOf(filterValue) >= 0) ||
(this.props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0)
);
};
return (options || []).filter(filterOption, this);
Expand Down

0 comments on commit 46550c9

Please sign in to comment.