fix running useFieldTree to often (#7543)

This commit is contained in:
Nitwel
2021-08-23 14:31:34 +02:00
committed by GitHub
parent 44a9ac013a
commit b36df28603

View File

@@ -3,7 +3,7 @@ import { Relation } from '@/types';
import { Field } from '@directus/shared/types';
import { getRelationType } from '@/utils/get-relation-type';
import { cloneDeep, orderBy } from 'lodash';
import { computed, Ref, ComputedRef } from 'vue';
import { Ref, ref, watch } from 'vue';
type FieldOption = { name: string; field: string; key: string; children?: FieldOption[]; group?: string };
@@ -14,15 +14,24 @@ export default function useFieldTree(
inject?: Ref<{ fields: Field[]; relations: Relation[] } | null>,
filter: (field: Field) => boolean = () => true,
depth = 3
): { tree: ComputedRef<FieldOption[]> } {
): { tree: Ref<FieldOption[]> } {
const fieldsStore = useFieldsStore();
const collectionsStore = useCollectionsStore();
const relationsStore = useRelationsStore();
const tree = computed(() => {
if (!collection.value) return [];
return parseLevel(collection.value, null);
});
const tree = ref<FieldOption[]>([]);
watch(
collection,
(newCollection) => {
if (!newCollection) {
tree.value = [];
return;
}
tree.value = parseLevel(newCollection, null);
},
{ immediate: true }
);
return { tree };