@tanstack/vue-table 8.20.1 → 8.20.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/vue-table",
3
- "version": "8.20.1",
3
+ "version": "8.20.3",
4
4
  "description": "Headless UI for building powerful tables & datagrids for Vue.",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -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'
7
2
  import {
8
3
  h,
9
4
  watchEffect,
@@ -12,25 +7,19 @@ import {
12
7
  isRef,
13
8
  unref,
14
9
  MaybeRef,
10
+ watch,
15
11
  } from 'vue'
16
12
  import { mergeProxy } from './merge-proxy'
17
13
 
18
14
  export * from '@tanstack/table-core'
19
15
 
20
- type TableOptionsWithReactiveData<TData extends RowData> = Omit<
16
+ export type TableOptionsWithReactiveData<TData extends RowData> = Omit<
21
17
  TableOptions<TData>,
22
18
  'data'
23
19
  > & {
24
20
  data: MaybeRef<TData[]>
25
21
  }
26
22
 
27
- type TableOptionsResolvedWithReactiveData<TData extends RowData> = Omit<
28
- TableOptionsResolved<TData>,
29
- 'data'
30
- > & {
31
- data: MaybeRef<TData[]>
32
- }
33
-
34
23
  export const FlexRender = defineComponent({
35
24
  props: ['render', 'props'],
36
25
  setup: (props: { render: any; props: any }) => {
@@ -47,33 +36,42 @@ export const FlexRender = defineComponent({
47
36
  },
48
37
  })
49
38
 
50
- export function useVueTable<TData extends RowData>(
39
+ function getOptionsWithReactiveData<TData extends RowData>(
51
40
  options: TableOptionsWithReactiveData<TData>
52
41
  ) {
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
+ },
56
+ getOptionsWithReactiveData(initialOptions)
57
+ )
58
+
59
+ const table = createTable<TData>(resolvedOptions)
60
+
61
+ // Add reactivity support
62
+ if (isRef(initialOptions.data)) {
63
+ watch(
64
+ initialOptions.data,
65
+ () => {
66
+ table.setState(prev => ({
67
+ ...prev,
68
+ data: unref(initialOptions.data),
69
+ }))
65
70
  },
66
- options
71
+ { immediate: true, deep: true }
67
72
  )
68
-
69
- // Add support for reactivity
70
- if (isRef(options.data)) {
71
- resolvedOptions.data = unref(options.data)
72
73
  }
73
74
 
74
- const table = createTable<TData>(
75
- resolvedOptions as TableOptionsResolved<TData>
76
- )
77
75
  // can't use `reactive` because update needs to be immutable
78
76
  const state = ref(table.initialState)
79
77
 
@@ -83,11 +81,11 @@ export function useVueTable<TData extends RowData>(
83
81
  get: (_, prop) => state.value[prop as keyof typeof state.value],
84
82
  })
85
83
 
86
- const newOptions = mergeProxy(prev, options, {
84
+ return mergeProxy(prev, getOptionsWithReactiveData(initialOptions), {
87
85
  // merge the initialState and `options.state`
88
86
  // create a new proxy on each `setOptions` call
89
87
  // and get the value from state on each property access
90
- state: mergeProxy(stateProxy, options.state ?? {}),
88
+ state: mergeProxy(stateProxy, initialOptions.state ?? {}),
91
89
  // Similarly, we'll maintain both our internal state and any user-provided
92
90
  // state.
93
91
  onStateChange: (updater: any) => {
@@ -97,19 +95,9 @@ export function useVueTable<TData extends RowData>(
97
95
  state.value = updater
98
96
  }
99
97
 
100
- options.onStateChange?.(updater)
98
+ initialOptions.onStateChange?.(updater)
101
99
  },
102
100
  })
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
113
101
  })
114
102
  })
115
103