UNPKG

react-select-v1

Version:

A Select control built with and for ReactJS

69 lines (50 loc) 2.38 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _stripDiacritics = require('./stripDiacritics'); var _stripDiacritics2 = _interopRequireDefault(_stripDiacritics); var _trim = require('./trim'); var _trim2 = _interopRequireDefault(_trim); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var isValid = function isValid(value) { return typeof value !== 'undefined' && value !== null && value !== ''; }; var filterOptions = function filterOptions(options, filterValue, excludeOptions, props) { if (props.ignoreAccents) { filterValue = (0, _stripDiacritics2.default)(filterValue); } if (props.ignoreCase) { filterValue = filterValue.toLowerCase(); } if (props.trimFilter) { filterValue = (0, _trim2.default)(filterValue); } if (excludeOptions) excludeOptions = excludeOptions.map(function (i) { return i[props.valueKey]; }); return options.filter(function (option) { if (excludeOptions && excludeOptions.indexOf(option[props.valueKey]) > -1) return false; if (props.filterOption) return props.filterOption.call(undefined, option, filterValue); if (!filterValue) return true; var value = option[props.valueKey]; var label = option[props.labelKey]; var hasValue = isValid(value); var hasLabel = isValid(label); if (!hasValue && !hasLabel) { return false; } var valueTest = hasValue ? String(value) : null; var labelTest = hasLabel ? String(label) : null; if (props.ignoreAccents) { if (valueTest && props.matchProp !== 'label') valueTest = (0, _stripDiacritics2.default)(valueTest); if (labelTest && props.matchProp !== 'value') labelTest = (0, _stripDiacritics2.default)(labelTest); } if (props.ignoreCase) { if (valueTest && props.matchProp !== 'label') valueTest = valueTest.toLowerCase(); if (labelTest && props.matchProp !== 'value') labelTest = labelTest.toLowerCase(); } return props.matchPos === 'start' ? valueTest && props.matchProp !== 'label' && valueTest.substr(0, filterValue.length) === filterValue || labelTest && props.matchProp !== 'value' && labelTest.substr(0, filterValue.length) === filterValue : valueTest && props.matchProp !== 'label' && valueTest.indexOf(filterValue) >= 0 || labelTest && props.matchProp !== 'value' && labelTest.indexOf(filterValue) >= 0; }); }; exports.default = filterOptions;