Choices
Conditional reveals and exclusive options for a group of radios or checkboxes.
Used by govuk-react's radios and checkboxes.
Unstyled
The primitive on plain elements, with no GOV.UK markup or styles: what you start from in your own library.
Default
Code
<fieldset>
<legend>How would you prefer to be contacted?</legend>
<Choices.Root>
<div>
<Choices.Input type="radio" id="contact-email" name="contact" value="email" controls="conditional-email" />
<label htmlFor="contact-email">Email</label>
</div>
<Choices.Conditional id="conditional-email">
<label htmlFor="email">Email address</label>
<input id="email" name="email" type="email" autoComplete="email" spellCheck={false} />
</Choices.Conditional>
<div>
<Choices.Input type="radio" id="contact-phone" name="contact" value="phone" controls="conditional-phone" />
<label htmlFor="contact-phone">Phone</label>
</div>
<Choices.Conditional id="conditional-phone">
<label htmlFor="phone">Phone number</label>
<input id="phone" name="phone" type="tel" autoComplete="tel" />
</Choices.Conditional>
</Choices.Root>
</fieldset>Exclusive
Checking "No, I will not be travelling to any of these countries" unchecks the others.
Code
<fieldset>
<legend>Will you be travelling to any of these countries?</legend>
<Choices.Root>
<div>
<Choices.Input type="checkbox" id="countries-france" name="countries" value="france" />
<label htmlFor="countries-france">France</label>
</div>
<div>
<Choices.Input type="checkbox" id="countries-portugal" name="countries" value="portugal" />
<label htmlFor="countries-portugal">Portugal</label>
</div>
<p>or</p>
<div>
<Choices.Input type="checkbox" id="countries-none" name="countries" value="none" exclusive />
<label htmlFor="countries-none">No, I will not be travelling to any of these countries</label>
</div>
</Choices.Root>
</fieldset>In govuk-react
govuk-react's radios, built with this primitive.
Code
<Radios.Field
idPrefix="where-do-you-live"
legend="Where do you live?"
>
{items}
</Radios.Field>Usage
import * as Choices from '@govuk-react/primitives/choices';
<Choices.Root>
<Choices.Input type="radio" id="email" name="contact" value="email" controls="email-reveal" />
<label htmlFor="email">Email</label>
<Choices.Conditional id="email-reveal">…</Choices.Conditional>
</Choices.Root>;
For a group of radios or checkboxes. Without JavaScript every Conditional is shown. On upgrade, each one shows only while the choice that controls it is checked, and that choice gets aria-controls and aria-expanded. A checkbox with exclusive, such as “None of these”, unchecks the other checkboxes with its name when checked, and is unchecked when one of them is. They’re unchecked with click(), so change events fire and controlled React inputs hear about it. Control the inputs as usual, with checked and onChange.
Inline script
To make the markup interactive before React hydrates, inline @govuk-react/primitives/inline/choices-element.js, or render it from React on the server:
import { InlineScript, choices } from '@govuk-react/primitives/inline-script';
<InlineScript script={choices} nonce={nonce} />;API reference
Choices.Root
| Prop | Type |
|---|---|
upgradedClassClasses for the element once upgraded. | string |
hiddenConditionalClassClasses that hide a reveal. Without them, | string |
Choices.Conditional
Content revealed while its choice is checked. Give it the id the choice controls.
| Prop | Type |
|---|---|
asChildRender the only child instead, with this part's props merged onto it. | boolean |
Choices.Input
A radio or checkbox.
| Prop | Type |
|---|---|
asChildRender the only child instead, with this part's props merged onto it. | boolean |
controlsThe id of the | string |
exclusiveFor a checkbox such as "None of these": checking it unchecks the others with its name. | boolean |