Skip to content

Commit fa0a3db

Browse files
Merge pull request #139 from vue-pivottable/feature/usePropsState-ts
feat(composables): usePropsState TypeScript 마이그레이션
2 parents b7bd0ab + b1b833b commit fa0a3db

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

src/composables/usePropsState.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { computed, reactive, ComputedRef, UnwrapRef } from 'vue'
2+
3+
// 초기 props 타입 정의 (실제 프로젝트 상황에 맞게 확장 가능)
4+
export interface UsePropsStateProps {
5+
languagePack: Record<string, { localeStrings: Record<string, string> }>
6+
locale: string
7+
valueFilter?: Record<string, any>
8+
rendererName?: string
9+
heatmapMode?: string
10+
aggregatorName?: string
11+
rowOrder?: string
12+
colOrder?: string
13+
vals?: any[]
14+
[key: string]: any
15+
}
16+
17+
export interface UsePropsStateReturn<T extends UsePropsStateProps> {
18+
state: UnwrapRef<T>
19+
localeStrings: ComputedRef<Record<string, string>>
20+
updateState: (key: keyof T, value: any) => void
21+
updateMultiple: (updates: Partial<T>) => void
22+
onUpdateValueFilter: (payload: { key: string; value: any }) => void
23+
onUpdateRendererName: (rendererName: string) => void
24+
onUpdateAggregatorName: (aggregatorName: string) => void
25+
onUpdateRowOrder: (rowOrder: string) => void
26+
onUpdateColOrder: (colOrder: string) => void
27+
onUpdateVals: (vals: any[]) => void
28+
onDraggedAttribute: (payload: { key: keyof T; value: any }) => void
29+
}
30+
31+
export function usePropsState<T extends UsePropsStateProps> (
32+
initialProps: T
33+
): UsePropsStateReturn<T> {
34+
const state = reactive({
35+
...initialProps
36+
}) as UnwrapRef<T>
37+
38+
const localeStrings = computed<Record<string, string>>(
39+
() => initialProps.languagePack[initialProps.locale].localeStrings
40+
)
41+
42+
const updateState = (key: keyof T, value: any) => {
43+
if (key in state) {
44+
(state as any)[key] = value
45+
}
46+
}
47+
48+
const updateMultiple = (updates: Partial<T>) => {
49+
Object.entries(updates).forEach(([key, value]) => {
50+
if (key in state) {
51+
(state as any)[key] = value
52+
}
53+
})
54+
}
55+
56+
const onUpdateValueFilter = ({ key, value }: { key: string; value: any }) => {
57+
updateState('valueFilter' as keyof T, {
58+
...(state.valueFilter || {}),
59+
[key]: value
60+
})
61+
}
62+
63+
const onUpdateRendererName = (rendererName: string) => {
64+
updateState('rendererName' as keyof T, rendererName)
65+
if (rendererName === 'Table Heatmap') {
66+
updateState('heatmapMode' as keyof T, 'full')
67+
} else if (rendererName === 'Table Row Heatmap') {
68+
updateState('heatmapMode' as keyof T, 'row')
69+
} else if (rendererName === 'Table Col Heatmap') {
70+
updateState('heatmapMode' as keyof T, 'col')
71+
} else {
72+
updateState('heatmapMode' as keyof T, '')
73+
}
74+
}
75+
76+
const onUpdateAggregatorName = (aggregatorName: string) => {
77+
updateState('aggregatorName' as keyof T, aggregatorName)
78+
}
79+
const onUpdateRowOrder = (rowOrder: string) => {
80+
updateState('rowOrder' as keyof T, rowOrder)
81+
}
82+
const onUpdateColOrder = (colOrder: string) => {
83+
updateState('colOrder' as keyof T, colOrder)
84+
}
85+
const onUpdateVals = (vals: any[]) => {
86+
updateState('vals' as keyof T, vals)
87+
}
88+
const onDraggedAttribute = ({ key, value }: { key: keyof T; value: any }) => {
89+
updateState(key, value)
90+
}
91+
92+
return {
93+
state,
94+
localeStrings,
95+
updateState,
96+
updateMultiple,
97+
onUpdateValueFilter,
98+
onUpdateRendererName,
99+
onUpdateAggregatorName,
100+
onUpdateRowOrder,
101+
onUpdateColOrder,
102+
onUpdateVals,
103+
onDraggedAttribute
104+
}
105+
}

0 commit comments

Comments
 (0)