diff --git a/src/formkit/utils/focus.ts b/src/formkit/utils/focus.ts new file mode 100644 index 000000000..f18e4a8b3 --- /dev/null +++ b/src/formkit/utils/focus.ts @@ -0,0 +1,9 @@ +export function setFocus(id: string) { + const inputElement = document.getElementById(id); + if (inputElement instanceof HTMLInputElement) { + const timer = setTimeout(() => { + inputElement?.focus(); + clearTimeout(timer); + }, 0); + } +} diff --git a/src/modules/contents/attachments/components/AttachmentGroupEditingModal.vue b/src/modules/contents/attachments/components/AttachmentGroupEditingModal.vue index 94c76a11f..d5b914ee9 100644 --- a/src/modules/contents/attachments/components/AttachmentGroupEditingModal.vue +++ b/src/modules/contents/attachments/components/AttachmentGroupEditingModal.vue @@ -7,6 +7,7 @@ import cloneDeep from "lodash.clonedeep"; import { apiClient } from "@halo-dev/admin-shared"; import { reset, submitForm } from "@formkit/core"; import { useMagicKeys } from "@vueuse/core"; +import { setFocus } from "@/formkit/utils/focus"; const props = withDefaults( defineProps<{ @@ -95,7 +96,9 @@ watchEffect(() => { watch( () => props.visible, (visible) => { - if (!visible) { + if (visible) { + setFocus("displayNameInput"); + } else { handleResetForm(); } } @@ -119,8 +122,14 @@ watch( :width="500" @update:visible="onVisibleChange" > - + { - + - + { } }; -watch(props, (newVal) => { - const { Command_Enter } = useMagicKeys(); - let keyboardWatcher; - if (newVal.visible) { - keyboardWatcher = watch(Command_Enter, (v) => { - if (v) { - submitForm("menu-form"); - } - }); - } else { - keyboardWatcher?.unwatch(); - } - - if (newVal.visible && props.menu) { - formState.value = cloneDeep(props.menu); - return; - } +const handleResetForm = () => { formState.value = cloneDeep(initialFormState); formState.value.metadata.name = uuid(); reset("menu-form"); +}; + +const { Command_Enter } = useMagicKeys(); + +watchEffect(() => { + if (Command_Enter.value && props.visible) { + submitForm("menu-form"); + } }); + +watch( + () => props.visible, + (visible) => { + if (visible) { + setFocus("displayNameInput"); + } else { + handleResetForm(); + } + } +); + +watch( + () => props.menu, + (menu) => { + if (menu) { + formState.value = cloneDeep(menu); + } else { + handleResetForm(); + } + } +);