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

166 lines
5.3 KiB
Vue
Raw Normal View History

2024-10-23 04:05:27 +00:00
<template>
<DocSectionText v-bind="$attrs">
2024-10-31 07:56:10 +00:00
<p>The <i>$form</i> object tracks the state management of the fields. Each field is linked with the <i>name</i> property. View the <i>FormFieldState</i> type in the API documentation for details about each property.</p>
2024-10-23 04:05:27 +00:00
</DocSectionText>
<div class="card flex justify-center">
2024-10-23 09:27:41 +00:00
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="grid lg:grid-cols-2 gap-4 w-full">
2024-10-23 04:05:27 +00:00
<div class="flex flex-col justify-center items-center gap-4">
2024-10-24 06:33:42 +00:00
<InputText name="username" type="text" placeholder="Username" class="w-full sm:w-56" />
<Button type="submit" severity="secondary" label="Submit" class="w-full sm:w-56" />
2024-10-23 04:05:27 +00:00
</div>
2024-10-31 07:56:10 +00:00
<Fieldset legend="Form States" class="h-80 overflow-auto">
2024-10-23 09:27:41 +00:00
<pre class="whitespace-pre-wrap">{{ $form }}</pre>
2024-10-23 04:05:27 +00:00
</Fieldset>
</Form>
</div>
<DocSectionCode :code="code" />
</template>
<script>
export default {
data() {
return {
initialValues: {
username: ''
},
code: {
basic: `
2024-10-23 09:27:41 +00:00
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="grid lg:grid-cols-2 gap-4 w-full">
2024-10-23 04:05:27 +00:00
<div class="flex flex-col justify-center items-center gap-4">
2024-10-24 06:33:42 +00:00
<InputText name="username" type="text" placeholder="Username" class="w-full sm:w-56" />
<Button type="submit" severity="secondary" label="Submit" class="w-full sm:w-56" />
2024-10-23 04:05:27 +00:00
</div>
2024-10-31 07:56:10 +00:00
<Fieldset legend="Form States" class="h-80 overflow-auto">
2024-10-23 09:27:41 +00:00
<pre class="whitespace-pre-wrap">{{ $form }}</pre>
2024-10-23 04:05:27 +00:00
</Fieldset>
</Form>
`,
options: `
<template>
2024-10-23 14:02:51 +00:00
<div class="card flex justify-center">
<Toast />
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="grid lg:grid-cols-2 gap-4 w-full">
<div class="flex flex-col justify-center items-center gap-4">
2024-10-24 06:33:42 +00:00
<InputText name="username" type="text" placeholder="Username" class="w-full sm:w-56" />
<Button type="submit" severity="secondary" label="Submit" class="w-full sm:w-56" />
2024-10-23 14:02:51 +00:00
</div>
2024-10-31 07:56:10 +00:00
<Fieldset legend="Form States" class="h-80 overflow-auto">
2024-10-23 14:02:51 +00:00
<pre class="whitespace-pre-wrap">{{ $form }}</pre>
</Fieldset>
</Form>
</div>
2024-10-23 04:05:27 +00:00
</template>
<script>
export default {
data() {
return {
initialValues: {
username: ''
2024-10-23 14:02:51 +00:00
},
2024-10-23 04:05:27 +00:00
};
},
methods: {
resolver: ({ values }) => {
2024-10-23 14:02:51 +00:00
const errors = { username: [] };
2024-10-23 04:05:27 +00:00
if (!values.username) {
2024-10-23 14:02:51 +00:00
errors.username.push({ type: 'required', message: 'Username is required.' });
}
if (values.username?.length < 3) {
errors.username.push({ type: 'minimum', message: 'Username must be at least 3 characters long.' });
2024-10-23 04:05:27 +00:00
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
<\/script>
`,
composition: `
<template>
2024-10-23 14:02:51 +00:00
<div class="card flex justify-center">
<Toast />
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="grid lg:grid-cols-2 gap-4 w-full">
<div class="flex flex-col justify-center items-center gap-4">
2024-10-24 06:33:42 +00:00
<InputText name="username" type="text" placeholder="Username" class="w-full sm:w-56" />
<Button type="submit" severity="secondary" label="Submit" class="w-full sm:w-56" />
2024-10-23 14:02:51 +00:00
</div>
2024-10-31 07:56:10 +00:00
<Fieldset legend="Form States" class="h-80 overflow-auto">
2024-10-23 14:02:51 +00:00
<pre class="whitespace-pre-wrap">{{ $form }}</pre>
</Fieldset>
</Form>
</div>
2024-10-23 04:05:27 +00:00
</template>
<script setup>
2024-10-23 14:02:51 +00:00
import { ref } from 'vue';
import { useToast } from 'primevue/usetoast';
const toast = useToast();
const initialValues = ref({
username: ''
});
const resolver = ({ values }) => {
const errors = { username: [] };
if (!values.username) {
errors.username.push({ type: 'required', message: 'Username is required.' });
}
if (values.username?.length < 3) {
errors.username.push({ type: 'minimum', message: 'Username must be at least 3 characters long.' });
}
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 }) => {
2024-10-23 09:27:41 +00:00
const errors = { username: [] };
2024-10-23 04:05:27 +00:00
if (!values.username) {
2024-10-23 09:27:41 +00:00
errors.username.push({ type: 'required', message: 'Username is required.' });
}
if (values.username?.length < 3) {
errors.username.push({ type: 'minimum', message: 'Username must be at least 3 characters long.' });
2024-10-23 04:05:27 +00:00
}
return {
errors
};
},
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>