surevine / govuk-react-jsx

govuk-frontend compatible React components
https://govuk-react-jsx.netlify.app/
MIT License
3 stars 0 forks source link

Accessibility issue when using Input with a prefix or suffix #131

Closed dave1976wilson closed 2 years ago

dave1976wilson commented 2 years ago

When using a prefix or suffix on an Input component the label element is not rendered within the same div as the input element and therefore it fails an accessibility check - 'Form elements do not have associated labels'.

andymantell commented 2 years ago

I've had a quick look at this and the following JSX:

<Input
  id="input-with-prefix"
  label={{
    children: 'Amount, in pounds'
  }}
  name="amount"
  prefix={{
    children: '£'
  }}
  type="text"
/>

yields this HTML:

<div class="govuk-form-group">
    <label class="govuk-label" for="input-with-prefix">Amount, in pounds</label>
    <div class="govuk-input__wrapper">
        <div aria-hidden="true" class="govuk-input__prefix ">£</div>
        <input id="input-with-prefix" class="govuk-input" name="amount" type="text">
    </div>
</div>

Which looks fine to me. The label is associated with the input with the for/id combination. Label / input association isn't affected by being in the same div or not.

Can you give a bit more detail on what you're seeing? It's a little hard to comment without an example of your issue. What JSX are you using and what HTML does it give? What specifically is wrong with it?

dave1976wilson commented 2 years ago

Thank you for your reply. I seem to get the a11y issue with and without the prefix now. I was sure I only saw it when using the prefix. I have attached a screenshot of my html and the lighthouse report. I will assume I am not doing something correctly and will investigate. Thanks again.

Screenshot 2021-12-16 at 11 11 49 Screenshot 2021-12-16 at 11 14 29
andymantell commented 2 years ago

Could you paste the JSX that you're using too?

andymantell commented 2 years ago

Looking at your code - you don't have an id on the input actually, and you're getting things like undefined-hint elsewhere. My hunch would be that you've not specified an ID in your JSX that's calling this.

I wonder whether I should be doing something with this, like outputting a console error if required things are missing 🤔

dave1976wilson commented 2 years ago

Thank you for solving it! I have added an id and that has fixed the a11y issue. Thanks again.

andymantell commented 2 years ago

Ah excellent, I'm glad that sorted it for you.

Out of interest - would you have liked to receive notification somehow that this was the issue? Perhaps a console error or warning?

I'm thinking an error - the resultant html is basically malformed and you wouldn't want to ship it to produciton like that.

dave1976wilson commented 2 years ago

Yes, I guess I would have liked to have received a console warning or error so I didn't post my 'issue'.

andymantell commented 2 years ago

Thanks @dave1976wilson. I've logged this as #133