# Checkbox

Source: https://ecosphere.dev/docs/forms/checkbox (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

A labelled on/off choice, with an optional hint, and checkgroup for a set of related choices under one legend.

`import { EcoCheckbox, EcoCheckboxGroup } from "vue-ecosphere"`

## States

The indeterminate box follows the group below.

Vue (vue-ecosphere):

```vue
<EcoCheckbox v-model="a" label="Unchecked" />
<EcoCheckbox v-model="b" label="Checked" />
<EcoCheckbox v-model="all" :indeterminate="some" label="Indeterminate: some rows chosen" />
<EcoCheckbox label="Disabled" disabled />
<EcoCheckbox :model-value="true" label="Checked and disabled" disabled />
```

React (react-ecosphere):

```tsx
const [a, setA] = useState(false);
const [b, setB] = useState(true);
const [columns, setColumns] = useState(["request", "owner"]);
const all = columns.length === 3;
const some = columns.length > 0 && !all;

<EcoCheckbox checked={a} onChange={setA} label="Unchecked" />
<EcoCheckbox checked={b} onChange={setB} label="Checked" />
<EcoCheckbox checked={all} onChange={(v) => setColumns(v ? ["request", "owner", "history"] : [])}
  indeterminate={some} label="Indeterminate: some rows chosen" />
<EcoCheckbox label="Disabled" disabled />
<EcoCheckbox checked label="Checked and disabled" disabled />
```

## With a hint

Vue (vue-ecosphere):

```vue
<EcoCheckbox v-model="assigned" label="Email me when a request is assigned"
  hint="Sent once per request, never as a digest." />
<EcoCheckbox label="Send a weekly summary to the team" disabled
  hint="Only workspace admins can turn this on." />
```

React (react-ecosphere):

```tsx
const [assigned, setAssigned] = useState(true);

<EcoCheckbox checked={assigned} onChange={setAssigned} label="Email me when a request is assigned"
  hint="Sent once per request, never as a digest." />
<EcoCheckbox label="Send a weekly summary to the team" disabled
  hint="Only workspace admins can turn this on." />
```

## Group

Vue (vue-ecosphere):

```vue
<EcoCheckboxGroup v-model="columns" label="Columns to export" :options="[
  { label: 'Request', value: 'request' },
  { label: 'Owner', value: 'owner' },
  { label: 'Status history', value: 'history' },
]" />
```

React (react-ecosphere):

```tsx
const [columns, setColumns] = useState(["request", "owner"]);

<EcoCheckboxGroup value={columns} onChange={setColumns} label="Columns to export" options={[
  { label: "Request", value: "request" },
  { label: "Owner", value: "owner" },
  { label: "Status history", value: "history" },
]} />
```

## Group in a row, invalid

One message for the group, not one per box. Check any option to clear it.

Vue (vue-ecosphere):

```vue
<EcoCheckboxGroup v-model="notify" label="Notify by" required row
  :options="notifyOptions"
  :error="notify.length ? undefined : 'Choose at least one way to be notified.'" />
```

React (react-ecosphere):

```tsx
const [notify, setNotify] = useState<string[]>([]);

<EcoCheckboxGroup value={notify} onChange={setNotify} label="Notify by" required row
  options={notifyOptions}
  error={notify.length ? undefined : "Choose at least one way to be notified."} />
```

## EcoCheckbox

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `boolean` | false | Checked state. |
| `label` | `string` | — | The thing that becomes true when checked. Default slot overrides. |
| `hint` | `string` | — | Second line (checkbox__h), wired with aria-describedby. |
| `indeterminate` | `boolean` | false | Set as a property on the input, for a partly checked parent. |
| `disabled` | `boolean` | false | Say why in the hint. |
| `invalid` | `boolean` | false | aria-invalid on the box. |
| `value / name` | `string` | — | Native form attributes. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `checked / onChange` | `boolean` | false | Checked state (defaultChecked when uncontrolled); onChange receives the boolean. |
| `label` | `string` | — | The thing that becomes true when checked. children override. |
| `hint` | `string` | — | Second line (checkbox__h), wired with aria-describedby. |
| `indeterminate` | `boolean` | false | Set as a property on the input, for a partly checked parent. |
| `disabled` | `boolean` | false | Say why in the hint. |
| `invalid` | `boolean` | false | aria-invalid on the box. |
| `value / name` | `string` | — | Native form attributes. |

## EcoCheckboxGroup

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `(string \| number)[]` | [] | The checked values. |
| `options` | `EcoOption[]` | — | { label, value, hint?, disabled? } |
| `label / required` | `—` | — | The legend (field__l) and its req mark. |
| `row` | `boolean` | false | Short options side by side (checkgroup--row). |
| `hint / error` | `string` | — | One line under the group; the error marks every box invalid. |
| `disabled / name` | `—` | — | Applied to every box. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value / onChange` | `(string \| number)[]` | [] | The checked values (defaultValue when uncontrolled). |
| `options` | `EcoOption[]` | — | { label, value, hint?, disabled? } |
| `label / required` | `—` | — | The legend (field__l) and its req mark. |
| `row` | `boolean` | false | Short options side by side (checkgroup--row). |
| `hint / error` | `string` | — | One line under the group; the error marks every box invalid. |
| `disabled / name` | `—` | — | Applied to every box. |
