Skip to content

Commit f56f937

Browse files
Merge pull request #167 from vue-pivottable/ts/pivot-ui
refactor: migrate VAggregatorCell to TypeScript
2 parents 2e79ef7 + c9c13b6 commit f56f937

File tree

1 file changed

+47
-55
lines changed

1 file changed

+47
-55
lines changed

src/components/pivottable-ui/VAggregatorCell.vue

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</div>
2828
<template v-if="numValsAllowed">
2929
<VDropdown
30-
v-for="(item, i) in new Array(numValsAllowed).fill()"
30+
v-for="(_, i) in Array.from({ length: numValsAllowed })"
3131
:key="i"
3232
:options="valsOptions"
3333
:value="vals[i]"
@@ -41,77 +41,69 @@
4141
<script setup lang="ts">
4242
import { computed } from 'vue'
4343
import VDropdown from './VDropdown.vue'
44+
import type { AggregatorTemplate } from '@/helper/utilities'
4445
45-
const emit = defineEmits([
46-
'update:aggregatorName',
47-
'update:rowOrder',
48-
'update:colOrder',
49-
'update:vals'
50-
])
51-
const props = defineProps({
52-
aggregatorItems: {
53-
type: Object,
54-
default: () => ({})
55-
},
56-
aggregatorName: {
57-
type: String,
58-
default: 'Count'
59-
},
60-
rowOrder: {
61-
type: String,
62-
default: 'key_a_to_z',
63-
validator: function (value) {
64-
return (
65-
['key_a_to_z', 'value_a_to_z', 'value_z_to_a'].indexOf(value) !== -1
66-
)
67-
}
68-
},
69-
colOrder: {
70-
type: String,
71-
default: 'key_a_to_z',
72-
validator: function (value) {
73-
return (
74-
['key_a_to_z', 'value_a_to_z', 'value_z_to_a'].indexOf(value) !== -1
75-
)
76-
}
77-
},
78-
vals: {
79-
type: Array,
80-
default: function () {
81-
return []
82-
}
83-
},
84-
attributeNames: {
85-
type: Array,
86-
default: () => []
87-
},
88-
hiddenFromAggregators: {
89-
type: Array,
90-
default: () => []
91-
}
46+
const emit = defineEmits<{
47+
(event: 'update:aggregatorName', value: string): void
48+
(event: 'update:rowOrder', value: string): void
49+
(event: 'update:colOrder', value: string): void
50+
(event: 'update:vals', value: string[]): void
51+
}>()
52+
53+
type OrderType = 'key_a_to_z' | 'value_a_to_z' | 'value_z_to_a'
54+
55+
interface AggregatorCellProps {
56+
aggregatorItems: Record<string, AggregatorTemplate>
57+
aggregatorName: string
58+
rowOrder: OrderType
59+
colOrder: OrderType
60+
vals: string[]
61+
attributeNames: string[]
62+
hiddenFromAggregators: string[]
63+
}
64+
65+
const props = withDefaults(defineProps<AggregatorCellProps>(), {
66+
aggregatorItems: () => ({}),
67+
aggregatorName: 'Count',
68+
rowOrder: 'key_a_to_z',
69+
colOrder: 'key_a_to_z',
70+
vals: () => [],
71+
attributeNames: () => [],
72+
hiddenFromAggregators: () => []
9273
})
93-
const sortIcons = {
74+
75+
const sortIcons: Record<
76+
OrderType,
77+
{ rowSymbol: string; colSymbol: string; next: OrderType }
78+
> = {
9479
key_a_to_z: { rowSymbol: '', colSymbol: '', next: 'value_a_to_z' },
9580
value_a_to_z: { rowSymbol: '', colSymbol: '', next: 'value_z_to_a' },
9681
value_z_to_a: { rowSymbol: '', colSymbol: '', next: 'key_a_to_z' }
97-
}
82+
} as const
83+
9884
const aggregatorOptions = computed(() => Object.keys(props.aggregatorItems))
9985
const valsOptions = computed(() =>
10086
props.attributeNames.filter(
10187
(item) => !props.hiddenFromAggregators.includes(item)
10288
)
10389
)
104-
const numValsAllowed = computed(
105-
() => props.aggregatorItems[props.aggregatorName]([])().numInputs || 0
106-
)
90+
const numValsAllowed = computed(() => {
91+
const aggregator = props.aggregatorItems[props.aggregatorName]
92+
if (!aggregator) return 0
93+
const aggregatorInstance = aggregator([])?.()
94+
return aggregatorInstance?.numInputs || 0
95+
})
96+
10797
const currentRowSortIcon = computed(() => sortIcons[props.rowOrder].rowSymbol)
10898
const currentColSortIcon = computed(() => sortIcons[props.colOrder].colSymbol)
109-
const updateAggregatorName = (value) => emit('update:aggregatorName', value)
99+
100+
const updateAggregatorName = (value: string) =>
101+
emit('update:aggregatorName', value)
110102
const updateRowOrder = () =>
111103
emit('update:rowOrder', sortIcons[props.rowOrder].next)
112104
const updateColOrder = () =>
113105
emit('update:colOrder', sortIcons[props.colOrder].next)
114-
const updateVals = (val, i) => {
106+
const updateVals = (val: string, i: number) => {
115107
const newVals = [...props.vals]
116108
newVals[i] = val
117109
emit('update:vals', newVals)

0 commit comments

Comments
 (0)