list-items.mjs.map 6.6 KB

1
  1. {"version":3,"file":"list-items.mjs","names":["computed","deepEqual","getPropertyFromItem","pick","propsFactory","makeItemsProps","items","type","Array","default","itemTitle","String","Function","itemValue","itemChildren","Boolean","itemProps","returnObject","transformItem","props","item","title","value","children","isArray","undefined","_props","transformItems","raw","array","push","useItems","useTransformItems","transform","transformIn","filter","v","some","map","existingItem","find","transformOut","_ref"],"sources":["../../src/composables/list-items.ts"],"sourcesContent":["// Utilities\nimport { computed } from 'vue'\nimport { deepEqual, getPropertyFromItem, pick, propsFactory } from '@/util'\n\n// Types\nimport type { PropType, Ref } from 'vue'\nimport type { SelectItemKey } from '@/util'\n\nexport interface ListItem<T = any> {\n title: string\n value: any\n props: {\n [key: string]: any\n title: string\n value: any\n }\n children?: ListItem<T>[]\n raw: T\n}\n\nexport interface ItemProps {\n items: any[]\n itemTitle: SelectItemKey\n itemValue: SelectItemKey\n itemChildren: SelectItemKey\n itemProps: SelectItemKey\n returnObject: boolean\n}\n\n// Composables\nexport const makeItemsProps = propsFactory({\n items: {\n type: Array as PropType<ItemProps['items']>,\n default: () => ([]),\n },\n itemTitle: {\n type: [String, Array, Function] as PropType<SelectItemKey>,\n default: 'title',\n },\n itemValue: {\n type: [String, Array, Function] as PropType<SelectItemKey>,\n default: 'value',\n },\n itemChildren: {\n type: [Boolean, String, Array, Function] as PropType<SelectItemKey>,\n default: 'children',\n },\n itemProps: {\n type: [Boolean, String, Array, Function] as PropType<SelectItemKey>,\n default: 'props',\n },\n returnObject: Boolean,\n}, 'list-items')\n\nexport function transformItem (props: Omit<ItemProps, 'items'>, item: any): ListItem {\n const title = getPropertyFromItem(item, props.itemTitle, item)\n const value = props.returnObject ? item : getPropertyFromItem(item, props.itemValue, title)\n const children = getPropertyFromItem(item, props.itemChildren)\n const itemProps = props.itemProps === true\n ? typeof item === 'object' && item != null && !Array.isArray(item)\n ? 'children' in item\n ? pick(item, ['children'])[1]\n : item\n : undefined\n : getPropertyFromItem(item, props.itemProps)\n\n const _props = {\n title,\n value,\n ...itemProps,\n }\n\n return {\n title: String(_props.title ?? ''),\n value: _props.value,\n props: _props,\n children: Array.isArray(children) ? transformItems(props, children) : undefined,\n raw: item,\n }\n}\n\nexport function transformItems (props: Omit<ItemProps, 'items'>, items: ItemProps['items']) {\n const array: ListItem[] = []\n\n for (const item of items) {\n array.push(transformItem(props, item))\n }\n\n return array\n}\n\nexport function useItems (props: ItemProps) {\n const items = computed(() => transformItems(props, props.items))\n\n return useTransformItems(items, value => transformItem(props, value))\n}\n\nexport function useTransformItems <T extends { value: unknown }> (items: Ref<T[]>, transform: (value: unknown) => T) {\n function transformIn (value: any[]): T[] {\n return value\n // When the model value is null, returns an InternalItem based on null\n // only if null is one of the items\n .filter(v => v !== null || items.value.some(item => item.value === null))\n .map(v => {\n const existingItem = items.value.find(item => deepEqual(v, item.value))\n // Nullish existingItem means value is a custom input value from combobox\n // In this case, use transformItem to create an InternalItem based on value\n return existingItem ?? transform(v)\n })\n }\n\n function transformOut (value: T[]) {\n return value.map(({ value }) => value)\n }\n\n return { items, transformIn, transformOut }\n}\n"],"mappings":"AAAA;AACA,SAASA,QAAQ,QAAQ,KAAK;AAAA,SACrBC,SAAS,EAAEC,mBAAmB,EAAEC,IAAI,EAAEC,YAAY,6BAE3D;AAyBA;AACA,OAAO,MAAMC,cAAc,GAAGD,YAAY,CAAC;EACzCE,KAAK,EAAE;IACLC,IAAI,EAAEC,KAAqC;IAC3CC,OAAO,EAAEA,CAAA,KAAO;EAClB,CAAC;EACDC,SAAS,EAAE;IACTH,IAAI,EAAE,CAACI,MAAM,EAAEH,KAAK,EAAEI,QAAQ,CAA4B;IAC1DH,OAAO,EAAE;EACX,CAAC;EACDI,SAAS,EAAE;IACTN,IAAI,EAAE,CAACI,MAAM,EAAEH,KAAK,EAAEI,QAAQ,CAA4B;IAC1DH,OAAO,EAAE;EACX,CAAC;EACDK,YAAY,EAAE;IACZP,IAAI,EAAE,CAACQ,OAAO,EAAEJ,MAAM,EA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