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`] = `
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Username must be unique
+
Username must be unique
"
`;
@@ -91,7 +91,7 @@ exports[`FormField > renders with help slot correctly 1`] = `
"
`;
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`] = `
"
`;