* feat: add `vee-validate` * chore: update * chore: update `AccountForm` example - add `FormDescription` component - include `src` in tsconfig * refactor: use radix-vue `Slot` component * chore: refresh lockfile * chore: update `ProfileForm.vue` and `AccountForm` fix vee-validate initialValues on components with `componentField` slotProp * chore: update `AppearanceForm.vue` update pnpm and some deps -_- * refactor: update - add new-york style - off eslint import/first rule - use `useId` from radix-vue * fix: class-name -> class * refactor: simplify validation for `Command` component * fix: v-bind="field" -> v-bind="componentField" * fix: useAttrs to prevent class duplication * docs: add `form.md` - change TabPreview.vue to showcase way of using vee-validate * docs: add form example for `checkbox` `input` and `datepicker` * docs: add `combobox`, `datepicker`, `radio-group`, `select`, `switch` and `textarea` form and some other exmaples * chore: typo, update `zod`, `vite`, and `@vitejs/plugin-vue`
184 lines
5.7 KiB
Vue
184 lines
5.7 KiB
Vue
<script setup lang="ts">
|
|
import { ref } from 'vue'
|
|
import * as z from 'zod'
|
|
import { format } from 'date-fns'
|
|
import { toTypedSchema } from '@vee-validate/zod'
|
|
import { configure } from 'vee-validate'
|
|
import { Check, ChevronsUpDown } from 'lucide-vue-next'
|
|
import { cn } from '@/lib/utils'
|
|
|
|
import RadixIconsCalendar from '~icons/radix-icons/calendar'
|
|
|
|
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/lib/registry/default/ui/form'
|
|
import { Input } from '@/lib/registry/new-york/ui/input'
|
|
import { Separator } from '@/lib/registry/new-york/ui/separator'
|
|
import {
|
|
Command,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
} from '@/lib/registry/default/ui/command'
|
|
import { Button } from '@/lib/registry/new-york/ui/button'
|
|
import {
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
} from '@/lib/registry/default/ui/popover'
|
|
import { Calendar } from '@/lib/registry/new-york/ui/calendar'
|
|
|
|
const open = ref(false)
|
|
|
|
const languages = [
|
|
{ label: 'English', value: 'en' },
|
|
{ label: 'French', value: 'fr' },
|
|
{ label: 'German', value: 'de' },
|
|
{ label: 'Spanish', value: 'es' },
|
|
{ label: 'Portuguese', value: 'pt' },
|
|
{ label: 'Russian', value: 'ru' },
|
|
{ label: 'Japanese', value: 'ja' },
|
|
{ label: 'Korean', value: 'ko' },
|
|
{ label: 'Chinese', value: 'zh' },
|
|
] as const
|
|
|
|
const accountFormSchema = toTypedSchema(z.object({
|
|
name: z
|
|
.string()
|
|
.min(2, {
|
|
message: 'Name must be at least 2 characters.',
|
|
})
|
|
.max(30, {
|
|
message: 'Name must not be longer than 30 characters.',
|
|
}),
|
|
dob: z.date({
|
|
required_error: 'A date of birth is required.',
|
|
}),
|
|
language: z.string().nonempty({
|
|
message: 'Please select a language.',
|
|
}),
|
|
}))
|
|
|
|
const filterFunction = (list: typeof languages, search: string) => list.filter(i => i.value.toLowerCase().includes(search.toLowerCase()))
|
|
|
|
// https://github.com/logaretm/vee-validate/issues/3521
|
|
// https://github.com/logaretm/vee-validate/discussions/3571
|
|
async function onSubmit(values: any) {
|
|
console.log('Form submitted!', values)
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<h3 class="text-lg font-medium">
|
|
Account
|
|
</h3>
|
|
<p class="text-sm text-muted-foreground">
|
|
Update your account settings. Set your preferred language and timezone.
|
|
</p>
|
|
</div>
|
|
<Separator />
|
|
<Form v-slot="{ setValues }" :validation-schema="accountFormSchema" class="space-y-8" @submit="onSubmit">
|
|
<FormField v-slot="{ componentField }" name="name">
|
|
<FormItem>
|
|
<FormLabel>Name</FormLabel>
|
|
<FormControl>
|
|
<Input type="text" placeholder="Your name" v-bind="componentField" />
|
|
</FormControl>
|
|
<FormDescription>
|
|
This is the name that will be displayed on your profile and in emails.
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
</FormField>
|
|
|
|
<FormField v-slot="{ componentField, value }" name="dob">
|
|
<FormItem>
|
|
<FormLabel>Date of birth</FormLabel>
|
|
<Popover>
|
|
<PopoverTrigger as-child>
|
|
<FormControl>
|
|
<Button
|
|
variant="outline" :class="cn(
|
|
'w-[280px] pl-3 text-left font-normal',
|
|
!value && 'text-muted-foreground',
|
|
)"
|
|
>
|
|
<span>{{ value ? format(value, "PPP") : "Pick a date" }}</span>
|
|
<RadixIconsCalendar class="ml-auto h-4 w-4 opacity-50" />
|
|
</Button>
|
|
</FormControl>
|
|
</PopoverTrigger>
|
|
<PopoverContent class="p-0">
|
|
<Calendar v-bind="componentField" />
|
|
</PopoverContent>
|
|
</Popover>
|
|
<FormDescription>
|
|
Your date of birth is used to calculate your age.
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
</FormField>
|
|
|
|
<FormField v-slot="{ value }" name="language">
|
|
<FormItem>
|
|
<FormLabel>Language</FormLabel>
|
|
|
|
<Popover v-model:open="open">
|
|
<PopoverTrigger as-child>
|
|
<FormControl>
|
|
<Button
|
|
variant="outline" role="combobox" :aria-expanded="open" :class="cn(
|
|
'w-[200px] justify-between',
|
|
!value && 'text-muted-foreground',
|
|
)"
|
|
>
|
|
{{ value ? languages.find(
|
|
(language) => language.value === value,
|
|
)?.label : 'Select language...' }}
|
|
|
|
<ChevronsUpDown class="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
|
</Button>
|
|
</FormControl>
|
|
</PopoverTrigger>
|
|
<PopoverContent class="w-[200px] p-0">
|
|
<Command>
|
|
<CommandInput placeholder="Search language..." />
|
|
<CommandEmpty>No framework found.</CommandEmpty>
|
|
<CommandGroup>
|
|
<CommandItem
|
|
v-for="language in languages" :key="language.value" :value="language.label"
|
|
@select="() => {
|
|
setValues({
|
|
language: language.value,
|
|
})
|
|
open = false
|
|
}"
|
|
>
|
|
<Check
|
|
:class="cn(
|
|
'mr-2 h-4 w-4',
|
|
value === language.value ? 'opacity-100' : 'opacity-0',
|
|
)"
|
|
/>
|
|
{{ language.label }}
|
|
</CommandItem>
|
|
</CommandGroup>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover>
|
|
|
|
<FormDescription>
|
|
This is the language that will be used in the dashboard.
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</FormItem>
|
|
</FormField>
|
|
|
|
<div class="flex justify-start">
|
|
<Button type="submit">
|
|
Update account
|
|
</Button>
|
|
</div>
|
|
</Form>
|
|
</template>
|