From da14ca8daed04ef07607799b9d5a14516aef24bb Mon Sep 17 00:00:00 2001 From: Rijk van Zanten Date: Fri, 15 Oct 2021 17:50:36 -0400 Subject: [PATCH] Fix default sort value (#8841) By allowing for default values in synced refs to be a function --- app/src/layouts/tabular/index.ts | 5 ++++- app/src/utils/sync-ref-property.ts | 13 +++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/layouts/tabular/index.ts b/app/src/layouts/tabular/index.ts index cf3aa988be..79bf9678ee 100644 --- a/app/src/layouts/tabular/index.ts +++ b/app/src/layouts/tabular/index.ts @@ -142,7 +142,10 @@ export default defineLayout({ function useItemOptions() { const page = syncRefProperty(layoutQuery, 'page', 1); const limit = syncRefProperty(layoutQuery, 'limit', 25); - const sort = syncRefProperty(layoutQuery, 'sort', primaryKeyField.value ? [primaryKeyField.value?.field] : []); + const sort = syncRefProperty(layoutQuery, 'sort', () => + primaryKeyField.value ? [primaryKeyField.value?.field] : [] + ); + const fields = syncRefProperty( layoutQuery, 'fields', diff --git a/app/src/utils/sync-ref-property.ts b/app/src/utils/sync-ref-property.ts index 40b547461a..5534258984 100644 --- a/app/src/utils/sync-ref-property.ts +++ b/app/src/utils/sync-ref-property.ts @@ -1,8 +1,17 @@ +import { isFunction } from 'lodash'; import { computed, Ref } from 'vue'; -export function syncRefProperty(ref: Ref, key: T, defaultValue: R[T]) { +export function syncRefProperty(ref: Ref, key: T, defaultValue: R[T] | (() => R[T])) { return computed({ - get: () => ref.value?.[key] ?? defaultValue, + get: () => { + if (ref.value?.[key]) { + return ref.value[key]; + } + + if (isFunction(defaultValue)) return defaultValue(); + + return defaultValue; + }, set: (value: R[T]) => { ref.value = Object.assign({}, ref.value, { [key]: value }) as R; },