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

Fixed search for invalid label and/or value #2179

Merged
merged 3 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 21 additions & 10 deletions src/utils/defaultFilterOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,35 @@ function filterOptions (options, filterValue, excludeOptions, props) {
if (excludeOptions && excludeOptions.indexOf(option[props.valueKey]) > -1) return false;
if (props.filterOption) return props.filterOption.call(this, option, filterValue);
if (!filterValue) return true;
var valueTest = String(option[props.valueKey]);
var labelTest = String(option[props.labelKey]);

var value = option[props.valueKey];
var label = option[props.labelKey];
var hasValue = typeof (value) !== 'undefined' && value !== null && value !== '';
var hasLabel = typeof (label) !== 'undefined' && label !== null && label !== '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract this logic into a function?


if (!hasValue && !hasLabel) {
return false;
}

var valueTest = hasValue ? String(value) : null;
var labelTest = hasLabel ? String(label) : null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not include this in the hasX checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because there is situation when only one of label or value is invalid and still need to search in valid field.


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

if (props.ignoreCase) {
if (props.matchProp !== 'label') valueTest = valueTest.toLowerCase();
if (props.matchProp !== 'value') labelTest = labelTest.toLowerCase();
if (valueTest && props.matchProp !== 'label') valueTest = valueTest.toLowerCase();
if (labelTest && props.matchProp !== 'value') labelTest = labelTest.toLowerCase();
}

return props.matchPos === 'start' ? (
(props.matchProp !== 'label' && valueTest.substr(0, filterValue.length) === filterValue) ||
(props.matchProp !== 'value' && labelTest.substr(0, filterValue.length) === filterValue)
(valueTest && props.matchProp !== 'label' && valueTest.substr(0, filterValue.length) === filterValue) ||
(labelTest && props.matchProp !== 'value' && labelTest.substr(0, filterValue.length) === filterValue)
) : (
(props.matchProp !== 'label' && valueTest.indexOf(filterValue) >= 0) ||
(props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0)
(valueTest && props.matchProp !== 'label' && valueTest.indexOf(filterValue) >= 0) ||
(labelTest && props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0)
);
});
}
Expand Down
22 changes: 21 additions & 1 deletion test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,11 @@ describe('Select', () => {
{ value: 20, label: 'Twenty' },
{ value: 21, label: 'Twenty-one' },
{ value: 34, label: 'Thirty-four' },
{ value: 54, label: 'Fifty-four' }
{ value: 54, label: 'Fifty-four' },
{ value: null, label: null },
{ fish: 'salomon', type: 'inedible' },
{ value: 0, type: 'inedible' },
{ label: 0, type: 'inedible' },
];

describe('with matchPos=any and matchProp=any', () => {
Expand Down Expand Up @@ -880,6 +884,22 @@ describe('Select', () => {
expect.it('to have text', 'Fifty-four')
]);
});

it('should not match text when value and/or label are invalid for search', () => {
typeSearchText('ined');
expect(ReactDOM.findDOMNode(instance), 'to contain elements matching',
'.Select-noresults');
expect(ReactDOM.findDOMNode(instance), 'to contain no elements matching',
'.Select-option');
});

it('should not match text when value and/or label are null', () => {
typeSearchText('null');
expect(ReactDOM.findDOMNode(instance), 'to contain elements matching',
'.Select-noresults');
expect(ReactDOM.findDOMNode(instance), 'to contain no elements matching',
'.Select-option');
});
});

describe('with matchPos=start and matchProp=any', () => {
Expand Down