NHSDigital / nhsuk-react-components

NHS.UK Frontend ported to React
https://nhsdigital.github.io/nhsuk-react-components
MIT License
56 stars 33 forks source link

Hints and errors are not semantically associated with fieldsets #214

Open edwardhorsford opened 6 months ago

edwardhorsford commented 6 months ago

I'm expecting that this library produce output markup that mirrors the design system reference markup.

It looks like hints and error messages are not associated correctly with fieldsets - and thus all items that use fieldset like radios and checkboxes. This is likely an accessibility issue - both that it's not following the reference implementation that's been tested hundreds of times, but also that errors aren't associated at all - and a screen reader user might not get it.

For a set of radios like this: Screenshot 2024-05-01 at 12 49 15

The reference design system markup is:

<div class="nhsuk-form-group nhsuk-form-group--error">

  <fieldset class="nhsuk-fieldset" aria-describedby="example-error-hint example-error-error">
  <legend class="nhsuk-fieldset__legend">
    Have you changed your name?
  </legend>

  <div class="nhsuk-hint" id="example-error-hint">
    This includes changing your last name or spelling your name differently.
  </div>

  <span class="nhsuk-error-message" id="example-error-error">
    <span class="nhsuk-u-visually-hidden">Error:</span> Select yes if you have changed your name
  </span>

  <div class="nhsuk-radios">

    <div class="nhsuk-radios__item">
      <input class="nhsuk-radios__input" id="example-error-1" name="example-error" type="radio" value="yes">
      <label class="nhsuk-label nhsuk-radios__label" for="example-error-1">
        Yes
      </label>
    </div>

    <div class="nhsuk-radios__item">
      <input class="nhsuk-radios__input" id="example-error-2" name="example-error" type="radio" value="no">
      <label class="nhsuk-label nhsuk-radios__label" for="example-error-2">
        No
      </label>
    </div>

  </div>
  </fieldset>

</div>

The markup in story book is:

<div class="nhsuk-form-group nhsuk-form-group--error">
  <fieldset class="nhsuk-fieldset">
    <legend class="nhsuk-fieldset__legend">Have you changed your name?</legend>
    <div class="nhsuk-form-group nhsuk-form-group--error">
      <div class="nhsuk-hint" id="radios_7l6kx--hint">This includes changing your last name or spelling your name differently.</div>
      <span class="nhsuk-error-message" id="radios_7l6kx--error-message" role="alert">
        <span class="nhsuk-u-visually-hidden">Error: </span>Please select an option
      </span>
      <div class="nhsuk-radios" id="radios_7l6kx" aria-describedby="radios_7l6kx--hint">
        <div class="nhsuk-radios__item">
          <input class="nhsuk-radios__input" id="example-1" name="example" aria-labelledby="example-1--label" type="radio" value="yes">
          <label class="nhsuk-label nhsuk-radios__label" id="example-1--label" for="example-1">Yes</label>
        </div>
        <div class="nhsuk-radios__item">
          <input class="nhsuk-radios__input" id="example-2" name="example" aria-labelledby="example-2--label" type="radio" value="no" checked="">
          <label class="nhsuk-label nhsuk-radios__label" id="example-2--label" for="example-2">No</label>
        </div>
      </div>
    </div>
  </fieldset>
</div>

Issues:

edwardhorsford commented 6 months ago

Thinking about this some more, I suspect it's because the radios and checkboxes components don't contain the fieldset or legend components - the examples in the storybook have you use those components separately and then nest the checkboses or radios.

This is a risky approach as it would require end users to manually add the appropriate aria descriptions to fieldsets - developers shouldn't need to worry about this - the component should aim to take care of all markup, given appropriate inputs.

The way the NHS and GOV.UK design system components handle this is to make fieldset an optional argument to the radio and checkboxes components - if provided, the component wraps itself in the fieldset component and applies the appropriate aria descriptions.

NHS design system native component:

{{ radios({
  "idPrefix": "example-error",
  "name": "example-error",
  "errorMessage": {
    "text": "Select yes if you have changed your name"
  },
  "fieldset": {
    "legend": {
      "text": "Have you changed your name?",
      "classes": "nhsuk-fieldset__legend--l",
      "isPageHeading": true
    }
  },
  "hint": {
    "text": "This includes changing your last name or spelling your name differently."
  },
  "items": [
    {
      "value": "yes",
      "text": "Yes"
    },
    {
      "value": "no",
      "text": "No"
    }
  ]
}) }}

Here you can see that fieldset is an optional property of radios and checkboxes, and legend is a property of fieldset. I think this this library's radio and checkbox components could be extended to accept these optional properties.