Skip to content

Commit 7331506

Browse files
authored
fix: improve reactivity for data in Vue-adapter (#5694)
1 parent 2fea8c2 commit 7331506

File tree

1 file changed

+41
-47
lines changed

1 file changed

+41
-47
lines changed

packages/vue-table/src/index.ts

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
TableOptions,
3-
createTable,
4-
TableOptionsResolved,
5-
RowData,
6-
} from '@tanstack/table-core'
1+
import { TableOptions, createTable, RowData } from '@tanstack/table-core'
72
import {
83
h,
94
watchEffect,
@@ -12,6 +7,7 @@ import {
127
isRef,
138
unref,
149
MaybeRef,
10+
watch,
1511
} from 'vue'
1612
import { mergeProxy } from './merge-proxy'
1713

@@ -24,13 +20,6 @@ type TableOptionsWithReactiveData<TData extends RowData> = Omit<
2420
data: MaybeRef<TData[]>
2521
}
2622

27-
type TableOptionsResolvedWithReactiveData<TData extends RowData> = Omit<
28-
TableOptionsResolved<TData>,
29-
'data'
30-
> & {
31-
data: MaybeRef<TData[]>
32-
}
33-
3423
export const FlexRender = defineComponent({
3524
props: ['render', 'props'],
3625
setup: (props: { render: any; props: any }) => {
@@ -47,33 +36,48 @@ export const FlexRender = defineComponent({
4736
},
4837
})
4938

50-
export function useVueTable<TData extends RowData>(
39+
function getOptionsWithReactiveData<TData extends RowData>(
5140
options: TableOptionsWithReactiveData<TData>
5241
) {
53-
const resolvedOptions: TableOptionsResolvedWithReactiveData<TData> =
54-
mergeProxy(
55-
{
56-
state: {}, // Dummy state
57-
onStateChange: () => {}, // noop
58-
renderFallbackValue: null,
59-
mergeOptions(
60-
defaultOptions: TableOptions<TData>,
61-
options: TableOptions<TData>
62-
) {
63-
return mergeProxy(defaultOptions, options)
64-
},
42+
return mergeProxy(options, {
43+
data: unref(options.data),
44+
})
45+
}
46+
47+
export function useVueTable<TData extends RowData>(
48+
initialOptions: TableOptionsWithReactiveData<TData>
49+
) {
50+
const resolvedOptions = mergeProxy(
51+
{
52+
state: {}, // Dummy state
53+
onStateChange: () => {}, // noop
54+
renderFallbackValue: null,
55+
mergeOptions(
56+
defaultOptions: TableOptions<TData>,
57+
options: TableOptions<TData>
58+
) {
59+
return mergeProxy(defaultOptions, options)
6560
},
66-
options
67-
)
61+
},
62+
getOptionsWithReactiveData(initialOptions)
63+
)
64+
65+
const table = createTable<TData>(resolvedOptions)
6866

69-
// Add support for reactivity
70-
if (isRef(options.data)) {
71-
resolvedOptions.data = unref(options.data)
67+
// Add reactivity support
68+
if (isRef(initialOptions.data)) {
69+
watch(
70+
initialOptions.data,
71+
() => {
72+
table.setState(prev => ({
73+
...prev,
74+
data: unref(initialOptions.data),
75+
}))
76+
},
77+
{ immediate: true, deep: true }
78+
)
7279
}
7380

74-
const table = createTable<TData>(
75-
resolvedOptions as TableOptionsResolved<TData>
76-
)
7781
// can't use `reactive` because update needs to be immutable
7882
const state = ref(table.initialState)
7983

@@ -83,11 +87,11 @@ export function useVueTable<TData extends RowData>(
8387
get: (_, prop) => state.value[prop as keyof typeof state.value],
8488
})
8589

86-
const newOptions = mergeProxy(prev, options, {
90+
return mergeProxy(prev, getOptionsWithReactiveData(initialOptions), {
8791
// merge the initialState and `options.state`
8892
// create a new proxy on each `setOptions` call
8993
// and get the value from state on each property access
90-
state: mergeProxy(stateProxy, options.state ?? {}),
94+
state: mergeProxy(stateProxy, initialOptions.state ?? {}),
9195
// Similarly, we'll maintain both our internal state and any user-provided
9296
// state.
9397
onStateChange: (updater: any) => {
@@ -97,19 +101,9 @@ export function useVueTable<TData extends RowData>(
97101
state.value = updater
98102
}
99103

100-
options.onStateChange?.(updater)
104+
initialOptions.onStateChange?.(updater)
101105
},
102106
})
103-
104-
// Add support for reactivity
105-
if (isRef(options.data)) {
106-
return {
107-
...newOptions,
108-
data: unref(options.data),
109-
}
110-
}
111-
112-
return newOptions
113107
})
114108
})
115109

0 commit comments

Comments
 (0)