vuestic-ui
Version:
Vue 3 UI Framework
1 lines • 5.49 kB
Source Map (JSON)
{"version":3,"file":"useColumns.mjs","sources":["../../../../../../src/components/va-data-table/hooks/useColumns.ts"],"sourcesContent":["import { computed, ExtractPropTypes, PropType } from 'vue'\nimport { startCase } from '../../../utils/text-case'\nimport { mergeDeepMultiple } from '../../../utils/merge-deep'\n\nimport { createItemsProp } from './useCommonProps'\n\nimport { warn } from '../../../utils/console'\n\nimport type {\n DataTableColumnSource,\n DataTableColumnInternal,\n DataTableSortingOptions,\n} from '../types'\n\nexport const sortingOptionsValidator = (options: DataTableSortingOptions) => {\n const isAllowedOptionsLength = options.length === 2 || options.length === 3\n const isAvailableOptions = options.every((option) => ['asc', 'desc', null].includes(option))\n const isUniqueOptions = (options.length === new Set(options).size)\n\n return isAllowedOptionsLength && isAvailableOptions && isUniqueOptions\n}\n\nexport const useColumnsProps = {\n ...createItemsProp(),\n columns: { type: Array as PropType<DataTableColumnSource[]>, default: () => [] as DataTableColumnSource[] },\n sortingOptions: {\n type: Array as PropType<DataTableSortingOptions>,\n default: () => ['asc', 'desc', null],\n validator: sortingOptionsValidator,\n },\n}\n\ntype useColumnsPropsType = ExtractPropTypes<typeof useColumnsProps>\n\nexport const buildTableColumn = (\n source: DataTableColumnSource,\n initialIndex: number,\n props: useColumnsPropsType,\n): DataTableColumnInternal => {\n const input = typeof source === 'string' ? { key: source } : source\n\n const isValidOptions = input.sortingOptions\n ? sortingOptionsValidator(input.sortingOptions)\n : true\n\n if (!isValidOptions) {\n warn(`The \"sortingOptions\" array in the column with \"${input.key}\" key is invalid. For this column, the \"sortingOptions\" value is taken as for the table: ${JSON.stringify(props.sortingOptions)}.`)\n }\n\n return {\n source,\n initialIndex,\n key: input.key,\n name: input.name || input.key,\n label: input.label || startCase(input.key),\n thTitle: input.thTitle || input.headerTitle || input.label || startCase(input.key),\n sortable: input.sortable || false,\n sortingFn: input.sortingFn,\n displayFormatFn: input.displayFormatFn,\n sortingOptions: (isValidOptions && input.sortingOptions) || props.sortingOptions,\n thAlign: input.thAlign || input.alignHead || 'left',\n thVerticalAlign: input.thVerticalAlign || input.verticalAlignHead || 'middle',\n tdAlign: input.tdAlign || input.align || 'left',\n tdVerticalAlign: input.tdVerticalAlign || input.verticalAlign || 'middle',\n width: input.width,\n tdClass: input.tdClass || input.classes,\n thClass: input.thClass || input.headerClasses,\n tdStyle: input.tdStyle || input.style,\n thStyle: input.thStyle || input.headerStyle,\n }\n}\n\nconst buildColumnsFromItems = (props: useColumnsPropsType) => {\n return Object.keys(mergeDeepMultiple({}, ...props.items)).map((item, index) => buildTableColumn(item, index, props))\n}\n\nconst buildNormalizedColumns = (props: useColumnsPropsType) => {\n return props.columns.map((item, index) => buildTableColumn(item, index, props))\n}\n\nexport const useColumns = (props: useColumnsPropsType) => {\n const columnsComputed = computed(() => {\n if (props.columns.length === 0) {\n // if no column definitions provided then build them based on provided rawItems\n // e.g. if provided items look like `[{a: 1}, {b: 2}]` then there should be 2 columns: A and B\n return buildColumnsFromItems(props)\n } else {\n return buildNormalizedColumns(props)\n }\n })\n\n return {\n columnsComputed,\n }\n}\n"],"names":[],"mappings":";;;;;AAca,MAAA,0BAA0B,CAAC,YAAqC;AAC3E,QAAM,yBAAyB,QAAQ,WAAW,KAAK,QAAQ,WAAW;AAC1E,QAAM,qBAAqB,QAAQ,MAAM,CAAC,WAAW,CAAC,OAAO,QAAQ,IAAI,EAAE,SAAS,MAAM,CAAC;AAC3F,QAAM,kBAAmB,QAAQ,WAAW,IAAI,IAAI,OAAO,EAAE;AAE7D,SAAO,0BAA0B,sBAAsB;AACzD;AAEO,MAAM,kBAAkB;AAAA,EAC7B,GAAG,gBAAgB;AAAA,EACnB,SAAS,EAAE,MAAM,OAA4C,SAAS,MAAM,CAAA,EAA8B;AAAA,EAC1G,gBAAgB;AAAA,IACd,MAAM;AAAA,IACN,SAAS,MAAM,CAAC,OAAO,QAAQ,IAAI;AAAA,IACnC,WAAW;AAAA,EACb;AACF;AAIO,MA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