import { OntarioCheckboxes } from '@ongov/ontario-design-system-component-library-react';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# ontario-checkboxes

Use checkboxes when you want the user to select one or more options from a list.

## User guidance

Please refer to the [Ontario Design System](https://designsystem.ontario.ca/components/detail/checkboxes.html) for current documentation guidance.

### Disabled and read-only states

This component intentionally does not provide group-level `readOnly` or `disabled` props.

Disabling form controls can create accessibility and usability barriers, and often does not explain what the user needs to fix.

Instead:

- keep controls and submission actions available
- use validation and error messaging to clearly identify missing or invalid input

For implementation examples, see [Error messaging](#error-messaging).

Source: https://designsystem.ontario.ca/components/detail/buttons.html#disabled-buttons

## Configuration

Once the component package has been installed (see Ontario Design System Component Library for installation instructions), the checkbox component can be added directly into the project's code, and can be customized by updating the properties outlined [here](#properties). Additional information on custom types for header properties are outlined [here](#custom-property-types). Please see the [examples](#examples) below for how to configure the component.

## Examples

Example of a bare-bones checkbox component, the legend for the checkbox fieldset is provided, as well as 2 checkbox options.

```mdx-code-block
<Tabs
	defaultValue="html"
	values={[
		{label: 'HTML', value: 'html'},
		{label: 'React', value: 'react'},
		{label: 'Angular', value: 'angular'},
	]}
	groupId="framework"
	queryString="framework">
<TabItem value="html">
```

```html
<ontario-checkboxes
	caption="Checkbox legend"
	name="checkboxes"
	options='[
    {
      "value": "checkbox-option-1",
      "label": "Checkbox option 1 label",
      "elementId": "checkbox-1"
    },
    {
      "value": "checkbox-option-2",
      "label": "Checkbox option 2 label",
      "elementId": "checkbox-2"
    }
  ]'
>
</ontario-checkboxes>
```

```mdx-code-block
</TabItem>
<TabItem value="react">
```

```tsx
<OntarioCheckboxes
	caption="Checkbox legend"
	name="checkboxes"
	options={[
		{
			value: 'checkbox-option-1',
			label: 'Checkbox option 1 label',
			elementId: 'checkbox-1',
		},
		{
			value: 'checkbox-option-2',
			label: 'Checkbox option 2 label',
			elementId: 'checkbox-2',
		},
	]}
/>
```

```mdx-code-block
</TabItem>
<TabItem value="angular">
```

```html
<ontario-checkboxes
	[caption]="'Checkbox legend'"
	[name]="'checkboxes'"
	[options]="[
		{ value: 'checkbox-option-1', label: 'Checkbox option 1 label', elementId: 'checkbox-1' },
		{ value: 'checkbox-option-2', label: 'Checkbox option 2 label', elementId: 'checkbox-2' }
	]"
></ontario-checkboxes>
```

```mdx-code-block
</TabItem>
</Tabs>
```

<div>
	<OntarioCheckboxes
		caption="Checkbox legend"
		name="checkboxes"
		options={[
			{
				value: 'checkbox-option-1',
				label: 'Checkbox option 1 label',
				elementId: 'checkbox-1',
			},
			{
				value: 'checkbox-option-2',
				label: 'Checkbox option 2 label',
				elementId: 'checkbox-2',
			},
		]}
	></OntarioCheckboxes>
</div>

Example of a checkbox component with multiple options, a hint text and hint expander for the entire checkbox group, and a hint expander on an individual option. It also has the `required` property set.

```mdx-code-block
<Tabs
	defaultValue="html"
	values={[
		{label: 'HTML', value: 'html'},
		{label: 'React', value: 'react'},
		{label: 'Angular', value: 'angular'},
	]}
	groupId="framework"
	queryString="framework">
<TabItem value="html">
```

```html
<ontario-checkboxes
	caption='{
		"captionText": "Checkbox legend",
		"captionType": "large"
	}'
	name="checkboxes"
	hint-text="Hint text for the checkbox group"
	required
	options='[
    {
      "value": "checkbox-option-1",
      "label": "Checkbox option 1 label",
      "elementId": "checkbox-1"
    },
    {
      "value": "checkbox-option-2",
      "label": "Checkbox option 2 label",
      "elementId": "checkbox-2",
      "hintExpander": {
        "hint": "Hint expander for checkbox option 2",
        "content": "Example hint expander content for checkbox option 2"
      }
    }
  ]'
>
</ontario-checkboxes>
```

```mdx-code-block
</TabItem>
<TabItem value="react">
```

```tsx
<OntarioCheckboxes
	caption={{
		captionText: 'Checkbox legend',
		captionType: 'large',
	}}
	name="checkboxes"
	hintText="Hint text for the checkbox group"
	required
	options={[
		{
			value: 'checkbox-option-1',
			label: 'Checkbox option 1 label',
			elementId: 'checkbox-1',
		},
		{
			value: 'checkbox-option-2',
			label: 'Checkbox option 2 label',
			elementId: 'checkbox-2',
			hintExpander: {
				hint: 'Hint expander for checkbox option 2',
				content: 'Example hint expander content for checkbox option 2',
			},
		},
	]}
/>
```

```mdx-code-block
</TabItem>
<TabItem value="angular">
```

```html
<ontario-checkboxes
	[caption]="{ captionText: 'Checkbox legend', captionType: 'large' }"
	[name]="'checkboxes'"
	[hintText]="'Hint text for the checkbox group'"
	required
	[options]="[
		{ value: 'checkbox-option-1', label: 'Checkbox option 1 label', elementId: 'checkbox-1' },
		{
			value: 'checkbox-option-2',
			label: 'Checkbox option 2 label',
			elementId: 'checkbox-2',
			hintExpander: { hint: 'Hint expander for checkbox option 2', content: 'Example hint expander content for checkbox option 2' }
		}
	]"
></ontario-checkboxes>
```

```mdx-code-block
</TabItem>
</Tabs>
```

<div>
	<OntarioCheckboxes
		caption={{
			captionText: 'Checkbox legend',
			captionType: 'large',
		}}
		name="checkboxes"
		hintText="Hint text for the checkbox group"
		required
		options={[
			{
				value: 'checkbox-option-1',
				label: 'Checkbox option 1 label',
				elementId: 'checkbox-1',
			},
			{
				value: 'checkbox-option-2',
				label: 'Checkbox option 2 label',
				elementId: 'checkbox-2',
				hintExpander: {
					hint: 'Hint expander for checkbox option 2',
					content: 'Example hint expander content for checkbox option 2',
				},
			},
		]}
	></OntarioCheckboxes>
</div>

In the following example, the selected checkbox options are set using the
component's `value`. Listen for the component `change` event to read the
current selected values.

```mdx-code-block
<Tabs
	defaultValue="html"
	values={[
		{label: 'HTML', value: 'html'},
		{label: 'React', value: 'react'},
		{label: 'Angular', value: 'angular'},
	]}
	groupId="framework"
	queryString="framework">
<TabItem value="html">
```

```html
<ontario-checkboxes
	id="checkboxes-value-example"
	caption="Checkbox legend"
	name="checkboxes"
	value='["checkbox-option-2"]'
	options='[
		{
			"value": "checkbox-option-1",
			"label": "Checkbox option 1 label",
			"elementId": "checkbox-1"
		},
		{
			"value": "checkbox-option-2",
			"label": "Checkbox option 2 label",
			"elementId": "checkbox-2"
		}
	]'
></ontario-checkboxes>
<script>
	document.getElementById('checkboxes-value-example')?.addEventListener('change', (event) => {
		console.log(event.target.value);
		console.log(event.detail.value);
	});
</script>
```

```mdx-code-block
</TabItem>
<TabItem value="react">
```

```tsx
<OntarioCheckboxes
	caption="Checkbox legend"
	name="checkboxes"
	value={['checkbox-option-2']}
	options={[
		{ value: 'checkbox-option-1', label: 'Checkbox option 1 label', elementId: 'checkbox-1' },
		{ value: 'checkbox-option-2', label: 'Checkbox option 2 label', elementId: 'checkbox-2' },
	]}
	onChange={(event) => {
		console.log((event.target as HTMLOntarioCheckboxesElement).value);
		console.log(event.detail.value);
	}}
/>
```

```mdx-code-block
</TabItem>
<TabItem value="angular">
```

```html
<ontario-checkboxes
	[caption]="'Checkbox legend'"
	[name]="'checkboxes'"
	[value]="['checkbox-option-2']"
	[options]="[
		{ value: 'checkbox-option-1', label: 'Checkbox option 1 label', elementId: 'checkbox-1' },
		{ value: 'checkbox-option-2', label: 'Checkbox option 2 label', elementId: 'checkbox-2' }
	]"
	(change)="handleCheckboxChange($event)"
></ontario-checkboxes>
```

```ts
handleCheckboxChange(event: Event) {
	console.log((event.target as HTMLOntarioCheckboxesElement).value);
	console.log((event as CustomEvent<{ value: string[] }>).detail.value);
}
```

```mdx-code-block
</TabItem>
</Tabs>
```

### Forms

The `ontario-checkboxes` supports integration with native HTML `<form>` elements. This element integrates with the underlying browser form API and should work the same as a a group of `<input type="checkbox">` elements.

```html
<form>
	<!-- Add ontario-checkboxes -->
	<ontario-checkboxes
		caption="Checkboxes"
		name="checkboxes-1"
		required
		options='[
      {
        "value": "checkbox-option-1",
        "label": "Checkbox option 1 label",
        "elementId": "checkbox-1"
      },
      {
        "value": "checkbox-option-2",
        "label": "Checkbox option 2 label",
        "elementId": "checkbox-2"
      }
    ]'
	>
	</ontario-checkboxes>

	<!-- Submit the form with a submit button -->
	<ontario-button type="primary" html-type="submit">Submit</ontario-button>
</form>
```

Remember to set the `name` attribute as this is used to identify the field when submitting the form.

## Event model

For most integrations, prefer the component `change` event and read the
selected values from `event.target.value`. That event also includes
`event.detail.value` as a convenience. Use `checkboxOnChange` when you
specifically want the component's per-option custom event payload.

Each custom event emitted by the component uses the
[`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent)
type. To access the data emitted by the component within the `CustomEvent` type
use the [CustomEvent.detail](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail)
property.

Example of the component `change` event:

```html
<ontario-checkboxes
	id="checkboxes-change-example"
	caption="Checkboxes"
	name="checkboxes-1"
	options='[
		{
			"value": "checkbox-option-1",
			"label": "Checkbox option 1 label",
			"elementId": "checkbox-1"
		},
		{
			"value": "checkbox-option-2",
			"label": "Checkbox option 2 label",
			"elementId": "checkbox-2"
		}
	]'
></ontario-checkboxes>
<script>
	window.onload = () => {
		const checkboxes1 = document.getElementById('checkboxes-change-example');
		checkboxes1.addEventListener('change', (event) => {
			console.log(event.target.value);
			console.log(event.detail.value);
		});
	};
</script>
```

Example `checkboxOnChange` usage when you need per-option detail:

```html
<ontario-checkboxes
	caption="Checkboxes"
	id="checkboxes-1"
	name="checkboxes-1"
	required
	options='[
      {
        "value": "checkbox-option-1",
        "label": "Checkbox option 1 label",
        "elementId": "checkbox-1"
      },
      {
        "value": "checkbox-option-2",
        "label": "Checkbox option 2 label",
        "elementId": "checkbox-2"
      }
    ]'
>
</ontario-checkboxes>
<script>
	// Note: this waits for the page and components to load before
	// locating the component.
	window.onload = () => {
		const checkboxes1 = document.getElementById('checkboxes-1');
		checkboxes1.addEventListener('checkboxOnChange', (event) => {
			console.log('OnChange detail:', event.detail);
		});
	};
</script>
```

If the first checkbox is checked within `checkboxes-1`, the value of `event.detail` is the object emitted along with the `checkboxOnChange` event.

```js
{ checked: true, id: "checkbox-1", value: "checkbox-option-1" }
```

See the [Events](#events) table to learn more about the available custom events from the component and what the type of `CustomEvent.detail` will be.

### Native `change` events

The component uses a ShadowDOM to maintain encapsulation, however, this changes how the events flow from the inside of the component to the outside in the DOM.

The component keeps its `value` in sync with the currently selected checkbox
options.

The native `change` event hits the ShadowDOM boundary and stops propagating.
The implication of this is that it can't be listened for outside the
component. To attempt to overcome this, a synthetic change event is generated
and emitted. The original `change` event is available via the `detail`
property on the emitted event.

When using libraries that listen for events, this process may not work with them and a workaround might be required depending on the framework or library in use.

## Error messaging

Use validation and error messaging to help users understand what needs to be corrected.

### Setting an error message

Set `errorMessage` when required selection rules are not met, and keep options available for correction.

### Static vs live validation

Use a static `errorMessage` when validation happens on submit (for example, after a form post or submit handler check).

Use live validation when you want real-time feedback as users interact (for example, on change or blur).

For more guidance, visit the [Error messaging guidance page](https://designsystem.ontario.ca/components/detail/error-messaging.html).

```mdx-code-block
<Tabs
	defaultValue="html"
	values={[
		{label: 'HTML', value: 'html'},
		{label: 'React', value: 'react'},
		{label: 'Angular', value: 'angular'},
	]}
	groupId="framework"
	queryString="framework">
<TabItem value="html">
```

```html
<ontario-checkboxes
	id="contact-methods"
	name="contact-methods"
	caption="How should we contact you?"
	required
	options='[
		{ "elementId": "email", "label": "Email", "value": "email" },
		{ "elementId": "phone", "label": "Phone", "value": "phone" }
	]'
></ontario-checkboxes>
<script>
	window.addEventListener('load', () => {
		const checkboxes = document.getElementById('contact-methods');
		checkboxes.addEventListener('checkboxOnChange', () => {
			const checked = checkboxes.querySelectorAll('input[type=\"checkbox\"]:checked').length;
			checkboxes.errorMessage = checked > 0 ? '' : 'Select at least one contact method.';
		});
	});
</script>
```

```mdx-code-block
</TabItem>
<TabItem value="react">
```

```tsx
<OntarioCheckboxes
	elementId="contact-methods"
	name="contact-methods"
	caption="How should we contact you?"
	required
	options={[
		{ elementId: 'email', label: 'Email', value: 'email' },
		{ elementId: 'phone', label: 'Phone', value: 'phone' },
	]}
	errorMessage="Select at least one contact method."
/>
```

```mdx-code-block
</TabItem>
<TabItem value="angular">
```

```html
<ontario-checkboxes
	[elementId]="'contact-methods'"
	[name]="'contact-methods'"
	[caption]="'How should we contact you?'"
	[required]="true"
	[options]="contactMethodOptions"
	[errorMessage]="'Select at least one contact method.'"
></ontario-checkboxes>
```

```mdx-code-block
</TabItem>
</Tabs>
```

### Live validation

Keep the control available and validate selections on interaction (for example, on change, blur, or submit). When validation fails, set a contextual error message that explains how to fix the issue.

## Custom property types

### caption

The `caption` property is used to render the legend for the ontario-checkbox. It can be passed either a string or an object. If no `captionType` needs to be specified, it can be passed as a string.

```html
caption='{ "captionText": "Checkbox legend", "captionType": "large" }'
```

| Property name | Type                               | Description                                                                                                                                          |
| ------------- | ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `captionText` | `string`                           | Text to display as the checkbox question                                                                                                             |
| `captionType` | `"default" \| "large"\| "heading"` | The type of legend to display for the checkbox question. This is optional, and if no information is provided, it will default to the `default` type. |

### hintExpander

The `hintExpander` property is used to include the Hint Expander component underneath the checkbox legend. This is passed in as an object with key-value pairs. This is optional.

```html
hintExpander='{ "hint": "This is the hint expander title", "content": "This is the hint expander content - it is only
visible when the hint expander title (hint) is toggled" }'
```

| Property name | Type     | Description                                                                                                     |
| ------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
| `hint`        | `string` | Text to display as the hint expander label/title. When toggled, it will display/hide the `hintExpander` content |
| `content`     | `string` | Text to display as the content of the hint expander                                                             |

### options

The options property will render out the different checkbox inputs and their labels. These are passed in as objects with key-value pairs, inside an array.

```html
options='[ { "value": "checkbox-option-1", "label": "Checkbox option 1 label", "elementId": "checkbox-1" }, { "value":
"checkbox-option-2", "label": "Checkbox option 2 label", "elementId": "checkbox-2", "hintExpander": { "hint": "Hint
expander for checkbox option 2", "content": "Example hint expander content for checkbox option 2" } }, ]'
```

| Property name  | Type           | Description                                                                                                                                                 |
| -------------- | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `value`        | `string`       | Text to defines the value associated with the input (this is also the value that is sent on submit)                                                         |
| `label`        | `string`       | Text to display as the label of the individual checkbox option                                                                                              |
| `elementId`    | `string`       | Text used to establish a relationship between checkbox option label and the checkbox option input. This ID must be unique to the checkbox option.           |
| `hintExpander` | `hintExpander` | An optional hint expander to display for the individual checkbox option. Information on `hintExpander` properties can be found in the custom property above |

## Accessibility

- Do not preselect checkboxes (there should be no checked attribute by default on the checkbox)
- All checkboxes in a group should have the same name value to associate them as a group of options

## Technical Note: SSR (Server-Side Rendering) Considerations

The Ontario Checkboxes component supports server-side rendering, with a few considerations:

- **Language prop:** Language change events only fire in the browser after hydration. To ensure the correct language is rendered during SSR, pass the desired `language` explicitly as a prop.
- **Dynamic ID generation:** Each checkbox option should use a stable `elementId`. If option IDs are generated differently between server and client, hydration mismatches are more likely.
- **Hint text and accessibility IDs:** If using `ontario-hint-text`, note that the `aria-describedby` reference is resolved after hydration. Make sure this does not impact critical accessibility paths in your application.
- **Form participation:** This component uses the [Form-Associated Custom Elements](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals) API (`@AttachInternals`) to participate in native form submission. During SSR, it renders a checkbox-group structure that can support straightforward form submission when the group `name`, `language`, and option ids are stable. Group-level error messaging and emitted events become available after hydration.
- **Hydrated-only behaviour:** Group-level error messaging and custom event handling should be treated as hydrated behaviour. Keep the group `name` stable across all options and verify the full submit flow in the consuming application.
- **Framework guidance:** Use the HTML `<form>` example above for native submit or Next.js server-action style flows. For client-managed integrations, use the event examples below. For App Router setup details, follow the [Next.js integration guide](https://designsystem.ontario.ca/developer-docs/framework-integrations/next-js-ssr/).

<!-- Auto Generated Below -->

## Overview

Ontario Checkboxes collects one or more selections from a defined option set.

This component intentionally does not expose group-level `readOnly` or `disabled` props.

To support accessible and understandable form completion:

- keep options and submission actions available
- use validation and error messaging to guide corrections

For component guidance, see:

- https://designsystem.ontario.ca/components/detail/checkboxes.html
- https://designsystem.ontario.ca/developer-docs/components/ontario-checkboxes/

Disabled/read-only policy source:

- https://designsystem.ontario.ca/components/detail/buttons.html#disabled-buttons

## Properties

| Property         | Attribute          | Description                                                                                                                                                                                                                                                                                                                                                                                                                            | Type                                    | Default     |
| ---------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------- | ----------- |
| `caption`        | `caption`          | The text to display for the checkbox legend.                                                                                                                                                                                                                                                                                                                                                                                           | `Caption \| string`                     | `undefined` |
| `customOnBlur`   | `custom-on-blur`   | Used to add a custom function to the checkbox onBlur event.                                                                                                                                                                                                                                                                                                                                                                            | `((event: Event) => void) \| undefined` | `undefined` |
| `customOnChange` | `custom-on-change` | Used to add a custom function to the checkbox onChange event.                                                                                                                                                                                                                                                                                                                                                                          | `((event: Event) => void) \| undefined` | `undefined` |
| `customOnFocus`  | `custom-on-focus`  | Used to add a custom function to the checkbox onFocus event.                                                                                                                                                                                                                                                                                                                                                                           | `((event: Event) => void) \| undefined` | `undefined` |
| `errorMessage`   | `error-message`    | Set this to display an error message                                                                                                                                                                                                                                                                                                                                                                                                   | `string \| undefined`                   | `undefined` |
| `hintExpander`   | `hint-expander`    | Used to include the ontario-hint-expander component for the checkbox group. This is passed in as an object with key-value pairs. This is optional.                                                                                                                                                                                                                                                                                     | `HintExpander \| string \| undefined`   | `undefined` |
| `hintText`       | `hint-text`        | Used to include the ontario-hint-text component for the checkbox group. This is optional.                                                                                                                                                                                                                                                                                                                                              | `Hint \| string \| undefined`           | `undefined` |
| `language`       | `language`         | The language of the component. This is used for translations, and is by default set through event listeners checking for a language property from the header. If no language is passed, it will default to English.                                                                                                                                                                                                                    | `"en" \| "fr" \| undefined`             | `undefined` |
| `name`           | `name`             | The name for the checkboxes. The name value is used to reference form data after a form is submitted.                                                                                                                                                                                                                                                                                                                                  | `string`                                | `undefined` |
| `options`        | `options`          | The options for the checkbox group. Each property will be passed in through an object in the options array. This can either be passed in as an object directly (if using react), or as a string in HTML. If there are multiple checkboxes in a fieldset, each checkbox will be displayed as an option. In the example below, the options are being passed in as a string and there are two checkboxes to be displayed in the fieldset. | `CheckboxOption[] \| string`            | `undefined` |
| `required`       | `required`         | This is used to determine whether the checkbox is required or not. This prop also gets passed to the InputCaption utility to display either an optional or required flag in the label. If no prop is set, it will default to false (optional).                                                                                                                                                                                         | `boolean \| undefined`                  | `false`     |
| `value`          | `value`            | The currently selected checkbox option values. The component keeps the host `value` in sync as users interact with the checkbox group. If `value` is provided, it takes precedence over any `checked` flags passed through `options`. In HTML, pass `value` as a JSON string array.                                                                                                                                                    | `string \| string[] \| undefined`       | `undefined` |

## Events

| Event                | Description                                                                                  | Type                                                         |
| -------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
| `checkboxOnBlur`     | Emitted when a keyboard input event occurs when a checkbox option has lost focus.            | `CustomEvent<InputInteractionEvent & { focused: boolean; }>` |
| `checkboxOnChange`   | Emitted when a keyboard input or mouse event occurs when a checkbox option has been changed. | `CustomEvent<InputInteractionEvent & { checked: boolean; }>` |
| `checkboxOnFocus`    | Emitted when a keyboard input event occurs when a checkbox option has gained focus.          | `CustomEvent<InputInteractionEvent & { focused: boolean; }>` |
| `inputErrorOccurred` | Emitted when an error message is reported to the component.                                  | `CustomEvent<{ errorMessage: string; }>`                     |

## Dependencies

### Depends on

- [ontario-hint-text](../ontario-hint-text)
- [ontario-hint-expander](../ontario-hint-expander)
- [ontario-icon-alert-error](../ontario-icon)

### Graph

```mermaid
graph TD;
  ontario-checkboxes --> ontario-hint-text
  ontario-checkboxes --> ontario-hint-expander
  ontario-checkboxes --> ontario-icon-alert-error
  ontario-hint-expander --> ontario-icon-chevron-up
  ontario-hint-expander --> ontario-icon-chevron-down
  style ontario-checkboxes fill:#f9f,stroke:#333,stroke-width:4px
```

---

_Built with [StencilJS](https://stenciljs.com/)_
