Skip to content
This repository was archived by the owner on Sep 25, 2025. It is now read-only.

Commit 99dc22a

Browse files
committed
feat: checkbox group
fix #186
1 parent 5049a00 commit 99dc22a

File tree

11 files changed

+1363
-4
lines changed

11 files changed

+1363
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script lang="ts" setup>
2+
import { useColor } from '@/app/composables/useColor'
3+
import CheckboxGroup from '@/ui/components/CheckboxGroup/CheckboxGroup.vue'
4+
5+
const { color } = useColor()
6+
7+
const items = [
8+
{ value: '1', label: 'Option 1' },
9+
{ value: '2', label: 'Option 2' },
10+
{ value: '3', label: 'Option 3' },
11+
]
12+
</script>
13+
14+
<template>
15+
<div class="flex flex-col gap-4">
16+
<CheckboxGroup :color="color" :items="items" />
17+
</div>
18+
</template>

.playground/typed-router.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ declare module 'vue-router/auto-routes' {
2828
'/components/card': RouteRecordInfo<'/components/card', '/components/card', Record<never, never>, Record<never, never>>,
2929
'/components/carousel': RouteRecordInfo<'/components/carousel', '/components/carousel', Record<never, never>, Record<never, never>>,
3030
'/components/checkbox': RouteRecordInfo<'/components/checkbox', '/components/checkbox', Record<never, never>, Record<never, never>>,
31+
'/components/checkbox-group': RouteRecordInfo<'/components/checkbox-group', '/components/checkbox-group', Record<never, never>, Record<never, never>>,
3132
'/components/chip': RouteRecordInfo<'/components/chip', '/components/chip', Record<never, never>, Record<never, never>>,
3233
'/components/color-picker': RouteRecordInfo<'/components/color-picker', '/components/color-picker', Record<never, never>, Record<never, never>>,
3334
'/components/command-palette': RouteRecordInfo<'/components/command-palette', '/components/command-palette', Record<never, never>, Record<never, never>>,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import CheckboxGroup from '@/ui/components/CheckboxGroup/CheckboxGroup.vue'
2+
import themeCheckbox from '@/ui/theme/checkbox'
3+
import theme from '@/ui/theme/checkbox-group'
4+
import { render } from '@testing-library/vue'
5+
import { describe, expect, it } from 'vitest'
6+
7+
describe('checkboxGroup', () => {
8+
const sizes = Object.keys(theme.variants.size) as any
9+
const variants = Object.keys(themeCheckbox.variants.variant) as any
10+
const indicators = Object.keys(themeCheckbox.variants.indicator) as any
11+
12+
const items = [
13+
{ value: '1', label: 'Option 1' },
14+
{ value: '2', label: 'Option 2' },
15+
{ value: '3', label: 'Option 3' },
16+
]
17+
18+
const props = { items }
19+
20+
it.each([
21+
['with items', { props }],
22+
['with modelValue', { props: { ...props, modelValue: ['1'] } }],
23+
['with defaultValue', { props: { ...props, defaultValue: ['1'] } }],
24+
['with valueKey', { props: { ...props, valueKey: 'label' } }],
25+
['with labelKey', { props: { ...props, labelKey: 'value' } }],
26+
['with descriptionKey', { props: { ...props, descriptionKey: 'value' } }],
27+
['with disabled', { props: { ...props, disabled: true } }],
28+
['with description', { props: { items: items.map((opt, count) => ({ ...opt, description: `Description ${count}` })) } }],
29+
['with required', { props: { ...props, legend: 'Legend', required: true } }],
30+
...sizes.map((size: string) => [`with size ${size}`, { props: { ...props, size, defaultValue: ['1'] } }]),
31+
...variants.map((variant: string) => [`with primary variant ${variant}`, { props: { ...props, variant, defaultValue: ['1'] } }]),
32+
...variants.map((variant: string) => [`with neutral variant ${variant}`, { props: { ...props, variant, color: 'neutral', defaultValue: ['1'] } }]),
33+
...variants.map((variant: string) => [`with horizontal variant ${variant}`, { props: { ...props, variant, orientation: 'horizontal', defaultValue: ['1'] } }]),
34+
...indicators.map((indicator: string) => [`with indicator ${indicator}`, { props: { ...props, indicator, defaultValue: ['1'] } }]),
35+
['with ariaLabel', { props, attrs: { 'aria-label': 'Aria label' } }],
36+
['with as', { props: { ...props, as: 'section' } }],
37+
['with class', { props: { ...props, class: 'absolute' } }],
38+
['with ui', { props: { ...props, ui: { fieldset: 'gap-x-4', label: 'text-red' } } }],
39+
// Slots
40+
['with legend slot', { props, slots: { legend: () => 'Legend slot' } }],
41+
['with label slot', { props, slots: { label: () => 'Label slot' } }],
42+
['with description slot', { props, slots: { description: () => 'Description slot' } }],
43+
])('renders %s correctly', (name, options) => {
44+
const { html } = render(CheckboxGroup, options)
45+
46+
expect(html()).toMatchSnapshot()
47+
})
48+
})
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<script lang="ts">
2+
import type { CheckboxProps } from '@/ui/components/Checkbox/Checkbox.vue'
3+
import type { AcceptableValue, ComponentConfig } from '@/ui/types/utils'
4+
import type { CheckboxGroupRootEmits, CheckboxGroupRootProps } from 'reka-ui'
5+
import Checkbox from '@/ui/components/Checkbox/Checkbox.vue'
6+
import { useFormField } from '@/ui/composables/useFormField'
7+
import theme from '@/ui/theme/checkbox-group'
8+
import { get } from '@/ui/utils/get'
9+
import { omit } from '@/ui/utils/omit'
10+
import { reactivePick } from '@vueuse/shared'
11+
import { CheckboxGroupRoot, useForwardProps, useForwardPropsEmits } from 'reka-ui'
12+
import { tv } from 'tailwind-variants'
13+
import { computed, useId } from 'vue'
14+
15+
type CheckboxGroup = ComponentConfig<typeof theme>
16+
17+
export type CheckboxGroupValue = AcceptableValue
18+
19+
export type CheckboxGroupItem = {
20+
label?: string
21+
description?: string
22+
disabled?: boolean
23+
value?: string
24+
[key: string]: any
25+
} | CheckboxGroupValue
26+
27+
export interface CheckboxGroupProps<T extends CheckboxGroupItem = CheckboxGroupItem> extends Pick<CheckboxGroupRootProps, 'defaultValue' | 'disabled' | 'loop' | 'modelValue' | 'name' | 'required'>, Pick<CheckboxProps, 'color' | 'variant' | 'indicator' | 'icon'> {
28+
as?: any
29+
legend?: string
30+
valueKey?: string
31+
labelKey?: string
32+
descriptionKey?: string
33+
items?: T[]
34+
size?: CheckboxGroup['variants']['size']
35+
orientation?: CheckboxGroupRootProps['orientation']
36+
class?: any
37+
ui?: CheckboxGroup['slots'] & CheckboxProps['ui']
38+
}
39+
40+
export type CheckboxGroupEmits = CheckboxGroupRootEmits & {
41+
change: [payload: Event]
42+
}
43+
44+
type SlotProps<T extends CheckboxGroupItem> = (props: { item: T & { id: string } }) => any
45+
46+
export interface CheckboxGroupSlots<T extends CheckboxGroupItem = CheckboxGroupItem> {
47+
legend: (props?: object) => any
48+
label: SlotProps<T>
49+
description: SlotProps<T>
50+
}
51+
</script>
52+
53+
<script setup lang="ts" generic="T extends CheckboxGroupItem">
54+
const props = withDefaults(defineProps<CheckboxGroupProps<T>>(), {
55+
valueKey: 'value',
56+
labelKey: 'label',
57+
descriptionKey: 'description',
58+
orientation: 'vertical',
59+
})
60+
const emits = defineEmits<CheckboxGroupEmits>()
61+
const slots = defineSlots<CheckboxGroupSlots<T>>()
62+
63+
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'modelValue', 'defaultValue', 'orientation', 'loop', 'required'), emits)
64+
const checkboxProps = useForwardProps(reactivePick(props, 'variant', 'indicator', 'icon'))
65+
const proxySlots = omit(slots, ['legend'])
66+
67+
const { color, name, size, id: _id, disabled, ariaAttrs } = useFormField<CheckboxGroupProps<T>>(props, { bind: false })
68+
const id = _id.value ?? useId()
69+
70+
const ui = computed(() => tv(theme)({
71+
size: size.value,
72+
required: props.required,
73+
orientation: props.orientation,
74+
}))
75+
76+
function normalizeItem(item: any) {
77+
if (item === null) {
78+
return {
79+
id: `${id}:null`,
80+
value: undefined,
81+
label: undefined,
82+
}
83+
}
84+
85+
if (typeof item === 'string' || typeof item === 'number') {
86+
return {
87+
id: `${id}:${item}`,
88+
value: String(item),
89+
label: String(item),
90+
}
91+
}
92+
93+
const value = get(item, props.valueKey as string)
94+
const label = get(item, props.labelKey as string)
95+
const description = get(item, props.descriptionKey as string)
96+
97+
return {
98+
...item,
99+
value,
100+
label,
101+
description,
102+
id: `${id}:${value}`,
103+
}
104+
}
105+
106+
const normalizedItems = computed(() => {
107+
if (!props.items) {
108+
return []
109+
}
110+
return props.items.map(normalizeItem)
111+
})
112+
113+
function onUpdate(value: any) {
114+
// @ts-expect-error - 'target' does not exist in type 'EventInit'
115+
const event = new Event('change', { target: { value } })
116+
emits('change', event)
117+
}
118+
</script>
119+
120+
<!-- eslint-disable vue/no-template-shadow -->
121+
<template>
122+
<CheckboxGroupRoot
123+
:id="id"
124+
v-bind="rootProps"
125+
:name="name"
126+
:disabled="disabled"
127+
:class="ui.root({ class: [props.class, props.ui?.root] })"
128+
@update:model-value="onUpdate"
129+
>
130+
<fieldset :class="ui.fieldset({ class: props.ui?.fieldset })" v-bind="ariaAttrs">
131+
<legend v-if="legend || !!slots.legend" :class="ui.legend({ class: props.ui?.legend })">
132+
<slot name="legend">
133+
{{ legend }}
134+
</slot>
135+
</legend>
136+
137+
<Checkbox
138+
v-for="item in normalizedItems"
139+
:key="item.value"
140+
v-bind="{ ...item, ...checkboxProps }"
141+
:color="color"
142+
:size="size"
143+
:name="name"
144+
:disabled="item.disabled || disabled"
145+
:ui="props.ui ? omit(props.ui, ['root']) : undefined"
146+
:class="ui.item({ class: props.ui?.item })"
147+
>
148+
<template v-for="(_, name) in proxySlots" #[name]>
149+
<slot :name="(name as keyof CheckboxGroupSlots<T>)" :item="item" />
150+
</template>
151+
</Checkbox>
152+
</fieldset>
153+
</CheckboxGroupRoot>
154+
</template>

0 commit comments

Comments
 (0)