From 03f7e81313e21c38988c47841756c37dc61bb36d Mon Sep 17 00:00:00 2001 From: Massedil Date: Wed, 26 Jun 2024 09:55:25 +0000 Subject: [PATCH] tags are correctly restored on the taginput (cherry picked from commit d88bae853613ceb713fc08d3143b411ff6a724b1) --- src/components/Event/TagInput.vue | 50 +++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/components/Event/TagInput.vue b/src/components/Event/TagInput.vue index dad5c7b9c..b8702c410 100644 --- a/src/components/Event/TagInput.vue +++ b/src/components/Event/TagInput.vue @@ -15,7 +15,8 @@ { const { load: fetchTags } = useFetchTags(); +initTagsStringsValue(); + const getFilteredTags = async (newText: string): Promise => { text.value = newText; const res = await fetchTags( @@ -91,11 +94,16 @@ const filteredTags = computed((): ITag[] => { ); }); -watch(props.modelValue, (newValue, oldValue) => { - if (newValue != oldValue) { - tagsStrings.value = propsValue.value.map((tag: ITag) => tag.title); - } -}); +// TODO It seems that '@update:modelValue="updateTags"' does not works anymore... +// so temporarily call the function updateTags() at remove and add tag event +// https://github.com/oruga-ui/oruga/issues/967 +function remove() { + updateTags(tagsStrings.value); +} + +function add() { + updateTags(tagsStrings.value); +} const updateTags = (newTagsStrings: string[]) => { const tagEntities = newTagsStrings.map((tag: string | ITag) => { @@ -106,4 +114,34 @@ const updateTags = (newTagsStrings: string[]) => { }); emit("update:modelValue", tagEntities); }; + +function isArraysEquals(array1: string[], array2: string[]) { + if (array1.length !== array2.length) { + return false; + } + + for (let i = 0; i < array1.length; i++) { + if (array1[i] !== array2[i]) { + return false; + } + } + + return true; +} + +function initTagsStringsValue() { + // This is useful when tag data is already cached from the API during navigation inside the app + tagsStrings.value = propsValue.value.map((tag: ITag) => tag.title); + + // This watch() function is useful when tag data loads directly from the API upon page load + watch(propsValue, () => { + const newTagsStrings = propsValue.value.map((tag: ITag) => tag.title); + + // Changing tagsStrings.value triggers updateTags(), updateTags() triggers this watch() function again. + // To stop the loop, edit tagsStrings.value only if it has changed ! + if (!isArraysEquals(tagsStrings.value, newTagsStrings)) { + tagsStrings.value = newTagsStrings; + } + }); +}