shadcn-vue/apps/www/src/lib/registry/default/example/SwitchForm.vue
Sadegh Barati d03067db67
feat: add vee-validate (#85)
* 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`
2023-10-06 12:04:38 +08:00

86 lines
2.4 KiB
Vue

<script setup lang="ts">
import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/zod'
import * as z from 'zod'
import { Button } from '@/lib/registry/default/ui/button'
import {
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
} from '@/lib/registry/default/ui/form'
import { Switch } from '@/lib/registry/default/ui/switch'
const formSchema = toTypedSchema(z.object({
marketing_emails: z.boolean().default(false).optional(),
security_emails: z.boolean(),
}))
const { handleSubmit } = useForm({
validationSchema: formSchema,
initialValues: {
security_emails: true,
},
})
const onSubmit = handleSubmit((values) => {
console.log('Form submitted!', values)
})
</script>
<template>
<form class="w-full space-y-6" @submit="onSubmit">
<div>
<h3 class="mb-4 text-lg font-medium">
Email Notifications
</h3>
<div class="space-y-4">
<FormField v-slot="{ value, handleChange }" name="marketing_emails">
<FormItem class="flex flex-row items-center justify-between rounded-lg border p-4">
<div class="space-y-0.5">
<FormLabel class="text-base">
Marketing emails
</FormLabel>
<FormDescription>
Receive emails about new products, features, and more.
</FormDescription>
</div>
<FormControl>
<Switch
:checked="value"
@update:checked="handleChange"
/>
</FormControl>
</FormItem>
</FormField>
<FormField v-slot="{ value, handleChange }" name="security_emails">
<FormItem class="flex flex-row items-center justify-between rounded-lg border p-4">
<div class="space-y-0.5">
<FormLabel class="text-base">
Security emails
</FormLabel>
<FormDescription>
Receive emails about your account security.
</FormDescription>
</div>
<FormControl>
<Switch
:checked="value"
disabled
aria-readonly
@update:checked="handleChange"
/>
</FormControl>
</FormItem>
</FormField>
</div>
</div>
<Button type="submit">
Submit
</Button>
</form>
</template>