import { Ref, computed } from "vue"; export type CascaderOptions = { label: string; value: string; children?: CascaderOptions; }[]; export const splitChar = ">"; export const getValue = (val: string) => val.split(splitChar); export const getRaw = (val: string[]) => val.join(splitChar); export const genCascaderValue = < K extends string, T extends Ref<{ [key in K]: string }> >( target: T, key: K, def?: string ) => computed({ get: () => target.value[key] ? target.value[key].split(">") : def ? [def] : undefined, set: (val) => (target.value[key] = val!.join(">")), }); export const getCode = (options: CascaderOptions, raw: string) => { const paths = getValue(raw); let current = options; let rets = ""; for (let val of paths) { const idx = current.findIndex(({ value }) => value === val); if (~idx) { rets += idx + 1; if (current[idx].children) { current = current[idx].children!; } else { break; } } else { // rets += "0"; break; } } return rets; };