chowa
Version:
UI component library based on React
146 lines (145 loc) • 5.53 kB
JavaScript
/**
* @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.
*/
;
Object.defineProperty(exports, "__esModule", { value: true });
function compileSelectedOptions(value, options) {
const selectedOptions = [];
if (Array.isArray(value) && value.length > 0) {
let nextOptions = [].concat(options);
value.forEach((val) => {
nextOptions.every((item) => {
if (item.value === val) {
selectedOptions.push(item);
nextOptions = item.children;
return false;
}
return true;
});
});
}
return selectedOptions;
}
exports.compileSelectedOptions = compileSelectedOptions;
function computedOptionsTier(options, nextTier = 1) {
let tier = 0;
options.forEach((option) => {
if (option.children && option.children.length > 0) {
tier = computedOptionsTier(option.children, nextTier + 1);
}
else if (nextTier > tier) {
tier = nextTier;
}
});
return tier;
}
exports.computedOptionsTier = computedOptionsTier;
function isSearchOption(searchValue, option) {
const { value, label } = option;
if (value.toString().indexOf(searchValue) > -1) {
return true;
}
if (label && label.toString().indexOf(searchValue) > -1) {
return true;
}
return false;
}
exports.isSearchOption = isSearchOption;
function filterOptions(options, searchValue, onFilter) {
if (!searchValue) {
return options;
}
const ret = [];
options.forEach((option) => {
const matching = onFilter ? onFilter(searchValue, option) : isSearchOption(searchValue, option);
const filterChildren = Array.isArray(option.children)
? filterOptions(option.children, searchValue, onFilter)
: [];
if (matching || filterChildren.length > 0) {
ret.push(Object.assign(Object.assign({}, option), { children: filterChildren.length > 0 ? filterChildren : option.children }));
}
});
return ret;
}
exports.filterOptions = filterOptions;
function spreadOptions(options) {
const ret = [];
[].concat(options).forEach((option) => {
if (Array.isArray(option.children)) {
const nextOptions = spreadOptions(option.children);
nextOptions.forEach((spread) => {
ret.push([option].concat(spread));
});
}
else {
ret.push([option]);
}
});
return ret;
}
exports.spreadOptions = spreadOptions;
function getRealOptions(selectedOptions, options, optionsTier) {
return (selectedOptions.length === 0
? options
: (optionsTier === selectedOptions.length
? selectedOptions[selectedOptions.length - 2].children || []
: selectedOptions[selectedOptions.length - 1].children || [])).filter((item) => item.disabled !== true);
}
exports.getRealOptions = getRealOptions;
function getPreOption(selectedOptions, options, optionsTier, option) {
const realOptions = getRealOptions(selectedOptions, options, optionsTier);
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;
function getNextOption(selectedOptions, options, optionsTier, option) {
const realOptions = getRealOptions(selectedOptions, options, optionsTier);
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 getPreFilterOption(options, spread) {
const realOptions = spreadOptions(options)
.filter((nextSpread) => {
return nextSpread.some((item) => item.disabled === true) !== true;
});
if (spread === undefined) {
return realOptions[0];
}
const matchValue = spread.reduce((pre, cur) => pre + cur.value.toString(), '');
const currentIndex = realOptions.findIndex((nextSpread) => {
return matchValue === nextSpread.reduce((pre, cur) => pre + cur.value.toString(), '');
});
const preIndex = currentIndex === 0 ? realOptions.length - 1 : currentIndex - 1;
return realOptions[preIndex];
}
exports.getPreFilterOption = getPreFilterOption;
function getNextFilterOption(options, spread) {
const realOptions = spreadOptions(options)
.filter((nextSpread) => {
return nextSpread.some((item) => item.disabled === true) !== true;
});
if (spread === undefined) {
return realOptions[0];
}
const matchValue = spread.reduce((pre, cur) => pre + cur.value.toString(), '');
const currentIndex = realOptions.findIndex((nextSpread) => {
return matchValue === nextSpread.reduce((pre, cur) => pre + cur.value.toString(), '');
});
const nextIndex = currentIndex === realOptions.length - 1 ? 0 : currentIndex + 1;
return realOptions[nextIndex];
}
exports.getNextFilterOption = getNextFilterOption;