chowa
Version:
UI component library based on React
85 lines (84 loc) • 2.87 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 });
const utils_1 = require("../utils");
function isOptionGroup(option) {
return typeof option === 'object' && utils_1.hasProperty(option, 'title');
}
exports.isOptionGroup = isOptionGroup;
function compileOptions(options, mode, searchValue) {
if (mode !== 'search') {
return options;
}
const ret = [];
options.forEach((option) => {
if (isOptionGroup(option)) {
const filterChildOptions = compileOptions(option.children, mode, searchValue);
if (filterChildOptions.length > 0) {
ret.push({
title: option.title,
children: filterChildOptions
});
}
}
else if (typeof option === 'string') {
if (option.indexOf(searchValue) > -1) {
ret.push(option);
}
}
else if (typeof option === 'number') {
if (option.toString().indexOf(searchValue) > -1) {
ret.push(option);
}
}
else {
if (option.value.toString().indexOf(searchValue) > -1) {
ret.push(option);
}
}
});
return ret;
}
exports.compileOptions = compileOptions;
function transformOptionsToRealValues(options) {
let realValues = [];
if (Array.isArray(options)) {
options.forEach((option) => {
if (isOptionGroup(option)) {
realValues = realValues.concat(transformOptionsToRealValues(option.children));
}
else {
realValues.push(typeof option === 'object' ? option.value : option);
}
});
}
return realValues;
}
exports.transformOptionsToRealValues = transformOptionsToRealValues;
function getNextValue(options, activeValue) {
const realValues = transformOptionsToRealValues(options);
if (activeValue === undefined) {
return realValues[0];
}
const currentIndex = realValues.indexOf(activeValue);
const nextIndex = currentIndex === realValues.length - 1 ? 0 : currentIndex + 1;
return realValues[nextIndex];
}
exports.getNextValue = getNextValue;
function getPreValue(options, activeValue) {
const realValues = transformOptionsToRealValues(options);
if (activeValue === undefined) {
return realValues[realValues.length - 1];
}
const currentIndex = realValues.indexOf(activeValue);
const preIndex = currentIndex === 0 ? realValues.length - 1 : currentIndex - 1;
return realValues[preIndex];
}
exports.getPreValue = getPreValue;