Skip to content

Commit c64c4cd

Browse files
ThiloHettmerromhml
andauthored
fix(FormField): resolve minor accessibility and rendering issues (#4515)
Co-authored-by: Romain Hamel <[email protected]>
1 parent 8b42365 commit c64c4cd

File tree

7 files changed

+80
-55
lines changed

7 files changed

+80
-55
lines changed

src/runtime/components/FormField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ provide(formFieldInjectionKey, computed(() => ({
121121
{{ error }}
122122
</slot>
123123
</div>
124-
<div v-else-if="help || !!slots.help" :class="ui.help({ class: props.ui?.help })">
124+
<div v-else-if="help || !!slots.help" :id="`${ariaId}-help`" :class="ui.help({ class: props.ui?.help })">
125125
<slot name="help" :help="help">
126126
{{ help }}
127127
</slot>

src/runtime/composables/useFormField.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ export function useFormField<T>(props?: Props<T>, opts?: { bind?: boolean, defer
8989
.filter(type => formField?.value?.[type])
9090
.map(type => `${formField?.value.ariaId}-${type}`) || []
9191

92-
return {
93-
'aria-describedby': descriptiveAttrs.join(' '),
92+
const attrs: Record<string, any> = {
9493
'aria-invalid': !!formField?.value.error
9594
}
95+
96+
if (descriptiveAttrs.length > 0) {
97+
attrs['aria-describedby'] = descriptiveAttrs.join(' ')
98+
}
99+
100+
return attrs
96101
})
97102
}
98103
}

test/components/FormField.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,25 @@ describe('FormField', () => {
157157
const attr = wrapper.find('[aria-invalid=true]')
158158
expect(attr.exists()).toBe(true)
159159
})
160+
161+
test('renders id for aria describedby when help prop is provided', async () => {
162+
const wrapper = await renderFormField({
163+
props: { help: 'somehelp' },
164+
inputComponent
165+
})
166+
167+
const attr = wrapper.find('[id=v-0-0-help]')
168+
expect(attr.exists()).toBe(true)
169+
})
170+
171+
test('renders no id for aria describedby when no help prop is provided', async () => {
172+
const wrapper = await renderFormField({
173+
props: { label: 'Username', description: 'Enter your username' },
174+
inputComponent
175+
})
176+
177+
const attr = wrapper.find('[id=v-0-0-help]')
178+
expect(attr.exists()).toBe(false)
179+
})
160180
})
161181
})

test/components/__snapshots__/Form-vue.spec.ts.snap

Lines changed: 24 additions & 24 deletions
Large diffs are not rendered by default.

test/components/__snapshots__/Form.spec.ts.snap

Lines changed: 24 additions & 24 deletions
Large diffs are not rendered by default.

test/components/__snapshots__/FormField-vue.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ exports[`FormField > renders with help correctly 1`] = `
7979
<!--v-if-->
8080
</div>
8181
<div class="">
82-
<div class="mt-2 text-muted">Username must be unique</div>
82+
<div id="v-0-0-help" class="mt-2 text-muted">Username must be unique</div>
8383
</div>
8484
</div>"
8585
`;
@@ -91,7 +91,7 @@ exports[`FormField > renders with help slot correctly 1`] = `
9191
<!--v-if-->
9292
</div>
9393
<div class="">
94-
<div class="mt-2 text-muted">Help slot</div>
94+
<div id="v-0-0-help" class="mt-2 text-muted">Help slot</div>
9595
</div>
9696
</div>"
9797
`;

test/components/__snapshots__/FormField.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ exports[`FormField > renders with help correctly 1`] = `
7979
<!--v-if-->
8080
</div>
8181
<div class="">
82-
<div class="mt-2 text-muted">Username must be unique</div>
82+
<div id="v-0-0-help" class="mt-2 text-muted">Username must be unique</div>
8383
</div>
8484
</div>"
8585
`;
@@ -91,7 +91,7 @@ exports[`FormField > renders with help slot correctly 1`] = `
9191
<!--v-if-->
9292
</div>
9393
<div class="">
94-
<div class="mt-2 text-muted">Help slot</div>
94+
<div id="v-0-0-help" class="mt-2 text-muted">Help slot</div>
9595
</div>
9696
</div>"
9797
`;

0 commit comments

Comments
 (0)