Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim search text from beginning and the end. (fixes #1861) #1862

Merged
merged 3 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -870,6 +870,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 @@ -1116,6 +1117,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 @@ -1162,6 +1164,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 @@ -3500,6 +3500,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