Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to set checkbox radio checked options #1105

Merged

Conversation

frankieroberto
Copy link
Contributor

@frankieroberto frankieroberto commented Jan 23, 2025

This adds a new param to both the Checkboxes and Radios components to make it easier to set which items should be checked by default.

This behaviour matches the Checkbox and Radios components within GOV.UK Frontend.

Description

Currently to set which radio or checkbox items should be pre-ticked, you have to set a checked boolean on the relevant items.

When using the NHS prototype kit, this means code like this:

{{ radios({
  idPrefix: "injectionSite",
  name: "injectionSite",
  fieldset: {
    legend: {
      text: "Where did you give the injection?",
      classes: "nhsuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  items: [
    {
      value: "Left arm",
      text: "Left arm",
      checked: (data.injectionSite == 'Left arm')
    },
    {
      value: "Right arm",
      text: "Right arm",
      checked: (data.injectionSite == 'Right arm')
    }
]}) }}

This pull request makes this simpler and less error-prone by allowing you to set the current selected value, and having the nunjucks macro set the matching item as checked, like this:

{{ radios({
  idPrefix: "injectionSite",
  name: "injectionSite",
  fieldset: {
    legend: {
      text: "Where did you give the injection?",
      classes: "nhsuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  value: data.injectionSite,
  items: [
    {
      value: "Left arm",
      text: "Left arm"
    },
    {
      value: "Right arm",
      text: "Right arm"
    }
]}) }}

A similar feature has been added for checkboxes, except that the param is called values, and works with an array of values which should be checked:

{{ checkboxes({
  idPrefix: "eligibility",
  name: "eligibility",
  fieldset: {
    legend: {
      text: "Why are you giving them the vaccine?",
      classes: "nhsuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  values: data.eligibility,
  items: [
    {
      value: "age-based"
      text: "Based on age"
    },
    {
      value: "pregnant"
      text: "Pregnant"
    },
    {
      value: "immunosuppressed",
      text: "Immunosuppressed"
    }
  ]
}) }}

Checklist

frankieroberto and others added 3 commits January 23, 2025 15:43
Currently you have to set `checked` on each item.

This would instead make it possible to set `value` (for radios) or `values` (for checkboxes) which would then automatically set the checked values for the items if they match the value given.

The previous method of setting `checked` on each item still works, and would override the default.
@paulrobertlloyd
Copy link
Contributor

In this (or a separate PR) might we also be able to make it so that the first item in the radio or checklist isn’t suffixed with -1 (this makes it easier to link to a radio or checkbox fieldset from an error summary using the field’s name).

Copy link
Contributor

@colinrotherham colinrotherham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ace

@anandamaryon1 anandamaryon1 merged commit 42dcf6a into main Feb 12, 2025
5 checks passed
@anandamaryon1 anandamaryon1 deleted the make-it-easier-to-set-checkbox-radio-checked-options branch February 12, 2025 13:43
@anandamaryon1 anandamaryon1 restored the make-it-easier-to-set-checkbox-radio-checked-options branch February 12, 2025 13:45
@anandamaryon1 anandamaryon1 mentioned this pull request Feb 12, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants