Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue): make normal JS-getters work, make reactivity a non-breaking change #5702

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 51 additions & 23 deletions packages/vue-table/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { TableOptions, createTable, RowData } from '@tanstack/table-core'
import {
TableOptions,
createTable,
RowData,
TableOptionsResolved,
} from '@tanstack/table-core'
import {
h,
watchEffect,
Expand All @@ -8,6 +13,7 @@ import {
unref,
MaybeRef,
watch,
shallowRef,
} from 'vue'
import { mergeProxy } from './merge-proxy'

Expand Down Expand Up @@ -47,28 +53,44 @@ function getOptionsWithReactiveData<TData extends RowData>(
export function useVueTable<TData extends RowData>(
initialOptions: TableOptionsWithReactiveData<TData>
) {
const IS_REACTIVE = isRef(initialOptions.data)

const resolvedOptions = mergeProxy(
{
state: {}, // Dummy state
onStateChange: () => {}, // noop
renderFallbackValue: null,
mergeOptions(
defaultOptions: TableOptions<TData>,
options: TableOptions<TData>
) {
return IS_REACTIVE
? {
...defaultOptions,
...options,
}
: mergeProxy(defaultOptions, options)
},
},
getOptionsWithReactiveData(initialOptions)
IS_REACTIVE ? getOptionsWithReactiveData(initialOptions) : initialOptions
)

const table = createTable<TData>(resolvedOptions)
const table = createTable<TData>(
resolvedOptions as TableOptionsResolved<TData>
)

// Add reactivity support
if (isRef(initialOptions.data)) {
if (IS_REACTIVE) {
const dataRef = shallowRef(initialOptions.data)
watch(
initialOptions.data,
dataRef,
() => {
table.setState(prev => ({
...prev,
data: unref(initialOptions.data),
data: dataRef.value,
}))
},
{ immediate: true, deep: true }
{ immediate: true }
)
}

Expand All @@ -81,23 +103,29 @@ export function useVueTable<TData extends RowData>(
get: (_, prop) => state.value[prop as keyof typeof state.value],
})

return mergeProxy(prev, getOptionsWithReactiveData(initialOptions), {
// merge the initialState and `options.state`
// create a new proxy on each `setOptions` call
// and get the value from state on each property access
state: mergeProxy(stateProxy, initialOptions.state ?? {}),
// Similarly, we'll maintain both our internal state and any user-provided
// state.
onStateChange: (updater: any) => {
if (updater instanceof Function) {
state.value = updater(state.value)
} else {
state.value = updater
}
return mergeProxy(
prev,
IS_REACTIVE
? getOptionsWithReactiveData(initialOptions)
: initialOptions,
{
// merge the initialState and `options.state`
// create a new proxy on each `setOptions` call
// and get the value from state on each property access
state: mergeProxy(stateProxy, initialOptions.state ?? {}),
// Similarly, we'll maintain both our internal state and any user-provided
// state.
onStateChange: (updater: any) => {
if (updater instanceof Function) {
state.value = updater(state.value)
} else {
state.value = updater
}

initialOptions.onStateChange?.(updater)
},
})
initialOptions.onStateChange?.(updater)
},
}
)
})
})

Expand Down