|
| 1 | +<script setup lang="ts"> |
| 2 | +import { toTypedSchema } from '@vee-validate/zod' |
| 3 | +import { useForm } from 'vee-validate' |
| 4 | +import { h } from 'vue' |
| 5 | +import { toast } from 'vue-sonner' |
| 6 | +
|
| 7 | +import * as z from 'zod' |
| 8 | +import { Button } from '@/registry/new-york-v4/ui/button' |
| 9 | +import { |
| 10 | + FormControl, |
| 11 | + FormDescription, |
| 12 | + FormField, |
| 13 | + FormItem, |
| 14 | + FormLabel, |
| 15 | + FormMessage, |
| 16 | +} from '@/registry/new-york-v4/ui/form' |
| 17 | +import { |
| 18 | + NumberField, |
| 19 | + NumberFieldContent, |
| 20 | + NumberFieldDecrement, |
| 21 | + NumberFieldIncrement, |
| 22 | + NumberFieldInput, |
| 23 | +} from '@/registry/new-york-v4/ui/number-field' |
| 24 | +
|
| 25 | +const formSchema = toTypedSchema(z.object({ |
| 26 | + payment: z.number().min(10, 'Min 10 euros to send payment').max(5000, 'Max 5000 euros to send payment'), |
| 27 | +})) |
| 28 | +
|
| 29 | +const { handleSubmit, setFieldValue } = useForm({ |
| 30 | + validationSchema: formSchema, |
| 31 | + initialValues: { |
| 32 | + payment: 10, |
| 33 | + }, |
| 34 | +}) |
| 35 | +
|
| 36 | +const onSubmit = handleSubmit((values) => { |
| 37 | + toast({ |
| 38 | + title: 'You submitted the following values:', |
| 39 | + description: h('pre', { class: 'mt-2 w-[340px] rounded-md bg-slate-950 p-4' }, h('code', { class: 'text-white' }, JSON.stringify(values, null, 2))), |
| 40 | + }) |
| 41 | +}) |
| 42 | +</script> |
| 43 | + |
| 44 | +<template> |
| 45 | + <form class="w-2/3 space-y-6" @submit="onSubmit"> |
| 46 | + <FormField v-slot="{ value }" name="payment"> |
| 47 | + <FormItem> |
| 48 | + <FormLabel>Payment</FormLabel> |
| 49 | + <NumberField |
| 50 | + class="gap-2" |
| 51 | + :min="0" |
| 52 | + :format-options="{ |
| 53 | + style: 'currency', |
| 54 | + currency: 'EUR', |
| 55 | + currencyDisplay: 'code', |
| 56 | + currencySign: 'accounting', |
| 57 | + }" |
| 58 | + :model-value="value" |
| 59 | + @update:model-value="(v) => { |
| 60 | + if (v) { |
| 61 | + setFieldValue('payment', v) |
| 62 | + } |
| 63 | + else { |
| 64 | + setFieldValue('payment', undefined) |
| 65 | + } |
| 66 | + }" |
| 67 | + > |
| 68 | + <NumberFieldContent> |
| 69 | + <NumberFieldDecrement /> |
| 70 | + <FormControl> |
| 71 | + <NumberFieldInput /> |
| 72 | + </FormControl> |
| 73 | + <NumberFieldIncrement /> |
| 74 | + </NumberFieldContent> |
| 75 | + </NumberField> |
| 76 | + <FormDescription> |
| 77 | + Enter value between 10 and 5000. |
| 78 | + </FormDescription> |
| 79 | + <FormMessage /> |
| 80 | + </FormItem> |
| 81 | + </FormField> |
| 82 | + <Button type="submit"> |
| 83 | + Submit |
| 84 | + </Button> |
| 85 | + </form> |
| 86 | +</template> |
0 commit comments