Skip to content

Commit 903ff33

Browse files
committed
fix: properly focus password input on error without using $el or ref
- Removed `passwordInput` ref and reliance on `$el` to access the input field inside the `Input` component. - Fixed `TypeError` caused by calling `focus()` on the component instance. - Now focusing the password input via `formElement.password` in the form's submit error handler. - Used `nextTick` to ensure the DOM is updated before focusing. - This approach is cleaner, avoids reliance on component internals, and works reliably with shadcn-vue components.
1 parent 83f0d35 commit 903ff33

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

resources/js/components/DeleteUser.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { useForm } from '@inertiajs/vue3';
3-
import { ref } from 'vue';
3+
import { nextTick } from 'vue';
44
55
// Components
66
import HeadingSmall from '@/components/HeadingSmall.vue';
@@ -19,8 +19,6 @@ import {
1919
import { Input } from '@/components/ui/input';
2020
import { Label } from '@/components/ui/label';
2121
22-
const passwordInput = ref<HTMLInputElement | null>(null);
23-
2422
const form = useForm({
2523
password: '',
2624
});
@@ -31,7 +29,13 @@ const deleteUser = (e: Event) => {
3129
form.delete(route('profile.destroy'), {
3230
preserveScroll: true,
3331
onSuccess: () => closeModal(),
34-
onError: () => passwordInput.value?.focus(),
32+
onError: () => {
33+
nextTick(() => {
34+
const formElement = e.target as HTMLFormElement;
35+
const passwordInput = formElement.password as HTMLInputElement;
36+
passwordInput.focus();
37+
});
38+
},
3539
onFinish: () => form.reset(),
3640
});
3741
};
@@ -66,7 +70,7 @@ const closeModal = () => {
6670

6771
<div class="grid gap-2">
6872
<Label for="password" class="sr-only">Password</Label>
69-
<Input id="password" type="password" name="password" ref="passwordInput" v-model="form.password" placeholder="Password" />
73+
<Input id="password" type="password" name="password" v-model="form.password" placeholder="Password" />
7074
<InputError :message="form.errors.password" />
7175
</div>
7276

0 commit comments

Comments
 (0)