UNPKG

sard-uniapp

Version:

sard-uniapp 是一套基于 Uniapp + Vue3 框架开发的兼容多端的 UI 组件库

53 lines (52 loc) 1.99 kB
import { isNullish } from '../../utils'; import { defaultConfig } from '../config'; export const defaultCascaderProps = () => ({ ...defaultConfig.cascader, options: () => [], }); export function getSelectedOptionsByValue(options, value, useOptionKeysReturn, multiple) { const { getValue, getChildren } = useOptionKeysReturn; // 多选 if (multiple) { if (Array.isArray(value)) { return value .map((item) => getSelectedOptionsByValue(options, item, useOptionKeysReturn)) .filter((item) => Array.isArray(item) ? item.length !== 0 : !isNullish(item)); } } // 单选 else { // 全路径 if (Array.isArray(value)) { const selectedOptions = []; let list = options; for (const item of value) { const option = list.find((option) => getValue(option) === item); if (!option) break; selectedOptions.push(option); list = getChildren(option); if (!Array.isArray(list)) break; } return selectedOptions; } // 最后一级 else { for (const option of options) { // 优先在子结点中查找,找到后再向上回溯路径 // 这样可以处理存在重复值场景时候更偏向于更深层次的选项 if (Array.isArray(getChildren(option))) { const selectedOptions = getSelectedOptionsByValue(getChildren(option), value, useOptionKeysReturn, multiple); if (selectedOptions) { return [option, ...selectedOptions]; } } if (getValue(option) === value) { return [option]; } } } } } export const cascaderOptionsContextSymbol = Symbol('cascader-options');