CheckboxGroup
Usage
Use the v-model
directive to control the value of the CheckboxGroup or the default-value
prop to set the initial value when you do not need to control its state.
Items
Use the items
prop as an array of strings or numbers:
<script setup lang="ts">
import type { CheckboxGroupItem, CheckboxGroupValue } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
const value = ref<CheckboxGroupValue[]>(['System'])
</script>
<template>
<UCheckboxGroup v-model="value" :items="items" />
</template>
You can also pass an array of objects with the following properties:
label?: string
description?: string
value?: string
disabled?: boolean
<script setup lang="ts">
import type { CheckboxGroupItem, CheckboxGroupValue } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>([
{
label: 'System',
description: 'This is the first option.',
value: 'system'
},
{
label: 'Light',
description: 'This is the second option.',
value: 'light'
},
{
label: 'Dark',
description: 'This is the third option.',
value: 'dark'
}
])
const value = ref<CheckboxGroupValue[]>([
'system'
])
</script>
<template>
<UCheckboxGroup v-model="value" :items="items" />
</template>
value
property of the object in the v-model
directive or the default-value
prop.Value Key
You can change the property that is used to set the value by using the value-key
prop. Defaults to value
.
<script setup lang="ts">
import type { CheckboxGroupItem, CheckboxGroupValue } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>([
{
label: 'System',
description: 'This is the first option.',
id: 'system'
},
{
label: 'Light',
description: 'This is the second option.',
id: 'light'
},
{
label: 'Dark',
description: 'This is the third option.',
id: 'dark'
}
])
const value = ref<CheckboxGroupValue[]>([
'light'
])
</script>
<template>
<UCheckboxGroup v-model="value" value-key="id" :items="items" />
</template>
Legend
Use the legend
prop to set the legend of the CheckboxGroup.
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
</script>
<template>
<UCheckboxGroup legend="Theme" :default-value="['System']" :items="items" />
</template>
Color
Use the color
prop to change the color of the CheckboxGroup.
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
</script>
<template>
<UCheckboxGroup color="neutral" :default-value="['System']" :items="items" />
</template>
Variant
Use the variant
prop to change the variant of the CheckboxGroup.
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
</script>
<template>
<UCheckboxGroup color="primary" variant="card" :default-value="['System']" :items="items" />
</template>
Size
Use the size
prop to change the size of the CheckboxGroup.
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
</script>
<template>
<UCheckboxGroup size="xl" variant="list" :default-value="['System']" :items="items" />
</template>
Orientation
Use the orientation
prop to change the orientation of the CheckboxGroup. Defaults to vertical
.
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
</script>
<template>
<UCheckboxGroup
orientation="horizontal"
variant="list"
:default-value="['System']"
:items="items"
/>
</template>
Indicator
Use the indicator
prop to change the position or hide the indicator. Defaults to start
.
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
</script>
<template>
<UCheckboxGroup indicator="end" variant="card" :default-value="['System']" :items="items" />
</template>
Disabled
Use the disabled
prop to disable the CheckboxGroup.
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'
const items = ref<CheckboxGroupItem[]>(['System', 'Light', 'Dark'])
</script>
<template>
<UCheckboxGroup disabled :default-value="['System']" :items="items" />
</template>
API
Props
Prop | Default | Type |
---|---|---|
as |
|
The element or component this component should render as. |
legend |
| |
valueKey |
|
When |
labelKey |
|
When |
descriptionKey |
|
When |
items |
| |
size |
|
|
orientation |
|
The orientation the checkbox buttons are laid out. |
defaultValue |
The value of the checkbox when it is initially rendered. Use when you do not need to control its value. | |
disabled |
When | |
loop |
|
Whether keyboard navigation should loop around |
modelValue |
The controlled value of the checkbox. Can be binded with v-model. | |
name |
The name of the field. Submitted with its owning form as part of a name/value pair. | |
required |
When | |
color |
|
|
variant |
|
|
indicator |
|
Position of the indicator. |
icon |
|
The icon displayed when checked. |
ui |
|
Slots
Slot | Type |
---|---|
legend |
|
label |
|
description |
|
Emits
Event | Type |
---|---|
change |
|
update:modelValue |
|
Theme
export default defineAppConfig({
ui: {
checkboxGroup: {
slots: {
root: 'relative',
fieldset: 'flex gap-x-2',
legend: 'mb-1 block font-medium text-default'
},
variants: {
orientation: {
horizontal: {
fieldset: 'flex-row'
},
vertical: {
fieldset: 'flex-col'
}
},
size: {
xs: {
fieldset: 'gap-y-0.5',
legend: 'text-xs'
},
sm: {
fieldset: 'gap-y-0.5',
legend: 'text-xs'
},
md: {
fieldset: 'gap-y-1',
legend: 'text-sm'
},
lg: {
fieldset: 'gap-y-1',
legend: 'text-sm'
},
xl: {
fieldset: 'gap-y-1.5',
legend: 'text-base'
}
},
required: {
true: {
legend: "after:content-['*'] after:ms-0.5 after:text-error"
}
}
},
defaultVariants: {
size: 'md'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
checkboxGroup: {
slots: {
root: 'relative',
fieldset: 'flex gap-x-2',
legend: 'mb-1 block font-medium text-default'
},
variants: {
orientation: {
horizontal: {
fieldset: 'flex-row'
},
vertical: {
fieldset: 'flex-col'
}
},
size: {
xs: {
fieldset: 'gap-y-0.5',
legend: 'text-xs'
},
sm: {
fieldset: 'gap-y-0.5',
legend: 'text-xs'
},
md: {
fieldset: 'gap-y-1',
legend: 'text-sm'
},
lg: {
fieldset: 'gap-y-1',
legend: 'text-sm'
},
xl: {
fieldset: 'gap-y-1.5',
legend: 'text-base'
}
},
required: {
true: {
legend: "after:content-['*'] after:ms-0.5 after:text-error"
}
}
},
defaultVariants: {
size: 'md'
}
}
}
})
]
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'
export default defineConfig({
plugins: [
vue(),
uiPro({
ui: {
checkboxGroup: {
slots: {
root: 'relative',
fieldset: 'flex gap-x-2',
legend: 'mb-1 block font-medium text-default'
},
variants: {
orientation: {
horizontal: {
fieldset: 'flex-row'
},
vertical: {
fieldset: 'flex-col'
}
},
size: {
xs: {
fieldset: 'gap-y-0.5',
legend: 'text-xs'
},
sm: {
fieldset: 'gap-y-0.5',
legend: 'text-xs'
},
md: {
fieldset: 'gap-y-1',
legend: 'text-sm'
},
lg: {
fieldset: 'gap-y-1',
legend: 'text-sm'
},
xl: {
fieldset: 'gap-y-1.5',
legend: 'text-base'
}
},
required: {
true: {
legend: "after:content-['*'] after:ms-0.5 after:text-error"
}
}
},
defaultVariants: {
size: 'md'
}
}
}
})
]
})