primevue-mirror/apps/showcase/doc/forms/BasicDoc.vue

181 lines
5.6 KiB
Vue
Raw Permalink Normal View History

2024-10-23 04:05:27 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>
2024-10-31 07:56:10 +00:00
All PrimeVue form components are designed for seamless integration with the forms library. Instead of using the standard <i>v-model</i>, the <i>name</i> property is used to link a state object that tracks values, errors, and actions. The
form component provides four key properties for state management.
2024-10-23 04:05:27 +00:00
</p>
</DocSectionText>
2024-10-31 07:56:10 +00:00
<div class="doc-tablewrapper">
<table class="doc-table">
<thead>
<tr>
<th class="w-1/4">Property</th>
<th class="w-3/4">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>v-slot="$form"</td>
<td>Exposes the main <i>$form</i> object that tracks the state management of the fields.</td>
</tr>
<tr>
<td>initialValues</td>
<td>Specifies the default values to initiate the form with.</td>
</tr>
<tr>
<td>resolver</td>
<td>The validation handler to implement validations or to bind a schema like <i>Zod</i>, <i>Yup</i>, <i>Valibot</i> and more.</td>
</tr>
<tr>
<td>@submit</td>
<td>The event handler to execute when the form is submitted.</td>
</tr>
</tbody>
</table>
</div>
2024-10-23 04:05:27 +00:00
<div class="card flex justify-center">
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
2024-10-23 04:05:27 +00:00
<InputText name="username" type="text" placeholder="Username" fluid />
2024-10-31 07:56:10 +00:00
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
2024-10-23 04:05:27 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
<DocSectionCode :code="code" />
</template>
<script>
export default {
data() {
return {
initialValues: {
username: ''
},
code: {
basic: `
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
2024-10-23 04:05:27 +00:00
<InputText name="username" type="text" placeholder="Username" fluid />
2024-10-31 07:56:10 +00:00
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
2024-10-23 04:05:27 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
`,
options: `
<template>
2024-10-23 11:06:59 +00:00
<div class="card flex justify-center">
<Toast />
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
2024-10-23 11:06:59 +00:00
<InputText name="username" type="text" placeholder="Username" fluid />
2024-10-31 07:56:10 +00:00
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
2024-10-23 11:06:59 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
2024-10-23 04:05:27 +00:00
</template>
<script>
export default {
data() {
return {
initialValues: {
username: ''
}
};
},
methods: {
resolver: ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
<\/script>
`,
composition: `
<template>
2024-10-23 11:06:59 +00:00
<div class="card flex justify-center">
<Toast />
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="flex flex-col gap-4 w-full sm:w-56">
2024-10-31 07:56:10 +00:00
<div class="flex flex-col gap-1">
2024-10-23 11:06:59 +00:00
<InputText name="username" type="text" placeholder="Username" fluid />
2024-10-31 07:56:10 +00:00
<Message v-if="$form.username?.invalid" severity="error" size="small" variant="simple">{{ $form.username.error?.message }}</Message>
2024-10-23 11:06:59 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
2024-10-23 04:05:27 +00:00
</template>
<script setup>
2024-10-23 11:06:59 +00:00
import { reactive } from 'vue';
import { useToast } from 'primevue/usetoast';
const toast = useToast();
const initialValues = reactive({
username: ''
});
const resolver = ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
return {
errors
};
};
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({
severity: 'success',
summary: 'Form is submitted.',
life: 3000
});
}
};
2024-10-23 04:05:27 +00:00
<\/script>
`
}
};
},
methods: {
resolver: ({ values }) => {
const errors = {};
if (!values.username) {
errors.username = [{ message: 'Username is required.' }];
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>