Checkbox
Use a checkbox when selecting multiple options from a list or turning an item on / off. They are used in most forms that require a save or add action.
Checkbox is a type of input that falls into the selection control family. This also includes radio group and switch. Only the UI in switch and checkbox is different, the code is identical.
Single variant
Standard
The size of the checkbox never changes. Label and hint text can be any width or wrap to multiple lines depending on use case.
Basic Usage
<Checkbox v-model:checked="isCheckedLabel" name="checkbox-label" label="With a label" />
Playground
Anatomy
- Checkbox field (target size)
- Checkbox
- Label Text
- Hint Text
- Icon
Standalone
Standalone checkboxes can be used to select items like rows in a table
Checked: false
<Checkbox
v-model:checked="isChecked"
id="checkbox-1"
name="checkbox-1"
/>
Multiline
<Checkbox
label="I swear on my stash that (a) my details are rock-solid, no munchie-induced memory lapses here, and (b) my entity is alive and well, not vanished like that last bit of smoke from a well-enjoyed joint."
/>
Indeterminate
A Checkbox can be made indeterminate by setting the indeterminate
property
Checked: false
<Checkbox indeterminate label="Indeterminate" />
<Checkbox disabled indeterminate label="Indeterminate and disabled" />
Hint text
Provided as a prop
<Checkbox hint-text="Hint text as a prop" label="Opt in" />
Provided as a slot
<Checkbox label="Opt in">
<template #hint>
Hint text as a <strong>slot</strong>
</template>
</Checkbox>
Error text
<Checkbox error-text="Please read and agree to the terms and conditions" label="I have read and agree to the terms and conditions" />
Disabled
Checkboxes can be disabled by setting the disabled
property
<Checkbox disabled label="Checked" />
<Checkbox disabled label="Unchecked" hint-text="Can't touch this" />
Checkbox Values
A string or number representing the value of the checkbox.
Checked: [ "1" ]
const checkboxesWithValues = ref([
{
label: 'Checkbox 1',
id: 'checkbox-value-1',
name: 'checkbox-value-1',
value: '1',
checked: true,
},
{
label: 'Checkbox 2',
id: 'checkbox-value-2',
name: 'checkbox-value-2',
value: '2',
checked: false,
},
{
label: 'Checkbox 3',
id: 'checkbox-value-3',
name: 'checkbox-value-3',
value: '3',
checked: false,
},
]);
const selectedCheckboxValues = computed(() => {
return checkboxesWithValues.value.filter((input) => input.checked).map((input) => input.value)
})
<Checkbox
v-for="checkbox in checkboxesWithValues"
v-bind="checkbox"
v-model:checked="checkbox.checked"
/>
Checked: {{ selectedCheckboxValues }}
API
See the documentation below for a complete reference to all the props and classes available to the components mentioned here.