Skip to content

Commit 2e79ef7

Browse files
Merge pull request #166 from vue-pivottable/feat/typescript-pivottable
feat: add TypeScript support to pivottable components
2 parents 10574b4 + 7abfc37 commit 2e79ef7

29 files changed

+294
-495
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"clean": "rimraf dist",
5555
"clean:all": "rimraf dist packages/*/dist",
5656
"dev": "vite",
57-
"build": "tsc src/helper/utilities.ts --declaration --emitDeclarationOnly --outDir src/helper && vite build",
57+
"build": "vite build",
5858
"preview": "vite preview",
5959
"lint": "eslint",
6060
"build:all": "pnpm clean && pnpm build && pnpm -r --filter './packages/*' build",

packages/lazy-table-renderer/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default markRaw({
66
'Lazy Table': defineComponent({
77
name: 'VueLazyTable',
88
props: { ...PivotUtilities.defaultProps },
9-
setup(props) {
9+
setup (props) {
1010
return () =>
1111
h(LazyPivottableRenderer, {
1212
...props,

src/components/pivottable-ui/VAggregatorCell.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</td>
3939
</template>
4040

41-
<script setup>
41+
<script setup lang="ts">
4242
import { computed } from 'vue'
4343
import VDropdown from './VDropdown.vue'
4444

src/components/pivottable-ui/VPivottableUi.vue

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -131,52 +131,44 @@
131131
</table>
132132
</template>
133133

134-
<script setup>
135-
import { defaultProps, PivotData, sortAs } from '@/helper'
134+
<script setup lang="ts">
135+
import { aggregators, PivotData, sortAs } from '@/helper'
136136
import VRendererCell from './VRendererCell.vue'
137137
import VAggregatorCell from './VAggregatorCell.vue'
138138
import VDragAndDropCell from './VDragAndDropCell.vue'
139139
import VPivottable from '../pivottable/VPivottable.vue'
140-
import TableRenderer from '../pivottable/renderer/index'
141140
import { computed, watch } from 'vue'
142141
import {
143142
usePropsState,
144143
useMaterializeInput,
145144
usePivotUiState,
146145
provideFilterBox
147146
} from '@/composables'
147+
import { DefaultPropsType } from '@/types'
148148
149-
const props = defineProps({
150-
...defaultProps,
151-
hiddenAttributes: {
152-
type: Array,
153-
default: () => []
154-
},
155-
hiddenFromAggregators: {
156-
type: Array,
157-
default: () => []
158-
},
159-
hiddenFromDragDrop: {
160-
type: Array,
161-
default: () => []
162-
},
163-
restrictedFromDragDrop: {
164-
type: Array,
165-
default: () => []
166-
},
167-
menuLimit: {
168-
type: Number,
169-
default: 500
170-
},
171-
pivotModel: {
172-
type: Object,
173-
default: () => ({})
174-
},
175-
hideFilterBoxOfUnusedAttributes: {
176-
type: Boolean,
177-
default: false
149+
const props = withDefaults(
150+
defineProps<
151+
DefaultPropsType & {
152+
hiddenAttributes?: string[]
153+
hiddenFromAggregators?: string[]
154+
hiddenFromDragDrop?: string[]
155+
restrictedFromDragDrop?: string[]
156+
menuLimit?: number
157+
pivotModel?: any
158+
hideFilterBoxOfUnusedAttributes?: boolean
159+
}
160+
>(),
161+
{
162+
aggregators: () => aggregators,
163+
hiddenAttributes: () => [],
164+
hiddenFromAggregators: () => [],
165+
hiddenFromDragDrop: () => [],
166+
restrictedFromDragDrop: () => [],
167+
menuLimit: 500,
168+
hideFilterBoxOfUnusedAttributes: false
178169
}
179-
})
170+
)
171+
180172
const {
181173
state,
182174
localeStrings,
@@ -205,7 +197,7 @@ const { allFilters, materializedInput } = useMaterializeInput(
205197
)
206198
207199
const rendererItems = computed(() =>
208-
Object.keys(state.renderers).length ? state.renderers : TableRenderer
200+
Object.keys(state.renderers).length ? state.renderers : {}
209201
)
210202
const aggregatorItems = computed(() => state.aggregators)
211203
const rowAttrs = computed(() => {
@@ -238,7 +230,7 @@ const unusedAttrs = computed(() => {
238230
!state.hiddenAttributes.includes(e) &&
239231
!state.hiddenFromDragDrop.includes(e)
240232
)
241-
.sort(sortAs(state.unusedOrder))
233+
.sort(sortAs(pivotUiState.unusedOrder))
242234
})
243235
244236
const pivotData = computed(() => new PivotData(state))
@@ -264,12 +256,13 @@ const pivotProps = computed(() => ({
264256
rowOrder: state.rowOrder,
265257
colOrder: state.colOrder,
266258
tableMaxWidth: state.tableMaxWidth,
267-
localeStrings: localeStrings.value
259+
localeStrings: localeStrings.value,
260+
menuLimit: props.menuLimit
268261
}))
269262
270-
onUpdateUnusedOrder(props.unusedAttrs)
263+
onUpdateUnusedOrder(unusedAttrs.value)
271264
272-
provideFilterBox(props)
265+
provideFilterBox(pivotProps.value)
273266
274267
watch(
275268
[allFilters, materializedInput],

src/components/pivottable-ui/VRendererCell.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const rendererOptions = computed<string[]>(() =>
2525
2626
interface RendererDefinition {
2727
name: string
28-
props: Record<string, any>
28+
props?: Record<string, any>
2929
setup: (props: any) => () => VNode
3030
}
3131

src/components/pivottable/VPivottable.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
/>
66
</template>
77

8-
<script setup>
8+
<script setup lang="ts">
99
import { computed } from 'vue'
10-
import { defaultProps } from '@/helper'
1110
import TableRenderer from './renderer'
12-
const props = defineProps({
13-
...defaultProps
14-
})
11+
import { DefaultPropsType } from '@/types'
12+
13+
const props = defineProps<DefaultPropsType>()
1514
1615
const rendererComponent = computed(
1716
() => props.renderers[props.rendererName] || TableRenderer.Table

src/components/pivottable/VPivottableBody.vue

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,33 @@
33
<VPivottableBodyRows
44
:row-keys="rowKeys"
55
:col-keys="colKeys"
6-
:row-total="rowTotal"
6+
:show-row-total="showRowTotal"
77
:table-options="tableOptions"
88
/>
99
<VPivottableBodyRowsTotalRow
10-
:col-total="colTotal"
11-
:row-total="rowTotal"
10+
:show-col-total="showColTotal"
11+
:show-row-total="showRowTotal"
1212
:row-attrs="rowAttrs"
1313
:col-attrs="colAttrs"
1414
:col-keys="colKeys"
15-
:locale-strings="localeStrings"
15+
:language-pack="languagePack"
1616
:table-options="tableOptions"
1717
/>
1818
</tbody>
1919
</template>
2020

21-
<script setup>
22-
import { useProvidePivotData } from '@/composables/useProvidePivotData'
21+
<script setup lang="ts">
22+
import { useProvidePivotData } from '@/composables'
2323
import VPivottableBodyRows from './VPivottableBodyRows.vue'
2424
import VPivottableBodyRowsTotalRow from './VPivottableBodyRowsTotalRow.vue'
25+
import type { DefaultPropsType } from '@/types'
2526
26-
defineProps({
27-
rowTotal: {
28-
type: Boolean,
29-
default: true
30-
},
31-
colTotal: {
32-
type: Boolean,
33-
default: true
34-
},
35-
localeStrings: {
36-
type: Object,
37-
default: () => ({
38-
totals: 'Totals'
39-
})
40-
},
41-
tableOptions: {
42-
type: Object,
43-
default: () => ({
44-
clickCallback: null
45-
})
46-
}
47-
})
27+
type Props = Pick<
28+
DefaultPropsType,
29+
'showRowTotal' | 'showColTotal' | 'languagePack' | 'tableOptions'
30+
>
31+
32+
defineProps<Props>()
4833
4934
const { pivotData, rowKeys, colKeys, rowAttrs, colAttrs } =
5035
useProvidePivotData()

src/components/pivottable/VPivottableBodyRows.vue

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}}
3737
</td>
3838
<td
39-
v-if="rowTotal"
39+
v-if="showRowTotal"
4040
class="pvtTotal"
4141
:style="getRowTotalStyle(rowKey)"
4242
@click="
@@ -48,27 +48,19 @@
4848
</tr>
4949
</template>
5050

51-
<script setup>
52-
import { useProvidePivotData } from '@/composables/useProvidePivotData'
51+
<script setup lang="ts">
52+
import { useProvidePivotData } from '@/composables'
53+
import { DefaultPropsType } from '@/types'
5354
54-
const props = defineProps({
55-
rowKeys: {
56-
type: Array,
57-
required: true
58-
},
59-
colKeys: {
60-
type: Array,
61-
required: true
62-
},
63-
rowTotal: {
64-
type: Boolean,
65-
required: true
66-
},
67-
tableOptions: {
68-
type: Object,
69-
required: true
70-
}
71-
})
55+
type VPivottableBodyRowsProps = Pick<
56+
DefaultPropsType,
57+
'showRowTotal' | 'tableOptions'
58+
> & {
59+
rowKeys: any[][]
60+
colKeys: any[][]
61+
}
62+
63+
const props = defineProps<VPivottableBodyRowsProps>()
7264
7365
const {
7466
pivotData,
@@ -80,34 +72,34 @@ const {
8072
getAggregator
8173
} = useProvidePivotData()
8274
83-
const getValueCellStyle = (rowKey, colKey) => {
75+
const getValueCellStyle = (rowKey: any, colKey: any) => {
8476
const value = getAggregator(rowKey, colKey).value()
8577
return valueCellColors(rowKey, colKey, value)
8678
}
8779
88-
const getRowTotalStyle = (rowKey) => {
80+
const getRowTotalStyle = (rowKey: any) => {
8981
const value = getAggregator(rowKey, []).value()
9082
return colTotalColors(value)
9183
}
9284
93-
const handleCellClick = (value, rowValues, colValues) => {
85+
const handleCellClick = (value: any, rowValues: any, colValues: any) => {
9486
if (props.tableOptions?.clickCallback) {
95-
const filters = {}
87+
const filters = {} as any
9688
97-
colAttrs.value.forEach((attr, i) => {
89+
colAttrs.value.forEach((attr: string, i: number) => {
9890
if (colValues[i] !== undefined && colValues[i] !== null) {
9991
filters[attr] = colValues[i]
10092
}
10193
})
10294
103-
rowAttrs.value.forEach((attr, i) => {
95+
rowAttrs.value.forEach((attr: string, i: number) => {
10496
if (rowValues[i] !== undefined && rowValues[i] !== null) {
10597
filters[attr] = rowValues[i]
10698
}
10799
})
108100
109-
return (event) =>
110-
props.tableOptions.clickCallback(event, value, filters, pivotData.value)
101+
return (event: MouseEvent) =>
102+
props.tableOptions?.clickCallback(event, value, filters, pivotData.value)
111103
}
112104
return () => ({})
113105
}

0 commit comments

Comments
 (0)