mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-26 18:00:43 +01:00
feat(Switch): form integration (#48)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
@@ -1,22 +1,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { z } from 'zod'
|
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({
|
const schema = z.object({
|
||||||
input: z.string().min(10),
|
input: z.string().min(10),
|
||||||
@@ -30,9 +15,9 @@ const schema = z.object({
|
|||||||
// selectMenu: z.any().refine(option => option?.value === 'option-2', {
|
// selectMenu: z.any().refine(option => option?.value === 'option-2', {
|
||||||
// message: 'Select Option 2'
|
// message: 'Select Option 2'
|
||||||
// }),
|
// }),
|
||||||
// toggle: z.boolean().refine(value => value === true, {
|
switch: z.boolean().refine(value => value === true, {
|
||||||
// message: 'Toggle me'
|
message: 'Toggle me'
|
||||||
// }),
|
}),
|
||||||
checkbox: z.boolean().refine(value => value === true, {
|
checkbox: z.boolean().refine(value => value === true, {
|
||||||
message: 'Check me'
|
message: 'Check me'
|
||||||
}),
|
}),
|
||||||
@@ -44,6 +29,9 @@ const schema = z.object({
|
|||||||
|
|
||||||
type Schema = z.output<typeof schema>
|
type Schema = z.output<typeof schema>
|
||||||
|
|
||||||
|
const state = reactive<Partial<Schema>>({})
|
||||||
|
const form = ref<Form<Schema>>()
|
||||||
|
|
||||||
const options = [
|
const options = [
|
||||||
{ label: 'Option 1', value: 'option-1' },
|
{ label: 'Option 1', value: 'option-1' },
|
||||||
{ label: 'Option 2', value: 'option-2' },
|
{ 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" />
|
<URadioGroup v-model="state.radioGroup" legend="Radio group" :options="options" />
|
||||||
</UFormField>
|
</UFormField>
|
||||||
|
|
||||||
|
<UFormField name="switch">
|
||||||
|
<USwitch v-model:checked="state.switch" />
|
||||||
|
</UFormField>
|
||||||
|
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
<UButton color="gray" type="submit">
|
<UButton color="gray" type="submit" :disabled="form?.disabled">
|
||||||
Submit
|
Submit
|
||||||
</UButton>
|
</UButton>
|
||||||
|
|
||||||
<UButton variant="outline" @click="form.clear()">
|
<UButton variant="outline" :disabled="form?.disabled" @click="form.clear()">
|
||||||
Clear
|
Clear
|
||||||
</UButton>
|
</UButton>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,16 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import type { Form, FormSubmitEvent } from '#ui/types/form'
|
import type { 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>>({})
|
|
||||||
|
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
email: z.string().email(),
|
email: z.string().email(),
|
||||||
@@ -18,9 +8,13 @@ const schema = z.object({
|
|||||||
tos: z.literal(true)
|
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)
|
console.log(event.data)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -79,44 +73,14 @@ function onSubmit (event: FormSubmitEvent<User>) {
|
|||||||
</UButton>
|
</UButton>
|
||||||
</div>
|
</div>
|
||||||
</UForm>
|
</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>
|
||||||
|
|
||||||
<div class="flex gap-4">
|
<div class="flex gap-4">
|
||||||
<FormNestedExample />
|
<FormNestedExample />
|
||||||
<FormNestedListExample />
|
<FormNestedListExample />
|
||||||
<FormElementsExample />
|
<FormElementsExample />
|
||||||
|
<FormElementsExample disabled />
|
||||||
</div>
|
</div>
|
||||||
|
<div class="flex gap-4" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type RadioGroupVariants = VariantProps<typeof radioGroup>
|
|||||||
export type RadioGroupOption<T> = {
|
export type RadioGroupOption<T> = {
|
||||||
label: string
|
label: string
|
||||||
value: T
|
value: T
|
||||||
description: string
|
description?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RadioGroupProps<T> extends Omit<RadioGroupRootProps, 'asChild' | 'dir'> {
|
export interface RadioGroupProps<T> extends Omit<RadioGroupRootProps, 'asChild' | 'dir'> {
|
||||||
@@ -49,15 +49,15 @@ const props = defineProps<RadioGroupProps<T>>()
|
|||||||
const emits = defineEmits<RadioGroupEmits>()
|
const emits = defineEmits<RadioGroupEmits>()
|
||||||
defineSlots<RadioGroupSlots<T>>()
|
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 inputId = _inputId.value ?? useId()
|
||||||
|
|
||||||
const ui = computed(() => tv({ extend: radioGroup, slots: props.ui })({
|
const ui = computed(() => tv({ extend: radioGroup, slots: props.ui })({
|
||||||
size: size.value,
|
size: size.value,
|
||||||
color: color.value,
|
color: color.value,
|
||||||
disabled: props.disabled,
|
disabled: disabled.value,
|
||||||
required: props.required
|
required: props.required
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@@ -102,6 +102,7 @@ function onUpdate () {
|
|||||||
v-model="modelValue"
|
v-model="modelValue"
|
||||||
v-bind="rootProps"
|
v-bind="rootProps"
|
||||||
:name="name"
|
:name="name"
|
||||||
|
:disabled="disabled"
|
||||||
:class="ui.root({ class: props.class })"
|
:class="ui.root({ class: props.class })"
|
||||||
@update:model-value="onUpdate"
|
@update:model-value="onUpdate"
|
||||||
>
|
>
|
||||||
@@ -116,6 +117,7 @@ function onUpdate () {
|
|||||||
<RadioGroupItem
|
<RadioGroupItem
|
||||||
:id="option.id"
|
:id="option.id"
|
||||||
:value="option.value"
|
:value="option.value"
|
||||||
|
:disabled="disabled"
|
||||||
:class="ui.base()"
|
:class="ui.base()"
|
||||||
>
|
>
|
||||||
<RadioGroupIndicator :class="ui.indicator()" />
|
<RadioGroupIndicator :class="ui.indicator()" />
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ const switchTv = tv({ extend: tv(theme), ...(appConfig.ui?.switch || {}) })
|
|||||||
type SwitchVariants = VariantProps<typeof switchTv>
|
type SwitchVariants = VariantProps<typeof switchTv>
|
||||||
|
|
||||||
export interface SwitchProps extends Omit<SwitchRootProps, 'asChild'> {
|
export interface SwitchProps extends Omit<SwitchRootProps, 'asChild'> {
|
||||||
|
id?: string
|
||||||
|
name?: string
|
||||||
color?: SwitchVariants['color']
|
color?: SwitchVariants['color']
|
||||||
size?: SwitchVariants['size']
|
size?: SwitchVariants['size']
|
||||||
loading?: boolean
|
loading?: boolean
|
||||||
@@ -30,23 +32,40 @@ export interface SwitchEmits extends SwitchRootEmits {}
|
|||||||
import { computed } from 'vue'
|
import { computed } from 'vue'
|
||||||
import { SwitchRoot, SwitchThumb, useForwardPropsEmits } from 'radix-vue'
|
import { SwitchRoot, SwitchThumb, useForwardPropsEmits } from 'radix-vue'
|
||||||
import { reactivePick } from '@vueuse/core'
|
import { reactivePick } from '@vueuse/core'
|
||||||
import { useAppConfig } from '#imports'
|
import { useAppConfig, useFormField } from '#imports'
|
||||||
|
|
||||||
const props = defineProps<SwitchProps>()
|
const props = defineProps<SwitchProps>()
|
||||||
const emits = defineEmits<SwitchEmits>()
|
const emits = defineEmits<SwitchEmits>()
|
||||||
|
|
||||||
const appConfig = useAppConfig()
|
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 })({
|
const ui = computed(() => tv({ extend: switchTv, slots: props.ui })({
|
||||||
color: props.color,
|
color: color.value,
|
||||||
size: props.size,
|
size: size.value,
|
||||||
loading: props.loading
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<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()">
|
<SwitchThumb :class="ui.thumb()">
|
||||||
<UIcon v-if="loading" :name="loadingIcon || appConfig.ui.icons.loading" :class="ui.icon({ checked: true, unchecked: true })" />
|
<UIcon v-if="loading" :name="loadingIcon || appConfig.ui.icons.loading" :class="ui.icon({ checked: true, unchecked: true })" />
|
||||||
<template v-else>
|
<template v-else>
|
||||||
|
|||||||
@@ -68,7 +68,8 @@ export default (config: { colors: string[] }) => ({
|
|||||||
|
|
||||||
disabled: {
|
disabled: {
|
||||||
true: {
|
true: {
|
||||||
container: 'cursor-not-allowed opacity-75'
|
base: 'cursor-not-allowed opacity-75',
|
||||||
|
label: 'cursor-not-allowed opacity-75'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user