feat(Switch): form integration (#48)

Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Romain Hamel
2024-04-08 12:15:18 +02:00
committed by GitHub
parent 2dfaea580a
commit ebb7c074af
5 changed files with 54 additions and 76 deletions

View File

@@ -1,22 +1,7 @@
<script setup lang="ts">
import { z } from 'zod'
import type { FormSubmitEvent } from '#ui/types/form'
import type { FormSubmitEvent, Form } from '#ui/types/form'
const form = ref()
const state = reactive({
input: undefined,
inputMenu: undefined,
textarea: undefined,
select: undefined,
selectMenu: undefined,
checkbox: undefined,
toggle: undefined,
radio: undefined,
radioGroup: undefined,
switch: undefined,
range: undefined
})
const schema = z.object({
input: z.string().min(10),
@@ -30,9 +15,9 @@ const schema = z.object({
// selectMenu: z.any().refine(option => option?.value === 'option-2', {
// message: 'Select Option 2'
// }),
// toggle: z.boolean().refine(value => value === true, {
// message: 'Toggle me'
// }),
switch: z.boolean().refine(value => value === true, {
message: 'Toggle me'
}),
checkbox: z.boolean().refine(value => value === true, {
message: 'Check me'
}),
@@ -44,6 +29,9 @@ const schema = z.object({
type Schema = z.output<typeof schema>
const state = reactive<Partial<Schema>>({})
const form = ref<Form<Schema>>()
const options = [
{ label: 'Option 1', value: 'option-1' },
{ label: 'Option 2', value: 'option-2' },
@@ -79,12 +67,16 @@ function onSubmit (event: FormSubmitEvent<Schema>) {
<URadioGroup v-model="state.radioGroup" legend="Radio group" :options="options" />
</UFormField>
<UFormField name="switch">
<USwitch v-model:checked="state.switch" />
</UFormField>
<div class="flex gap-2">
<UButton color="gray" type="submit">
<UButton color="gray" type="submit" :disabled="form?.disabled">
Submit
</UButton>
<UButton variant="outline" @click="form.clear()">
<UButton variant="outline" :disabled="form?.disabled" @click="form.clear()">
Clear
</UButton>
</div>

View File

@@ -1,16 +1,6 @@
<script setup lang="ts">
import { z } from 'zod'
import type { Form, FormSubmitEvent } from '#ui/types/form'
type User = {
email: string
password: string
tos: boolean
}
const state = reactive<Partial<User>>({})
const state2 = reactive<Partial<User>>({})
const state3 = reactive<Partial<User>>({})
import type { FormSubmitEvent } from '#ui/types/form'
const schema = z.object({
email: z.string().email(),
@@ -18,9 +8,13 @@ const schema = z.object({
tos: z.literal(true)
})
const disabledForm = ref<Form<User>>()
type Schema = z.output<typeof schema>
function onSubmit (event: FormSubmitEvent<User>) {
const state = reactive<Partial<Schema>>({})
const state2 = reactive<Partial<Schema>>({})
function onSubmit (event: FormSubmitEvent<Schema>) {
console.log(event.data)
}
</script>
@@ -79,44 +73,14 @@ function onSubmit (event: FormSubmitEvent<User>) {
</UButton>
</div>
</UForm>
<UForm
ref="disabledForm"
:state="state3"
:schema="schema"
class="gap-4 flex flex-col w-60"
disabled
@submit="(event) => onSubmit(event)"
>
<UFormField label="Email" name="email">
<UInput v-model="state3.email" placeholder="john@lennon.com" />
</UFormField>
<UFormField
label="Password"
name="password"
:validate-on-input-delay="50"
eager-validation
>
<UInput v-model="state3.password" type="password" />
</UFormField>
<UFormField name="tos">
<UCheckbox v-model="state3.tos" label="I accept the terms and conditions" />
</UFormField>
<div>
<UButton color="gray" type="submit" :disabled="disabledForm?.disabled">
Submit
</UButton>
</div>
</UForm>
</div>
<div class="flex gap-4">
<FormNestedExample />
<FormNestedListExample />
<FormElementsExample />
<FormElementsExample disabled />
</div>
<div class="flex gap-4" />
</div>
</template>

View File

@@ -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()" />

View File

@@ -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>

View File

@@ -68,7 +68,8 @@ export default (config: { colors: string[] }) => ({
disabled: {
true: {
container: 'cursor-not-allowed opacity-75'
base: 'cursor-not-allowed opacity-75',
label: 'cursor-not-allowed opacity-75'
}
},