mirror of
https://github.com/directus/directus.git
synced 2026-01-29 19:58:08 -05:00
Fix default sort value (#8841)
By allowing for default values in synced refs to be a function
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user