-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Benjamin Canac <[email protected]>
- Loading branch information
1 parent
1667e5a
commit 319fce1
Showing
13 changed files
with
698 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
docs/app/components/content/examples/form/FormExampleBasic.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<script setup lang="ts"> | ||
import type { FormError, FormSubmitEvent } from '#ui/types' | ||
const state = reactive({ | ||
email: undefined, | ||
password: undefined | ||
}) | ||
const validate = (state: any): FormError[] => { | ||
const errors = [] | ||
if (!state.email) errors.push({ name: 'email', message: 'Required' }) | ||
if (!state.password) errors.push({ name: 'password', message: 'Required' }) | ||
return errors | ||
} | ||
const toast = useToast() | ||
async function onSubmit(event: FormSubmitEvent<any>) { | ||
toast.add({ title: 'Success', description: 'The form has been submitted.', color: 'green' }) | ||
console.log(event.data) | ||
} | ||
</script> | ||
|
||
<template> | ||
<UForm :validate="validate" :state="state" class="space-y-4" @submit="onSubmit"> | ||
<UFormField label="Email" name="email"> | ||
<UInput v-model="state.email" /> | ||
</UFormField> | ||
|
||
<UFormField label="Password" name="password"> | ||
<UInput v-model="state.password" type="password" /> | ||
</UFormField> | ||
|
||
<UButton type="submit"> | ||
Submit | ||
</UButton> | ||
</UForm> | ||
</template> |
115 changes: 115 additions & 0 deletions
115
docs/app/components/content/examples/form/FormExampleElements.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<script setup lang="ts"> | ||
import { z } from 'zod' | ||
import type { FormSubmitEvent, Form } from '@nuxt/ui' | ||
const schema = z.object({ | ||
input: z.string().min(10), | ||
inputMenu: z.any().refine(option => option?.value === 'option-2', { | ||
message: 'Select Option 2' | ||
}), | ||
inputMenuMultiple: z.any().refine(values => !!values?.find((option: any) => option.value === 'option-2'), { | ||
message: 'Include Option 2' | ||
}), | ||
textarea: z.string().min(10), | ||
select: z.string().refine(value => value === 'option-2', { | ||
message: 'Select Option 2' | ||
}), | ||
selectMenu: z.any().refine(option => option?.value === 'option-2', { | ||
message: 'Select Option 2' | ||
}), | ||
selectMenuMultiple: z.any().refine(values => !!values?.find((option: any) => option.value === 'option-2'), { | ||
message: 'Include Option 2' | ||
}), | ||
switch: z.boolean().refine(value => value === true, { | ||
message: 'Toggle me' | ||
}), | ||
checkbox: z.boolean().refine(value => value === true, { | ||
message: 'Check me' | ||
}), | ||
radioGroup: z.string().refine(value => value === 'option-2', { | ||
message: 'Select Option 2' | ||
}), | ||
slider: z.number().max(20, { message: 'Must be less than 20' }) | ||
}) | ||
type Schema = z.output<typeof schema> | ||
const state = reactive<Partial<Schema>>({}) | ||
const form = ref<Form<Schema>>() | ||
const items = [ | ||
{ label: 'Option 1', value: 'option-1' }, | ||
{ label: 'Option 2', value: 'option-2' }, | ||
{ label: 'Option 3', value: 'option-3' } | ||
] | ||
const toast = useToast() | ||
async function onSubmit(event: FormSubmitEvent<any>) { | ||
toast.add({ title: 'Success', description: 'The form has been submitted.', color: 'green' }) | ||
console.log(event.data) | ||
} | ||
</script> | ||
|
||
<template> | ||
<UForm ref="form" :state="state" :schema="schema" @submit="onSubmit"> | ||
<div class="grid grid-cols-3 gap-4"> | ||
<UFormField label="Input" name="input"> | ||
<UInput v-model="state.input" placeholder="[email protected]" class="w-40" /> | ||
</UFormField> | ||
|
||
<div class="flex flex-col gap-4"> | ||
<UFormField name="switch"> | ||
<USwitch v-model="state.switch" label="Switch me" /> | ||
</UFormField> | ||
|
||
<UFormField name="checkbox"> | ||
<UCheckbox v-model="state.checkbox" label="Check me" /> | ||
</UFormField> | ||
</div> | ||
|
||
<UFormField name="slider" label="Slider"> | ||
<USlider v-model="state.slider" /> | ||
</UFormField> | ||
|
||
<UFormField name="select" label="Select"> | ||
<USelect v-model="state.select" :items="items" /> | ||
</UFormField> | ||
|
||
<UFormField name="selectMenu" label="Select Menu"> | ||
<USelectMenu v-model="state.selectMenu" :items="items" /> | ||
</UFormField> | ||
|
||
<UFormField name="selectMenuMultiple" label="Select Menu (Multiple)"> | ||
<USelectMenu v-model="state.selectMenuMultiple" multiple :items="items" /> | ||
</UFormField> | ||
|
||
<UFormField name="inputMenu" label="Input Menu"> | ||
<UInputMenu v-model="state.inputMenu" :items="items" /> | ||
</UFormField> | ||
|
||
<UFormField name="inputMenuMultiple" label="Input Menu (Multiple)"> | ||
<UInputMenu v-model="state.inputMenuMultiple" multiple :items="items" /> | ||
</UFormField> | ||
|
||
<span /> | ||
|
||
<UFormField label="Textarea" name="textarea"> | ||
<UTextarea v-model="state.textarea" /> | ||
</UFormField> | ||
|
||
<UFormField name="radioGroup"> | ||
<URadioGroup v-model="state.radioGroup" legend="Radio group" :items="items" /> | ||
</UFormField> | ||
</div> | ||
|
||
<div class="flex gap-2 mt-8"> | ||
<UButton color="gray" type="submit" :disabled="form?.disabled"> | ||
Submit | ||
</UButton> | ||
|
||
<UButton color="gray" variant="outline" :disabled="form?.disabled" @click="form?.clear()"> | ||
Clear | ||
</UButton> | ||
</div> | ||
</UForm> | ||
</template> |
38 changes: 38 additions & 0 deletions
38
docs/app/components/content/examples/form/FormExampleJoi.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<script setup lang="ts"> | ||
import Joi from 'joi' | ||
import type { FormSubmitEvent } from '#ui/types' | ||
const schema = Joi.object({ | ||
email: Joi.string().required(), | ||
password: Joi.string() | ||
.min(8) | ||
.required() | ||
}) | ||
const state = reactive({ | ||
email: undefined, | ||
password: undefined | ||
}) | ||
const toast = useToast() | ||
async function onSubmit(event: FormSubmitEvent<any>) { | ||
toast.add({ title: 'Success', description: 'The form has been submitted.', color: 'green' }) | ||
console.log(event.data) | ||
} | ||
</script> | ||
|
||
<template> | ||
<UForm :schema="schema" :state="state" class="space-y-4" @submit="onSubmit"> | ||
<UFormField label="Email" name="email"> | ||
<UInput v-model="state.email" /> | ||
</UFormField> | ||
|
||
<UFormField label="Password" name="password"> | ||
<UInput v-model="state.password" type="password" /> | ||
</UFormField> | ||
|
||
<UButton type="submit"> | ||
Submit | ||
</UButton> | ||
</UForm> | ||
</template> |
54 changes: 54 additions & 0 deletions
54
docs/app/components/content/examples/form/FormExampleNested.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<script setup lang="ts"> | ||
import { z } from 'zod' | ||
import type { FormSubmitEvent } from '@nuxt/ui' | ||
const schema = z.object({ | ||
name: z.string().min(2), | ||
news: z.boolean() | ||
}) | ||
type Schema = z.output<typeof schema> | ||
const nestedSchema = z.object({ | ||
email: z.string().email() | ||
}) | ||
type NestedSchema = z.output<typeof nestedSchema> | ||
const state = reactive<Partial<Schema & NestedSchema>>({ }) | ||
const toast = useToast() | ||
async function onSubmit(event: FormSubmitEvent<any>) { | ||
toast.add({ title: 'Success', description: 'The form has been submitted.', color: 'green' }) | ||
console.log(event.data) | ||
} | ||
</script> | ||
|
||
<template> | ||
<UForm | ||
:state="state" | ||
:schema="schema" | ||
class="gap-4 flex flex-col w-60" | ||
@submit="(event) => onSubmit(event)" | ||
> | ||
<UFormField label="Name" name="name"> | ||
<UInput v-model="state.name" placeholder="John Lennon" /> | ||
</UFormField> | ||
|
||
<div> | ||
<UCheckbox v-model="state.news" name="news" label="Register to our newsletter" /> | ||
</div> | ||
|
||
<UForm v-if="state.news" :state="state" :schema="nestedSchema"> | ||
<UFormField label="Email" name="email"> | ||
<UInput v-model="state.email" placeholder="[email protected]" /> | ||
</UFormField> | ||
</UForm> | ||
|
||
<div> | ||
<UButton type="submit"> | ||
Submit | ||
</UButton> | ||
</div> | ||
</UForm> | ||
</template> |
Oops, something went wrong.