From ce93d9813be75fc3c136f2fa8102131f194ec1c7 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Fri, 9 Sep 2022 21:48:07 +0800 Subject: [PATCH] perf: auto focus when opening editing forms --- src/formkit/utils/focus.ts | 9 ++ .../AttachmentGroupEditingModal.vue | 13 +- .../components/AttachmentGroupList.vue | 2 +- .../AttachmentPolicyEditingModal.vue | 10 +- .../components/CategoryEditingModal.vue | 13 +- .../posts/tags/components/PostTag.vue | 2 +- .../posts/tags/components/TagEditingModal.vue | 13 +- .../menus/components/MenuEditingModal.vue | 53 ++++++--- .../menus/components/MenuItemEditingModal.vue | 13 +- .../roles/components/RoleEditingModal.vue | 8 +- src/modules/system/settings/module.ts | 3 +- src/modules/system/users/UserList.vue | 2 - .../users/components/UserEditingModal.vue | 112 +++++++++--------- .../components/UserPasswordChangeModal.vue | 88 +++++++++----- 14 files changed, 216 insertions(+), 125 deletions(-) create mode 100644 src/formkit/utils/focus.ts diff --git a/src/formkit/utils/focus.ts b/src/formkit/utils/focus.ts new file mode 100644 index 00000000..f18e4a8b --- /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 94c76a11..d5b914ee 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(); + } + } +);