UNPKG

chowa

Version:

UI component library based on React

151 lines (150 loc) 5.35 kB
/** * @license chowa v1.1.3 * * Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn). * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const React = require("react"); const select_option_1 = require("./select-option"); const select_option_group_1 = require("./select-option-group"); const utils_1 = require("../utils"); function transformReactNodeToOptions(children) { const options = []; React.Children.forEach(children, (child) => { if (!utils_1.isReactElement(child) || (child.type !== select_option_1.default && child.type !== select_option_group_1.default)) { return; } if (child.type === select_option_1.default) { options.push(Object.assign({}, child.props)); } if (child.type === select_option_group_1.default) { options.push(Object.assign(Object.assign({}, child.props), { options: transformReactNodeToOptions(child.props.children) })); } }); return options; } exports.transformReactNodeToOptions = transformReactNodeToOptions; function compileValue(value) { if (Array.isArray(value)) { return value; } return value === undefined ? [] : [value]; } exports.compileValue = compileValue; function compileSelectedOptions(values, options) { const selectedOptions = []; options.forEach((option) => { if (utils_1.hasProperty(option, 'options')) { compileSelectedOptions(values, option.options).forEach((nextTierOption) => { selectedOptions.push(nextTierOption); }); } else { if (values.findIndex((value) => utils_1.isEqual(value, option.value)) > -1) { selectedOptions.push(option); } } }); return selectedOptions; } exports.compileSelectedOptions = compileSelectedOptions; function getOptionLabel(option) { return option.children || option.label; } exports.getOptionLabel = getOptionLabel; function isSearchOption(searchValue, option) { const { value, label, guessers, children } = option; if (value.toString().indexOf(searchValue) > -1) { return true; } if (typeof children !== 'object' && children.toString().indexOf(searchValue) > -1) { return true; } if (label && label.toString().indexOf(searchValue) > -1) { return true; } if (guessers) { return guessers.some((guesser) => { return guesser.toString().indexOf(searchValue) > -1; }); } return false; } exports.isSearchOption = isSearchOption; function filterOptions(options, searchValue, onFilter) { if (!searchValue) { return options; } const ret = []; options.forEach((option) => { if (utils_1.hasProperty(option, 'options')) { const nextTierOption = filterOptions(option.options, searchValue, onFilter); if (nextTierOption.length > 0) { options.push({ title: option.title, className: option.className, options: nextTierOption }); } } else { const matching = onFilter ? onFilter(searchValue, option) : isSearchOption(searchValue, option); if (matching) { ret.push(option); } } }); return ret; } exports.filterOptions = filterOptions; function transformRenderOptionsToRealOptions(options) { let realOptions = []; options.forEach((option) => { if (utils_1.hasProperty(option, 'options')) { realOptions = realOptions.concat(transformRenderOptionsToRealOptions(option.options)); } else { realOptions.push(option); } }); return realOptions; } exports.transformRenderOptionsToRealOptions = transformRenderOptionsToRealOptions; function getNextOption(options, searchValue, option) { let realOptions = transformRenderOptionsToRealOptions(options) .filter((item) => item.disabled !== true); if (searchValue !== '') { realOptions = realOptions.filter((item) => { return isSearchOption(searchValue, item); }); } if (option === undefined) { return realOptions[0]; } const currentIndex = realOptions.findIndex((item) => item.value === option.value); const nextIndex = currentIndex === realOptions.length - 1 ? 0 : currentIndex + 1; return realOptions[nextIndex]; } exports.getNextOption = getNextOption; function getPreOption(options, searchValue, option) { let realOptions = transformRenderOptionsToRealOptions(options) .filter((item) => item.disabled !== true); if (searchValue !== '') { realOptions = realOptions.filter((item) => { return isSearchOption(searchValue, item); }); } if (option === undefined) { return realOptions[realOptions.length - 1]; } const currentIndex = realOptions.findIndex((item) => item.value === option.value); const preIndex = currentIndex === 0 ? realOptions.length - 1 : currentIndex - 1; return realOptions[preIndex]; } exports.getPreOption = getPreOption;