refactor: improve code base of post tag-related (#5959)

#### What type of PR is this?

/area ui
/kind improvement
/milestone 2.16.x

#### What this PR does / why we need it:

优化文章标签的表单组件代码。

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/5972/head
Ryan Wang 2024-05-22 17:34:46 +08:00 committed by GitHub
parent bbc5c979b7
commit ea98ae5add
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 51 deletions

View File

@ -27,7 +27,7 @@ import { useI18n } from "vue-i18n";
const { t } = useI18n();
const editingModal = ref(false);
const selectedTag = ref<Tag | null>(null);
const selectedTag = ref<Tag>();
const selectedTagNames = ref<string[]>([]);
const checkedAll = ref(false);
@ -66,7 +66,7 @@ const {
sort: selectedSort,
});
const handleOpenEditingModal = (tag: Tag | null) => {
const handleOpenEditingModal = (tag?: Tag) => {
selectedTag.value = tag;
editingModal.value = true;
};
@ -132,8 +132,9 @@ const handleSelectNext = async () => {
};
const onEditingModalClose = () => {
selectedTag.value = null;
selectedTag.value = undefined;
queryName.value = null;
editingModal.value = false;
handleFetchTags();
};
@ -157,7 +158,7 @@ watch(selectedTagNames, (newVal) => {
</script>
<template>
<TagEditingModal
v-model:visible="editingModal"
v-if="editingModal"
:tag="selectedTag"
@close="onEditingModalClose"
@next="handleSelectNext"

View File

@ -19,27 +19,24 @@ import SubmitButton from "@/components/button/SubmitButton.vue";
import type { Tag } from "@halo-dev/api-client";
// libs
import { cloneDeep } from "lodash-es";
import { reset } from "@formkit/core";
import { setFocus } from "@/formkit/utils/focus";
import AnnotationsForm from "@/components/form/AnnotationsForm.vue";
import useSlugify from "@console/composables/use-slugify";
import { useI18n } from "vue-i18n";
import { FormType } from "@/types/slug";
import { onMounted } from "vue";
import { toRaw } from "vue";
const props = withDefaults(
defineProps<{
visible: boolean;
tag: Tag | null;
tag?: Tag;
}>(),
{
visible: false,
tag: null,
tag: undefined,
}
);
const emit = defineEmits<{
(event: "update:visible", visible: boolean): void;
(event: "close"): void;
(event: "previous"): void;
(event: "next"): void;
@ -47,7 +44,7 @@ const emit = defineEmits<{
const { t } = useI18n();
const initialFormState: Tag = {
const formState = ref<Tag>({
spec: {
displayName: "",
slug: "",
@ -60,14 +57,13 @@ const initialFormState: Tag = {
name: "",
generateName: "tag-",
},
};
});
const modal = ref();
const formState = ref<Tag>(cloneDeep(initialFormState));
const saving = ref(false);
const isUpdateMode = computed(() => {
return !!formState.value.metadata.creationTimestamp;
});
const isUpdateMode = computed(() => !!props.tag);
const modalTitle = computed(() => {
return isUpdateMode.value
@ -104,7 +100,8 @@ const handleSaveTag = async () => {
tag: formState.value,
});
}
onVisibleChange(false);
modal.value.close();
Toast.success(t("core.common.toast.save_success"));
} catch (e) {
@ -114,37 +111,19 @@ const handleSaveTag = async () => {
}
};
const onVisibleChange = (visible: boolean) => {
emit("update:visible", visible);
if (!visible) {
emit("close");
}
};
const handleResetForm = () => {
formState.value = cloneDeep(initialFormState);
reset("tag-form");
};
watch(
() => props.visible,
(visible) => {
if (visible) {
setFocus("displayNameInput");
} else {
handleResetForm();
}
}
);
onMounted(() => {
setFocus("displayNameInput");
});
watch(
() => props.tag,
(tag) => {
if (tag) {
formState.value = cloneDeep(tag);
} else {
handleResetForm();
formState.value = toRaw(tag);
}
},
{
immediate: true,
}
);
@ -164,12 +143,7 @@ const { handleGenerateSlug } = useSlugify(
);
</script>
<template>
<VModal
:title="modalTitle"
:visible="visible"
:width="700"
@update:visible="onVisibleChange"
>
<VModal ref="modal" :title="modalTitle" :width="700" @close="emit('close')">
<template #actions>
<span @click="emit('previous')">
<IconArrowLeft />
@ -278,14 +252,13 @@ const { handleGenerateSlug } = useSlugify(
<template #footer>
<VSpace>
<SubmitButton
v-if="visible"
:loading="saving"
type="secondary"
:text="$t('core.common.buttons.submit')"
@submit="$formkit.submit('tag-form')"
>
</SubmitButton>
<VButton @click="onVisibleChange(false)">
<VButton @click="modal.close()">
{{ $t("core.common.buttons.cancel_and_shortcut") }}
</VButton>
</VSpace>