mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-18 05:58:07 +01:00
feat(Switch): form integration (#48)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
@@ -14,7 +14,7 @@ type RadioGroupVariants = VariantProps<typeof radioGroup>
|
||||
export type RadioGroupOption<T> = {
|
||||
label: string
|
||||
value: T
|
||||
description: string
|
||||
description?: string
|
||||
}
|
||||
|
||||
export interface RadioGroupProps<T> extends Omit<RadioGroupRootProps, 'asChild' | 'dir'> {
|
||||
@@ -49,15 +49,15 @@ const props = defineProps<RadioGroupProps<T>>()
|
||||
const emits = defineEmits<RadioGroupEmits>()
|
||||
defineSlots<RadioGroupSlots<T>>()
|
||||
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'defaultValue', 'orientation', 'disabled', 'loop', 'name', 'required'), emits)
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'defaultValue', 'orientation', 'loop', 'name', 'required'), emits)
|
||||
|
||||
const { emitFormChange, color, name, size, inputId: _inputId } = useFormField<RadioGroupProps<T>>(props)
|
||||
const { emitFormChange, color, name, size, inputId: _inputId, disabled } = useFormField<RadioGroupProps<T>>(props)
|
||||
const inputId = _inputId.value ?? useId()
|
||||
|
||||
const ui = computed(() => tv({ extend: radioGroup, slots: props.ui })({
|
||||
size: size.value,
|
||||
color: color.value,
|
||||
disabled: props.disabled,
|
||||
disabled: disabled.value,
|
||||
required: props.required
|
||||
}))
|
||||
|
||||
@@ -102,6 +102,7 @@ function onUpdate () {
|
||||
v-model="modelValue"
|
||||
v-bind="rootProps"
|
||||
:name="name"
|
||||
:disabled="disabled"
|
||||
:class="ui.root({ class: props.class })"
|
||||
@update:model-value="onUpdate"
|
||||
>
|
||||
@@ -116,6 +117,7 @@ function onUpdate () {
|
||||
<RadioGroupItem
|
||||
:id="option.id"
|
||||
:value="option.value"
|
||||
:disabled="disabled"
|
||||
:class="ui.base()"
|
||||
>
|
||||
<RadioGroupIndicator :class="ui.indicator()" />
|
||||
|
||||
@@ -13,6 +13,8 @@ const switchTv = tv({ extend: tv(theme), ...(appConfig.ui?.switch || {}) })
|
||||
type SwitchVariants = VariantProps<typeof switchTv>
|
||||
|
||||
export interface SwitchProps extends Omit<SwitchRootProps, 'asChild'> {
|
||||
id?: string
|
||||
name?: string
|
||||
color?: SwitchVariants['color']
|
||||
size?: SwitchVariants['size']
|
||||
loading?: boolean
|
||||
@@ -30,23 +32,40 @@ export interface SwitchEmits extends SwitchRootEmits {}
|
||||
import { computed } from 'vue'
|
||||
import { SwitchRoot, SwitchThumb, useForwardPropsEmits } from 'radix-vue'
|
||||
import { reactivePick } from '@vueuse/core'
|
||||
import { useAppConfig } from '#imports'
|
||||
import { useAppConfig, useFormField } from '#imports'
|
||||
|
||||
const props = defineProps<SwitchProps>()
|
||||
const emits = defineEmits<SwitchEmits>()
|
||||
|
||||
const appConfig = useAppConfig()
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'defaultChecked', 'checked', 'required', 'name', 'id', 'value'), emits)
|
||||
const rootProps = useForwardPropsEmits(reactivePick(props, 'as', 'defaultChecked', 'checked', 'required', 'value'), emits)
|
||||
|
||||
|
||||
const { inputId, emitFormChange, size, color, name, disabled } = useFormField<SwitchProps>(props)
|
||||
|
||||
const ui = computed(() => tv({ extend: switchTv, slots: props.ui })({
|
||||
color: props.color,
|
||||
size: props.size,
|
||||
color: color.value,
|
||||
size: size.value,
|
||||
loading: props.loading
|
||||
}))
|
||||
|
||||
// FIXME: I think there's a race condition between this and the v-model event.
|
||||
// This must be triggered after the value updates, otherwise the form validates
|
||||
// the previous value.
|
||||
async function onChecked () {
|
||||
emitFormChange()
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<SwitchRoot :disabled="disabled || loading" v-bind="rootProps" :class="ui.root({ class: props.class })">
|
||||
<SwitchRoot
|
||||
:id="inputId"
|
||||
:name="name"
|
||||
:disabled="disabled || loading"
|
||||
v-bind="rootProps"
|
||||
:class="ui.root({ class: props.class })"
|
||||
@update:checked="onChecked"
|
||||
>
|
||||
<SwitchThumb :class="ui.thumb()">
|
||||
<UIcon v-if="loading" :name="loadingIcon || appConfig.ui.icons.loading" :class="ui.icon({ checked: true, unchecked: true })" />
|
||||
<template v-else>
|
||||
|
||||
Reference in New Issue
Block a user