Files
ui/src/runtime/components/RadioGroup.vue
Sandro Circi b61696cdca feat(Select/SelectMenu): handle size prop (#133)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
2024-07-02 12:03:10 +02:00

148 lines
4.4 KiB
Vue

<script lang="ts">
import { tv, type VariantProps } from 'tailwind-variants'
import type { RadioGroupRootProps, RadioGroupRootEmits, RadioGroupItemProps } from 'radix-vue'
import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/radio-group'
import type { AcceptableValue } from '../types/utils'
const appConfig = _appConfig as AppConfig & { ui: { radioGroup: Partial<typeof theme> } }
const radioGroup = tv({ extend: tv(theme), ...(appConfig.ui?.radioGroup || {}) })
type RadioGroupVariants = VariantProps<typeof radioGroup>
export interface RadioGroupItem extends Pick<RadioGroupItemProps, 'disabled' | 'value'> {
label?: string
description?: string
}
export interface RadioGroupProps<T> extends Pick<RadioGroupRootProps, 'defaultValue' | 'disabled' | 'loop' | 'modelValue' | 'name' | 'required'> {
/**
* The element or component this component should render as.
* @defaultValue `div`
*/
as?: any
legend?: string
items?: T[]
size?: RadioGroupVariants['size']
color?: RadioGroupVariants['color']
/**
* The orientation the radio buttons are laid out.
* @defaultValue 'vertical'
*/
orientation?: RadioGroupRootProps['orientation']
class?: any
ui?: Partial<typeof radioGroup.slots>
}
export type RadioGroupEmits = RadioGroupRootEmits & {
change: [payload: Event]
}
type SlotProps<T> = (props: { item: T, modelValue?: string }) => any
export interface RadioGroupSlots<T> {
legend(props?: {}): any
label: SlotProps<T>
description: SlotProps<T>
}
</script>
<script setup lang="ts" generic="T extends RadioGroupItem | AcceptableValue">
import { ref, computed } from 'vue'
import { RadioGroupRoot, RadioGroupItem, RadioGroupIndicator, Label, useForwardPropsEmits } from 'radix-vue'
import { reactivePick } from '@vueuse/core'
import { useId, useFormField } from '#imports'
const props = withDefaults(defineProps<RadioGroupProps<T>>(), {
orientation: 'vertical'
})
const emits = defineEmits<RadioGroupEmits>()
const slots = defineSlots<RadioGroupSlots<T>>()
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'modelValue', 'defaultValue', 'orientation', 'loop', 'required'), emits)
const { emitFormChange, emitFormInput, color, name, size, id: _id, disabled } = useFormField<RadioGroupProps<T>>(props)
const id = _id.value ?? useId()
const ui = computed(() => tv({ extend: radioGroup, slots: props.ui })({
size: size.value,
color: color.value,
disabled: disabled.value,
required: props.required,
orientation: props.orientation
}))
function normalizeItem(item: any) {
if (['string', 'number', 'boolean'].includes(typeof item)) {
return {
id: `${id}:${item}`,
value: item,
label: item
}
}
return {
...item,
id: `${id}:${item.value}`
}
}
const normalizedItems = computed(() => {
if (!props.items) return []
return props.items.map(normalizeItem)
})
function onUpdate(value: any) {
// @ts-expect-error - 'target' does not exist in type 'EventInit'
const event = new Event('change', { target: { value } })
emits('change', event)
emitFormChange()
emitFormInput()
}
</script>
<template>
<RadioGroupRoot
:id="id"
v-slot="{ modelValue }"
v-bind="rootProps"
:name="name"
:disabled="disabled"
:class="ui.root({ class: props.class })"
@update:model-value="onUpdate"
>
<fieldset :class="ui.fieldset()">
<legend v-if="legend || !!slots.legend" :class="ui.legend()">
<slot name="legend">
{{ legend }}
</slot>
</legend>
<div v-for="item in normalizedItems" :key="item.value" :class="ui.item()">
<div :class="ui.container()">
<RadioGroupItem
:id="item.id"
:value="item.value"
:disabled="disabled"
:class="ui.base()"
>
<RadioGroupIndicator :class="ui.indicator()" />
</RadioGroupItem>
</div>
<div :class="ui.wrapper()">
<Label :class="ui.label()" :for="item.id">
<slot name="label" :item="item" :model-value="modelValue">{{ item.label }}</slot>
</Label>
<p v-if="item.description || !!slots.description" :class="ui.description()">
<slot name="description" :item="item" :model-value="modelValue">
{{ item.description }}
</slot>
</p>
</div>
</div>
</fieldset>
</RadioGroupRoot>
</template>