winbox-ui-next
Version:
We Design Vue 2.0: A Vue.js 3 UI Library
140 lines (139 loc) • 4.1 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
import { isObject, isNumber, isFunction } from "../_utils/is.js";
const isGroupOption = (option) => {
return isObject(option) && "isGroup" in option;
};
const isGroupOptionInfo = (option) => {
return isObject(option) && "isGroup" in option;
};
const getValueString = (value, valueKey = "value") => String(isObject(value) ? value[valueKey] : value);
const getKeyFromValue = (value, valueKey = "value") => {
if (isObject(value)) {
return `__w__option__object__${value[valueKey]}`;
}
if (value || isNumber(value)) {
return `__w__option__${typeof value}-${value}`;
}
return "";
};
const createOptionInfo = (option, {
valueKey,
fieldNames,
origin,
index = -1
}) => {
var _a;
if (isObject(option)) {
const value = option[fieldNames.value];
return {
raw: option,
index,
key: getKeyFromValue(value, valueKey),
origin,
value,
label: (_a = option[fieldNames.label]) != null ? _a : getValueString(value, valueKey),
render: option[fieldNames.render],
disabled: Boolean(option[fieldNames.disabled]),
tagProps: option[fieldNames.tagProps]
};
}
const raw = {
value: option,
label: String(option),
disabled: false
};
return __spreadValues({
raw,
index,
key: getKeyFromValue(option, valueKey),
origin
}, raw);
};
const getOptionInfos = (options, {
valueKey,
fieldNames,
origin,
optionInfoMap
}) => {
var _a;
const infos = [];
for (const item of options) {
if (isGroupOption(item)) {
const options2 = getOptionInfos((_a = item.options) != null ? _a : [], {
valueKey,
fieldNames,
origin,
optionInfoMap
});
if (options2.length > 0) {
infos.push(__spreadProps(__spreadValues({}, item), {
key: `__w__group__${item.label}`,
options: options2
}));
}
} else {
const optionInfo = createOptionInfo(item, {
valueKey,
fieldNames,
origin
});
infos.push(optionInfo);
if (!optionInfoMap.get(optionInfo.key)) {
optionInfoMap.set(optionInfo.key, optionInfo);
}
}
}
return infos;
};
const getValidOptions = (optionInfos, {
inputValue,
filterOption
}) => {
const travel = (optionInfos2) => {
var _a;
const options = [];
for (const item of optionInfos2) {
if (isGroupOptionInfo(item)) {
const _options = travel((_a = item.options) != null ? _a : []);
if (_options.length > 0) {
options.push(__spreadProps(__spreadValues({}, item), { options: _options }));
}
} else if (isValidOption(item, { inputValue, filterOption })) {
options.push(item);
}
}
return options;
};
return travel(optionInfos);
};
const isValidOption = (optionInfo, {
inputValue,
filterOption
}) => {
if (isFunction(filterOption)) {
return !inputValue || filterOption(inputValue, optionInfo.raw);
}
if (filterOption) {
return optionInfo.label.toLowerCase().includes((inputValue != null ? inputValue : "").toLowerCase());
}
return true;
};
export { createOptionInfo, getKeyFromValue, getOptionInfos, getValidOptions, getValueString, isGroupOption, isGroupOptionInfo, isValidOption };