From c64c4cdea0bef3321b361455e43b7ff1422b0b2a Mon Sep 17 00:00:00 2001 From: Thilo Hettmer <47103966+ThiloHettmer@users.noreply.github.com> Date: Wed, 16 Jul 2025 21:55:57 +0200 Subject: [PATCH] fix(FormField): resolve minor accessibility and rendering issues (#4515) Co-authored-by: Romain Hamel --- src/runtime/components/FormField.vue | 2 +- src/runtime/composables/useFormField.ts | 9 +++- test/components/FormField.spec.ts | 20 ++++++++ .../__snapshots__/Form-vue.spec.ts.snap | 48 +++++++++---------- .../__snapshots__/Form.spec.ts.snap | 48 +++++++++---------- .../__snapshots__/FormField-vue.spec.ts.snap | 4 +- .../__snapshots__/FormField.spec.ts.snap | 4 +- 7 files changed, 80 insertions(+), 55 deletions(-) diff --git a/src/runtime/components/FormField.vue b/src/runtime/components/FormField.vue index e8c719c5..2587871b 100644 --- a/src/runtime/components/FormField.vue +++ b/src/runtime/components/FormField.vue @@ -121,7 +121,7 @@ provide(formFieldInjectionKey, computed(() => ({ {{ error }} -
+
{{ help }} diff --git a/src/runtime/composables/useFormField.ts b/src/runtime/composables/useFormField.ts index 7a5328ff..404c1e29 100644 --- a/src/runtime/composables/useFormField.ts +++ b/src/runtime/composables/useFormField.ts @@ -89,10 +89,15 @@ export function useFormField(props?: Props, opts?: { bind?: boolean, defer .filter(type => formField?.value?.[type]) .map(type => `${formField?.value.ariaId}-${type}`) || [] - return { - 'aria-describedby': descriptiveAttrs.join(' '), + const attrs: Record = { 'aria-invalid': !!formField?.value.error } + + if (descriptiveAttrs.length > 0) { + attrs['aria-describedby'] = descriptiveAttrs.join(' ') + } + + return attrs }) } } diff --git a/test/components/FormField.spec.ts b/test/components/FormField.spec.ts index 3bb40189..a3c4ac25 100644 --- a/test/components/FormField.spec.ts +++ b/test/components/FormField.spec.ts @@ -157,5 +157,25 @@ describe('FormField', () => { const attr = wrapper.find('[aria-invalid=true]') expect(attr.exists()).toBe(true) }) + + test('renders id for aria describedby when help prop is provided', async () => { + const wrapper = await renderFormField({ + props: { help: 'somehelp' }, + inputComponent + }) + + const attr = wrapper.find('[id=v-0-0-help]') + expect(attr.exists()).toBe(true) + }) + + test('renders no id for aria describedby when no help prop is provided', async () => { + const wrapper = await renderFormField({ + props: { label: 'Username', description: 'Enter your username' }, + inputComponent + }) + + const attr = wrapper.find('[id=v-0-0-help]') + expect(attr.exists()).toBe(false) + }) }) }) diff --git a/test/components/__snapshots__/Form-vue.spec.ts.snap b/test/components/__snapshots__/Form-vue.spec.ts.snap index 6701093d..bd9a2a4d 100644 --- a/test/components/__snapshots__/Form-vue.spec.ts.snap +++ b/test/components/__snapshots__/Form-vue.spec.ts.snap @@ -8,7 +8,7 @@ exports[`Form > custom validation works > with error 1`] = `
-
+
@@ -21,7 +21,7 @@ exports[`Form > custom validation works > with error 1`] = `
-
+
@@ -39,7 +39,7 @@ exports[`Form > custom validation works > without error 1`] = `
-
+
@@ -52,7 +52,7 @@ exports[`Form > custom validation works > without error 1`] = `
-
+
@@ -70,7 +70,7 @@ exports[`Form > joi validation works > with error 1`] = `
-
+
@@ -83,7 +83,7 @@ exports[`Form > joi validation works > with error 1`] = `
-
+
@@ -101,7 +101,7 @@ exports[`Form > joi validation works > without error 1`] = `
-
+
@@ -114,7 +114,7 @@ exports[`Form > joi validation works > without error 1`] = `
-
+
@@ -136,7 +136,7 @@ exports[`Form > superstruct validation works > with error 1`] = `
-
+
@@ -149,7 +149,7 @@ exports[`Form > superstruct validation works > with error 1`] = `
-
+
@@ -167,7 +167,7 @@ exports[`Form > superstruct validation works > without error 1`] = `
-
+
@@ -180,7 +180,7 @@ exports[`Form > superstruct validation works > without error 1`] = `
-
+
@@ -198,7 +198,7 @@ exports[`Form > valibot validation works > with error 1`] = `
-
+
@@ -211,7 +211,7 @@ exports[`Form > valibot validation works > with error 1`] = `
-
+
@@ -229,7 +229,7 @@ exports[`Form > valibot validation works > without error 1`] = `
-
+
@@ -242,7 +242,7 @@ exports[`Form > valibot validation works > without error 1`] = `
-
+
@@ -260,7 +260,7 @@ exports[`Form > yup validation works > with error 1`] = `
-
+
@@ -273,7 +273,7 @@ exports[`Form > yup validation works > with error 1`] = `
-
+
@@ -291,7 +291,7 @@ exports[`Form > yup validation works > without error 1`] = `
-
+
@@ -304,7 +304,7 @@ exports[`Form > yup validation works > without error 1`] = `
-
+
@@ -322,7 +322,7 @@ exports[`Form > zod validation works > with error 1`] = `
-
+
@@ -335,7 +335,7 @@ exports[`Form > zod validation works > with error 1`] = `
-
+
@@ -353,7 +353,7 @@ exports[`Form > zod validation works > without error 1`] = `
-
+
@@ -366,7 +366,7 @@ exports[`Form > zod validation works > without error 1`] = `
-
+
diff --git a/test/components/__snapshots__/Form.spec.ts.snap b/test/components/__snapshots__/Form.spec.ts.snap index 0a0e2677..d7c7f898 100644 --- a/test/components/__snapshots__/Form.spec.ts.snap +++ b/test/components/__snapshots__/Form.spec.ts.snap @@ -8,7 +8,7 @@ exports[`Form > custom validation works > with error 1`] = `
-
+
@@ -21,7 +21,7 @@ exports[`Form > custom validation works > with error 1`] = `
-
+
@@ -39,7 +39,7 @@ exports[`Form > custom validation works > without error 1`] = `
-
+
@@ -52,7 +52,7 @@ exports[`Form > custom validation works > without error 1`] = `
-
+
@@ -70,7 +70,7 @@ exports[`Form > joi validation works > with error 1`] = `
-
+
@@ -83,7 +83,7 @@ exports[`Form > joi validation works > with error 1`] = `
-
+
@@ -101,7 +101,7 @@ exports[`Form > joi validation works > without error 1`] = `
-
+
@@ -114,7 +114,7 @@ exports[`Form > joi validation works > without error 1`] = `
-
+
@@ -136,7 +136,7 @@ exports[`Form > superstruct validation works > with error 1`] = `
-
+
@@ -149,7 +149,7 @@ exports[`Form > superstruct validation works > with error 1`] = `
-
+
@@ -167,7 +167,7 @@ exports[`Form > superstruct validation works > without error 1`] = `
-
+
@@ -180,7 +180,7 @@ exports[`Form > superstruct validation works > without error 1`] = `
-
+
@@ -198,7 +198,7 @@ exports[`Form > valibot validation works > with error 1`] = `
-
+
@@ -211,7 +211,7 @@ exports[`Form > valibot validation works > with error 1`] = `
-
+
@@ -229,7 +229,7 @@ exports[`Form > valibot validation works > without error 1`] = `
-
+
@@ -242,7 +242,7 @@ exports[`Form > valibot validation works > without error 1`] = `
-
+
@@ -260,7 +260,7 @@ exports[`Form > yup validation works > with error 1`] = `
-
+
@@ -273,7 +273,7 @@ exports[`Form > yup validation works > with error 1`] = `
-
+
@@ -291,7 +291,7 @@ exports[`Form > yup validation works > without error 1`] = `
-
+
@@ -304,7 +304,7 @@ exports[`Form > yup validation works > without error 1`] = `
-
+
@@ -322,7 +322,7 @@ exports[`Form > zod validation works > with error 1`] = `
-
+
@@ -335,7 +335,7 @@ exports[`Form > zod validation works > with error 1`] = `
-
+
@@ -353,7 +353,7 @@ exports[`Form > zod validation works > without error 1`] = `
-
+
@@ -366,7 +366,7 @@ exports[`Form > zod validation works > without error 1`] = `
-
+
diff --git a/test/components/__snapshots__/FormField-vue.spec.ts.snap b/test/components/__snapshots__/FormField-vue.spec.ts.snap index bee8f680..be708f59 100644 --- a/test/components/__snapshots__/FormField-vue.spec.ts.snap +++ b/test/components/__snapshots__/FormField-vue.spec.ts.snap @@ -79,7 +79,7 @@ exports[`FormField > renders with help correctly 1`] = `
-
Username must be unique
+
Username must be unique
" `; @@ -91,7 +91,7 @@ exports[`FormField > renders with help slot correctly 1`] = `
-
Help slot
+
Help slot
" `; diff --git a/test/components/__snapshots__/FormField.spec.ts.snap b/test/components/__snapshots__/FormField.spec.ts.snap index bee8f680..be708f59 100644 --- a/test/components/__snapshots__/FormField.spec.ts.snap +++ b/test/components/__snapshots__/FormField.spec.ts.snap @@ -79,7 +79,7 @@ exports[`FormField > renders with help correctly 1`] = `
-
Username must be unique
+
Username must be unique
" `; @@ -91,7 +91,7 @@ exports[`FormField > renders with help slot correctly 1`] = `
-
Help slot
+
Help slot
" `;