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

124 lines
3.7 KiB
Vue
Raw Normal View History

2024-10-23 04:05:27 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>Form uses the <i>name</i> property to create a state object for tracking values, errors, and actions.</p>
</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">
<InputText name="username" type="text" placeholder="Username" />
<Button type="submit" severity="secondary" label="Submit" />
</div>
<Fieldset legend="Form States">
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">
<InputText name="username" type="text" placeholder="Username" />
<Button type="submit" severity="secondary" label="Submit" />
</div>
<Fieldset legend="Form States">
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>
<Form v-slot="$form" :initialValues :resolver @submit="onFormSubmit" class="grid md:grid-cols-2 gap-4 w-full">
<div class="flex flex-col justify-center items-center gap-4">
<InputText name="username" type="text" placeholder="Username" />
<Button type="submit" severity="secondary" label="Submit" />
</div>
<Fieldset legend="Form States">
<pre>{{ $form }}</pre>
</Fieldset>
</Form>
</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>
<Card>
<template #title>Simple Card</template>
<template #content>
<p class="m-0">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore sed consequuntur error repudiandae numquam deserunt quisquam repellat libero asperiores earum nam nobis, culpa ratione quam perferendis esse, cupiditate neque
quas!
</p>
</template>
</Card>
</template>
<script setup>
<\/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>