CheckboxGroup
Basic Usage
<script setup>
import { CheckboxGroup, Checkbox } from '@8ctavio/vergil/components'
import { useModel } from '@8ctavio/vergil'
const planets = useModel([])
const ships = useModel([])
const planetOptions = {
earth: 'Earth',
reach: 'Reach',
harvest: 'Harvest'
}
</script>
<template>
<!-- options prop: Checkbox value-label pairs -->
<CheckboxGroup v-model="planets" :options="planetOptions" label="Planets"/>
<!-- default slot -->
<CheckboxGroup v-model="ships" label="Ships">
<Checkbox value="pillarOfAutumn" label="Pillar of Autumn" checked/>
<Checkbox value="inAmberClad" label="In Amber Clad" theme="user"/>
<Checkbox value="forwardUntoDawn" label="Forward Unto Dawn"/>
</CheckboxGroup>
</template>Props
Props that affect a Checkbox component's appereance are passed to (and, therefore, shared between) all underlying CheckboxGroup's Checkbox components.
Options options: (string | [string, string])[] | Record<string, string | [string, string]> = []
The options prop is an array or (plain) object that provides the required data to define each option's value, label, and description, which correspond to the value, label, and description props of each underlying Checkbox components.
Options' values, labels, and descriptions can be automatically inferred from the values of options — each denoted as option — if they're either a string or an array of strings.
The following table summarizes how option's label and description are inferred from an option.
Type of option | label | description |
|---|---|---|
string | option | undefined |
string[] | option[0] | option[1] |
The following table summarizes how an option's value is inferred depending on the type of options.
Type of options | value |
|---|---|
Array | Same as label |
object | The option's key |
Example. Options Array
<script setup>
const options = [
'Option 1',
['Option 2', 'Description 2']
]
</script>
<template>
<CheckboxGroup v-model="checked" :options/>
</template>checked.value === ''Example. Options Object
<script setup>
const options = {
value1: 'Option 1',
value2: ['Option 2', 'Description 2']
}
</script>
<template>
<CheckboxGroup v-model="checked" :options/>
</template>checked.value === ''TIP
The CheckboxGroup default slot may be used instead to directly pass Checkbox components. In such case, the options prop is ignored and CheckboxGroup group-level props may be overwritten on each Checkbox component.
Option's attributes option-[value|label|description]: (string | Function)
The option-value, option-label, and option-description props allow to specify custom options' values, labels, and descriptions.
As strings, these props represent an object key. If an option is an object, the resulting value/label/description is obtained by accessing that object with the provided key.
<script setup>
const options = [{
id: '123',
name: 'Abc Def',
email: 'abc.def@vergil.com'
},{
id: '456',
name: 'Uvw Xyz',
email: 'uvw.xyz@vergil.com'
}]
</script>
<template>
<CheckboxGroup v-model="checked" :options
option-value="id"
option-label="name"
option-description="email"
/>
</template>checked.value === ''As functions, these props are called for each option, receive the option and its key (index for arrays) as arguments, and their return value becomes the resulting value/label/description.
<script setup>
const options = [{
id: '123',
name: 'Abc Def',
email: 'abc.def@vergil.com'
},{
id: '456',
name: 'Uvw Xyz',
email: 'uvw.xyz@vergil.com'
}]
</script>
<template>
<CheckboxGroup v-model="checked" :options
:option-value="option => kebabCase(option.name)"
:option-label="option => option.name.split(' ')[0]"
:option-description="option => `@@mail@@ ${option.email}`"
/>
</template>checked.value === ''The following functions are the default values for the option-value, option-label, and option-description props.
function defaultOptionValue(option, key) {
return typeof key === 'number'
? Array.isArray(option) ? option[0] : option
: key
}
function defaultOptionLabel(option) {
return Array.isArray(option) ? option[0] : option
}
function defaultOptionDescription {
return Array.isArray(option) ? option[1] : undefined
}Options' attributes options-attributes: Record<string, unknown> | ((...Args) => Record<string, unknown>)
As an object, options-attributes contains additional attributes to apply to all underlying Checkbox components.
<CheckboxGroup
:options
:options-attributes="{
'data-custom': 'data'
}"
/>As a function, options-attributes is called for each option, receives the computed Checkbox component's key, value, label, and description, and its return object becomes the additional attributes.
<CheckboxGroup
:options
:options-attributes="(key,value,label,description) => ({
/* additional attributes */
})
/>Variant variant: ('classic' | 'card' | 'toggle' | 'list') = 'classic'
Show symbol show-symbol: boolean
Direction direction: ('column' | 'row') = 'column'
Theme theme: theme = 'brand'
Size size: ('xs' | 'sm' | 'md' | 'lg' | 'xl') = 'md'
Radius radius: ('none' | 'sm' | 'md' | 'lg' | 'full') = 'full'
Spacing spacing: ('compact' | 'expanded') = ''
Disabled disabled: boolean
Elements
| element | tag | description |
|---|---|---|
options | <div.toggle-group-wrapper> | Wrapper of underlying Checkbox components. |
Anatomy
<div.form-field.checkbox-group/>
<div.form-field-label-wrapper/>
<label.form-field-label/>
<span.form-field-hint/>
<p.form-field-details.form-field-description/>
<div.toggle-group-wrapper/>
<slot name="default"/>
<Checkbox v-for="(text,value) in options"/>
<p.form-field-details.form-field-help/>
API Reference
Props
| prop | type | default |
|---|---|---|
value | array | string | [] |
options | (string | [string, string])[] | Record<string, string | [string, string]> | [] |
optionValue | string | ((option: unknown, key: string | number) => string) | |
optionLabel | string | ((option: unknown, key: string | number) => string) | |
optionDescription | string | ((option: unknown, key: string | number) => string) | |
optionsAttributes | Record<string, unknown> | ((key: string | number, value: string, label: string, description: string) => Record<string, unknown>) | |
variant | 'classic' | 'card' | 'list' | 'toggle' | 'classic' |
showSymbol | boolean | |
direction | 'column' | 'row' | 'column' |
disabled | boolean | |
label | string | |
hint | string | |
description | string | |
help | string | |
showErrros | boolean | |
descendant | boolean | |
theme | 'brand' | 'user' | 'ok' | 'info' | 'warn' | 'danger' | 'neutral' | 'brand' |
size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' |
radius | 'none' | 'sm' | 'md' | 'lg' | 'full' | 'md' |
spacing | '' | 'compact' | 'extended' | '' |
validator | Function | |
eagerValidation | boolean | |
elements | object |
Configuration options
The CheckboxGroup is configured through the Checkbox configuration options.