Skip to main content

Checkboxes

Default

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "nationality",
  items: [
    {
      value: "british",
      text: "British"
    },
    {
      value: "irish",
      text: "Irish"
    },
    {
      value: "other",
      text: "Citizen of another country"
    }
  ]
}} />

With preChecked values

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "nationality",
  items: [
    {
      value: "british",
      text: "British"
    },
    {
      value: "irish",
      text: "Irish"
    },
    {
      value: "other",
      text: "Citizen of another country",
      conditional: {
        html: "<div class=\"govuk-form-group\">\n  <label class=\"govuk-label\" for=\"other-country\">\n    Country\n  </label>\n<input class=\"govuk-input\" id=\"other-country\" name=\"other-country\" type=\"text\" value=\"Ravka\">\n</div>\n"
      }
    }
  ],
  values: [
    "british",
    "other"
  ]
}} />

With divider and None

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "with-divider-and-none",
  fieldset: {
    legend: {
      text: "Which types of waste do you transport regularly?",
      classes: "govuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  items: [
    {
      value: "animal",
      text: "Waste from animal carcasses"
    },
    {
      value: "mines",
      text: "Waste from mines or quarries"
    },
    {
      value: "farm",
      text: "Farm or agricultural waste"
    },
    {
      divider: "or"
    },
    {
      value: "none",
      text: "None of these",
      behaviour: "exclusive"
    }
  ]
}} />

With divider, None and conditional items

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "with-divider-and-none-and-conditional-items",
  fieldset: {
    legend: {
      text: "Do you have any access needs?",
      classes: "govuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  items: [
    {
      value: "accessible-toilets",
      text: "Accessible toilets available"
    },
    {
      value: "braille",
      text: "Braille translation service available"
    },
    {
      value: "disabled-car-parking",
      text: "Disabled car parking available"
    },
    {
      value: "another-access-need",
      text: "Another access need",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"other-access-needs\">Other access needs</label>\n<textarea class=\"govuk-textarea govuk-!-width-one-third\" name=\"other-access-needs\" id=\"other-access-needs\"></textarea>\n"
      }
    },
    {
      divider: "or"
    },
    {
      value: "none",
      text: "None of these",
      behaviour: "exclusive"
    }
  ]
}} />

With id and name

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "with-id-and-name",
  fieldset: {
    legend: {
      text: "What is your nationality?"
    }
  },
  hint: {
    text: "If you have dual nationality, select all options that are relevant to you."
  },
  items: [
    {
      name: "british",
      id: "item_british",
      value: "yes",
      text: "British"
    },
    {
      name: "irish",
      id: "item_irish",
      value: "irish",
      text: "Irish"
    },
    {
      name: "custom-name-scottish",
      text: "Scottish",
      value: "scottish"
    }
  ]
}} />

With hints on items

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "with-hints-on-items",
  fieldset: {
    legend: {
      text: "How do you want to sign in?",
      classes: "govuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  items: [
    {
      name: "gateway",
      id: "government-gateway",
      value: "gov-gateway",
      text: "Sign in with Government Gateway",
      hint: {
        text: "You'll have a user ID if you've registered for Self Assessment or filed a tax return online before."
      }
    },
    {
      name: "verify",
      id: "govuk-verify",
      value: "gov-verify",
      text: "Sign in with GOV.UK Verify",
      hint: {
        text: "You'll have an account if you've already proved your identity with either Barclays, CitizenSafe, Digidentity, Experian, Post Office, Royal Mail or SecureIdentity."
      }
    }
  ]
}} />

With disabled item

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "sign-in",
  fieldset: {
    legend: {
      text: "How do you want to sign in?",
      classes: "govuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  items: [
    {
      name: "gateway",
      id: "government-gateway",
      value: "gov-gateway",
      text: "Sign in with Government Gateway",
      hint: {
        text: "You'll have a user ID if you've registered for Self Assessment or filed a tax return online before."
      }
    },
    {
      name: "verify",
      id: "govuk-verify",
      value: "gov-verify",
      text: "Sign in with GOV.UK Verify",
      hint: {
        text: "You'll have an account if you've already proved your identity with either Barclays, CitizenSafe, Digidentity, Experian, Post Office, Royal Mail or SecureIdentity."
      },
      disabled: true
    }
  ]
}} />

With legend as a page heading

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "waste",
  fieldset: {
    legend: {
      text: "Which types of waste do you transport regularly?",
      classes: "govuk-fieldset__legend--l",
      isPageHeading: true
    }
  },
  hint: {
    text: "Select all that apply"
  },
  items: [
    {
      value: "animal",
      text: "Waste from animal carcasses"
    },
    {
      value: "mines",
      text: "Waste from mines or quarries"
    },
    {
      value: "farm",
      text: "Farm or agricultural waste"
    }
  ]
}} />

With a medium legend

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "waste",
  fieldset: {
    legend: {
      text: "Which types of waste do you transport regularly?",
      classes: "govuk-fieldset__legend--m"
    }
  },
  hint: {
    text: "Select all that apply"
  },
  errorMessage: {
    text: "Select which types of waste you transport regularly"
  },
  items: [
    {
      value: "animal",
      text: "Waste from animal carcasses"
    },
    {
      value: "mines",
      text: "Waste from mines or quarries"
    },
    {
      value: "farm",
      text: "Farm or agricultural waste"
    }
  ]
}} />

Without fieldset

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "colours",
  items: [
    {
      value: "red",
      text: "Red"
    },
    {
      value: "green",
      text: "Green"
    },
    {
      value: "blue",
      text: "Blue"
    }
  ]
}} />

With single option set 'ariaDescribedby' on input

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "t-and-c",
  errorMessage: {
    text: "Please accept the terms and conditions"
  },
  items: [
    {
      value: "yes",
      text: "I agree to the terms and conditions"
    }
  ]
}} />

With single option (and hint) set 'ariaDescribedby' on input

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "t-and-c-with-hint",
  errorMessage: {
    text: "Please accept the terms and conditions"
  },
  items: [
    {
      value: "yes",
      text: "I agree to the terms and conditions",
      hint: {
        text: "Go on, you know you want to!"
      }
    }
  ]
}} />

With fieldset and error message

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "nationality",
  errorMessage: {
    text: "Please accept the terms and conditions"
  },
  fieldset: {
    legend: {
      text: "What is your nationality?"
    }
  },
  items: [
    {
      value: "british",
      text: "British"
    },
    {
      value: "irish",
      text: "Irish"
    },
    {
      value: "other",
      text: "Citizen of another country"
    }
  ]
}} />

With error message

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "waste",
  errorMessage: {
    text: "Please select an option"
  },
  fieldset: {
    legend: {
      text: "Which types of waste do you transport regularly?"
    }
  },
  items: [
    {
      value: "animal",
      text: "Waste from animal carcasses"
    },
    {
      value: "mines",
      text: "Waste from mines or quarries"
    },
    {
      value: "farm",
      text: "Farm or agricultural waste"
    }
  ]
}} />

With error message and hints on items

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "waste",
  errorMessage: {
    text: "Please select an option"
  },
  fieldset: {
    legend: {
      text: "Which types of waste do you transport regularly?"
    }
  },
  items: [
    {
      value: "animal",
      text: "Waste from animal carcasses",
      hint: {
        text: "Nullam id dolor id nibh ultricies vehicula ut id elit."
      }
    },
    {
      value: "mines",
      text: "Waste from mines or quarries",
      hint: {
        text: "Nullam id dolor id nibh ultricies vehicula ut id elit."
      }
    },
    {
      value: "farm",
      text: "Farm or agricultural waste",
      hint: {
        text: "Nullam id dolor id nibh ultricies vehicula ut id elit."
      }
    }
  ]
}} />

With very long option text

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "waste",
  hint: {
    text: "Nullam id dolor id nibh ultricies vehicula ut id elit."
  },
  errorMessage: {
    text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
  },
  fieldset: {
    legend: {
      text: "Maecenas faucibus mollis interdum?"
    }
  },
  items: [
    {
      value: "nullam",
      text: "Nullam id dolor id nibh ultricies vehicula ut id elit. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Maecenas faucibus mollis interdum. Donec id elit non mi porta gravida at eget metus."
    },
    {
      value: "aenean",
      text: "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Donec sed odio dui. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Cras mattis consectetur purus sit amet fermentum."
    },
    {
      value: "fusce",
      text: "Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Etiam porta sem malesuada magna mollis euismod. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. Sed posuere consectetur est at lobortis."
    }
  ]
}} />

With conditional items

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "with-conditional-items",
  idPrefix: "how-contacted",
  fieldset: {
    legend: {
      text: "How do you want to be contacted?"
    }
  },
  items: [
    {
      value: "email",
      text: "Email",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"context-email\">Email address</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"context-email\" type=\"text\" id=\"context-email\">\n"
      }
    },
    {
      value: "phone",
      text: "Phone",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-phone\">Phone number</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"contact-phone\" type=\"text\" id=\"contact-phone\">\n"
      }
    },
    {
      value: "text",
      text: "Text message",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-text-message\">Mobile phone number</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"contact-text-message\" type=\"text\" id=\"contact-text-message\">\n"
      }
    }
  ]
}} />

With conditional items with special characters

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "contact-prefs",
  idPrefix: "user.profile[contact-prefs]",
  fieldset: {
    legend: {
      text: "How do you want to be contacted?"
    }
  },
  items: [
    {
      value: "email",
      text: "Email",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"context-email\">Email address</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"context-email\" type=\"text\" id=\"context-email\">\n"
      }
    },
    {
      value: "phone",
      text: "Phone",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-phone\">Phone number</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"contact-phone\" type=\"text\" id=\"contact-phone\">\n"
      }
    },
    {
      value: "text",
      text: "Text message",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-text-message\">Mobile phone number</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"contact-text-message\" type=\"text\" id=\"contact-text-message\">\n"
      }
    }
  ]
}} />

With conditional item checked

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "how-contacted-checked",
  idPrefix: "how-contacted-checked",
  fieldset: {
    legend: {
      text: "How do you want to be contacted?"
    }
  },
  items: [
    {
      value: "email",
      text: "Email",
      checked: true,
      conditional: {
        html: "<label class=\"govuk-label\" for=\"context-email\">Email address</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"context-email\" type=\"text\" id=\"context-email\">\n"
      }
    },
    {
      value: "phone",
      text: "Phone",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-phone\">Phone number</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"contact-phone\" type=\"text\" id=\"contact-phone\">\n"
      }
    },
    {
      value: "text",
      text: "Text message",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-text-message\">Mobile phone number</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"contact-text-message\" type=\"text\" id=\"contact-text-message\">\n"
      }
    }
  ]
}} />

With optional formGroup classes showing group error

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "how-contacted-checked",
  idPrefix: "how-contacted-checked",
  formGroup: {
    classes: "govuk-form-group--error"
  },
  fieldset: {
    legend: {
      text: "How do you want to be contacted?"
    }
  },
  items: [
    {
      value: "email",
      text: "Email",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"context-email\">Email address</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"context-email\" type=\"text\" id=\"context-email\">\n"
      }
    },
    {
      value: "phone",
      text: "Phone",
      checked: true,
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-phone\">Phone number</label>\n<span id=\"contact-phone-error\" class=\"govuk-error-message\">Problem with input</span>\n<input class=\"govuk-input govuk-input--error govuk-!-width-one-third\" name=\"contact-phone\" type=\"text\" id=\"contact-phone\" aria-describedby=\"contact-phone-error\">\n"
      }
    },
    {
      value: "text",
      text: "Text message",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"contact-text-message\">Mobile phone number</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"contact-text-message\" type=\"text\" id=\"contact-text-message\">\n"
      }
    }
  ]
}} />

Small

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  idPrefix: "nationality",
  name: "nationality",
  classes: "govuk-checkboxes--small",
  fieldset: {
    legend: {
      text: "Filter by"
    }
  },
  items: [
    {
      value: "a",
      text: "a thing"
    },
    {
      value: "b",
      text: "another thing"
    },
    {
      value: "c",
      text: "this thing"
    }
  ]
}} />

Small with long text

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  idPrefix: "nationality",
  name: "nationality",
  classes: "govuk-checkboxes--small",
  fieldset: {
    legend: {
      text: "Filter by"
    }
  },
  items: [
    {
      value: "nullam",
      text: "Nullam id dolor id nibh ultricies vehicula ut id elit. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Maecenas faucibus mollis interdum. Donec id elit non mi porta gravida at eget metus."
    },
    {
      value: "aenean",
      text: "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Donec sed odio dui. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Cras mattis consectetur purus sit amet fermentum."
    },
    {
      value: "fusce",
      text: "Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Etiam porta sem malesuada magna mollis euismod. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. Sed posuere consectetur est at lobortis."
    }
  ]
}} />

Small with error

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  idPrefix: "nationality",
  name: "nationality",
  classes: "govuk-checkboxes--small",
  errorMessage: {
    text: "Select a thing"
  },
  fieldset: {
    legend: {
      text: "Filter by"
    }
  },
  items: [
    {
      value: "a",
      text: "a thing"
    },
    {
      value: "b",
      text: "another thing"
    },
    {
      value: "c",
      text: "this thing"
    }
  ]
}} />

Small with hint

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  idPrefix: "nationality",
  name: "nationality",
  classes: "govuk-checkboxes--small",
  fieldset: {
    legend: {
      text: "Filter by"
    }
  },
  items: [
    {
      value: "a",
      text: "a thing",
      hint: {
        text: "hint for a thing"
      }
    },
    {
      value: "b",
      text: "another thing"
    },
    {
      value: "c",
      text: "this thing"
    }
  ]
}} />

Small with disabled

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  idPrefix: "nationality",
  name: "nationality",
  classes: "govuk-checkboxes--small",
  fieldset: {
    legend: {
      text: "Filter by"
    }
  },
  items: [
    {
      value: "a",
      text: "a thing"
    },
    {
      value: "b",
      text: "another thing"
    },
    {
      value: "c",
      text: "this thing",
      disabled: true
    }
  ]
}} />

Small with conditional reveal

Svelte code
<script>
	import { Checkboxes } from '@danboscaro/govuk-svelte'
</script>

<Checkboxes {...{
  name: "how-contacted",
  idPrefix: "how-contacted",
  classes: "govuk-checkboxes--small",
  fieldset: {
    legend: {
      text: "How do you want to be contacted?"
    }
  },
  items: [
    {
      value: "a",
      text: "a thing",
      conditional: {
        html: "<label class=\"govuk-label\" for=\"context-email\">Foo</label>\n<input class=\"govuk-input govuk-!-width-one-third\" name=\"context-email\" type=\"text\" id=\"context-email\">\n"
      }
    },
    {
      value: "b",
      text: "another thing"
    }
  ]
}} />