Fix default sort value (#8841)

By allowing for default values in synced refs to be a function
This commit is contained in:
Rijk van Zanten
2021-10-15 17:50:36 -04:00
committed by GitHub
parent a18fd90731
commit da14ca8dae
2 changed files with 15 additions and 3 deletions

View File

@@ -142,7 +142,10 @@ export default defineLayout<LayoutOptions, LayoutQuery>({
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',

View File

@@ -1,8 +1,17 @@
import { isFunction } from 'lodash';
import { computed, Ref } from 'vue';
export function syncRefProperty<R, T extends keyof R>(ref: Ref<R>, key: T, defaultValue: R[T]) {
export function syncRefProperty<R, T extends keyof R>(ref: Ref<R>, key: T, defaultValue: R[T] | (() => R[T])) {
return computed<R[T]>({
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;
},