Skip to content

Commit

Permalink
Trim search text from beginning and the end. (fixes #1861)
Browse files Browse the repository at this point in the history
  • Loading branch information
serkanozer committed Jul 6, 2017
1 parent 6c0ee59 commit d6f5f61
Show file tree
Hide file tree
Showing 8 changed files with 24,238 additions and 23,472 deletions.
13,537 changes: 6,780 additions & 6,757 deletions examples/dist/app.js

Large diffs are not rendered by default.

3,618 changes: 1,940 additions & 1,678 deletions examples/dist/bundle.js

Large diffs are not rendered by default.

27,246 changes: 13,705 additions & 13,541 deletions examples/dist/common.js

Large diffs are not rendered by default.

3,253 changes: 1,757 additions & 1,496 deletions examples/dist/standalone.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ class Select extends React.Component {
matchPos: this.props.matchPos,
matchProp: this.props.matchProp,
valueKey: this.props.valueKey,
trimFilter: this.props.trimFilter
}
);
} else {
Expand Down Expand Up @@ -1107,6 +1108,7 @@ Select.propTypes = {
style: PropTypes.object, // optional style to apply to the control
tabIndex: PropTypes.string, // optional tab index of the control
tabSelectsValue: PropTypes.bool, // whether to treat tabbing out while focused to be value selection
trimFilter: PropTypes.bool, // whether to trim whitespace around filter value
value: PropTypes.any, // initial field value
valueComponent: PropTypes.func, // value component to render
valueKey: PropTypes.string, // path of the label value in option objects
Expand Down Expand Up @@ -1151,6 +1153,7 @@ Select.defaultProps = {
searchable: true,
simpleValue: false,
tabSelectsValue: true,
trimFilter: true,
valueComponent: Value,
valueKey: 'value',
};
Expand Down
7 changes: 7 additions & 0 deletions src/utils/defaultFilterOptions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import stripDiacritics from './stripDiacritics';
import trim from './trim';

function filterOptions (options, filterValue, excludeOptions, props) {
if (props.ignoreAccents) {
Expand All @@ -9,6 +10,10 @@ function filterOptions (options, filterValue, excludeOptions, props) {
filterValue = filterValue.toLowerCase();
}

if (props.trimFilter) {
filterValue = trim(filterValue);
}

if (excludeOptions) excludeOptions = excludeOptions.map(i => i[props.valueKey]);

return options.filter(option => {
Expand All @@ -17,10 +22,12 @@ function filterOptions (options, filterValue, excludeOptions, props) {
if (!filterValue) return true;
var valueTest = String(option[props.valueKey]);
var labelTest = String(option[props.labelKey]);

if (props.ignoreAccents) {
if (props.matchProp !== 'label') valueTest = stripDiacritics(valueTest);
if (props.matchProp !== 'value') labelTest = stripDiacritics(labelTest);
}

if (props.ignoreCase) {
if (props.matchProp !== 'label') valueTest = valueTest.toLowerCase();
if (props.matchProp !== 'value') labelTest = labelTest.toLowerCase();
Expand Down
10 changes: 10 additions & 0 deletions src/utils/trim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function trim(str) {
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
};
36 changes: 36 additions & 0 deletions test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3478,6 +3478,42 @@ describe('Select', () => {
});
});

describe('with trimFilter=true', () => {
beforeEach(() => {

instance = createControl({
searchable: true,
trimFilter: true,
options: defaultOptions
});
});

it('finds options for a filter wrapped around with whitespaces', () => {

typeSearchText(' def ');
var options = ReactDOM.findDOMNode(instance).querySelectorAll('.Select-option');
expect(options[0], 'to have text', 'AbcDef');
expect(options, 'to have length', 1);
});
});

describe('with trimFilter=false', () => {
beforeEach(() => {

instance = createControl({
searchable: true,
trimFilter: false,
options: defaultOptions
});
});

it('finds options for a filter wrapped around with whitespaces', () => {

typeSearchText(' def ');
expect(ReactDOM.findDOMNode(instance), 'to contain no elements matching', '.Select-option');
});
});

describe('valueRenderer', () => {

var valueRenderer;
Expand Down

0 comments on commit d6f5f61

Please sign in to comment.