primevue-mirror/apps/showcase/doc/cascadeselect/FormsDoc.vue

368 lines
13 KiB
Vue
Raw Normal View History

2024-10-22 06:31:16 +00:00
<template>
2024-10-22 11:16:22 +00:00
<DocSectionText v-bind="$attrs">
2024-10-30 10:26:41 +00:00
<p>CascadeSelect integrates seamlessly with the <NuxtLink to="/forms">PrimeVue Forms</NuxtLink> library.</p>
2024-10-22 11:16:22 +00:00
</DocSectionText>
2024-10-22 06:31:16 +00:00
<div class="card flex justify-center">
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
2024-10-30 10:26:41 +00:00
<div class="flex flex-col gap-1">
2024-10-22 06:31:16 +00:00
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
2024-10-30 10:26:41 +00:00
<Message v-if="$form.city?.invalid" severity="error" size="small" variant="simple">{{ $form.city.error?.message }}</Message>
2024-10-22 06:31:16 +00:00
</div>
2024-10-22 11:16:22 +00:00
<Button type="submit" severity="secondary" label="Submit" />
2024-10-22 06:31:16 +00:00
</Form>
</div>
2024-10-22 11:16:22 +00:00
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
2024-10-22 06:31:16 +00:00
</template>
<script>
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default {
data() {
return {
initialValues: {
2024-10-23 11:12:57 +00:00
city: null
2024-10-22 06:31:16 +00:00
},
2024-10-22 11:16:22 +00:00
resolver: zodResolver(
z.object({
2024-10-23 11:17:49 +00:00
city: z.union([
z.object({
2024-10-30 10:26:41 +00:00
cname: z.string().min(1, 'City is required.')
2024-10-23 11:17:49 +00:00
}),
2024-10-30 10:26:41 +00:00
z.any().refine((val) => false, { message: 'City is required.' })
2024-10-23 11:17:49 +00:00
])
2024-10-22 06:31:16 +00:00
})
2024-10-22 11:16:22 +00:00
),
2024-10-22 06:31:16 +00:00
countries: [
{
name: 'Australia',
code: 'AU',
states: [
{
name: 'New South Wales',
cities: [
{ cname: 'Sydney', code: 'A-SY' },
{ cname: 'Newcastle', code: 'A-NE' },
{ cname: 'Wollongong', code: 'A-WO' }
]
},
{
name: 'Queensland',
cities: [
{ cname: 'Brisbane', code: 'A-BR' },
{ cname: 'Townsville', code: 'A-TO' }
]
}
]
},
{
name: 'Canada',
code: 'CA',
states: [
{
name: 'Quebec',
cities: [
{ cname: 'Montreal', code: 'C-MO' },
{ cname: 'Quebec City', code: 'C-QU' }
]
},
{
name: 'Ontario',
cities: [
{ cname: 'Ottawa', code: 'C-OT' },
{ cname: 'Toronto', code: 'C-TO' }
]
}
]
},
{
name: 'United States',
code: 'US',
states: [
{
name: 'California',
cities: [
{ cname: 'Los Angeles', code: 'US-LA' },
{ cname: 'San Diego', code: 'US-SD' },
{ cname: 'San Francisco', code: 'US-SF' }
]
},
{
name: 'Florida',
cities: [
{ cname: 'Jacksonville', code: 'US-JA' },
{ cname: 'Miami', code: 'US-MI' },
{ cname: 'Tampa', code: 'US-TA' },
{ cname: 'Orlando', code: 'US-OR' }
]
},
{
name: 'Texas',
cities: [
{ cname: 'Austin', code: 'US-AU' },
{ cname: 'Dallas', code: 'US-DA' },
{ cname: 'Houston', code: 'US-HO' }
]
}
]
}
],
code: {
basic: `
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
2024-10-30 10:26:41 +00:00
<div class="flex flex-col gap-1">
2024-10-22 06:31:16 +00:00
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
2024-10-30 10:26:41 +00:00
<Message v-if="$form.city?.invalid" severity="error" size="small" variant="simple">{{ $form.city.error?.message }}</Message>
2024-10-22 06:31:16 +00:00
</div>
2024-10-22 11:16:22 +00:00
<Button type="submit" severity="secondary" label="Submit" />
2024-10-22 06:31:16 +00:00
</Form>
`,
options: `
<template>
<div class="card flex justify-center">
2024-10-22 11:16:22 +00:00
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
2024-10-30 10:26:41 +00:00
<div class="flex flex-col gap-1">
2024-10-22 11:16:22 +00:00
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
2024-10-30 10:26:41 +00:00
<Message v-if="$form.city?.invalid" severity="error" size="small" variant="simple">{{ $form.city.error?.message }}</Message>
2024-10-22 11:16:22 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
<Toast />
2024-10-22 06:31:16 +00:00
</div>
</template>
<script>
export default {
data() {
return {
2024-10-22 11:16:22 +00:00
initialValues: {
2024-10-23 11:15:03 +00:00
city: null
2024-10-22 11:16:22 +00:00
},
resolver: zodResolver(
z.object({
2024-10-30 11:28:01 +00:00
city: z.union([
z.object({
cname: z.string().min(1, 'City is required.')
}),
z.any().refine((val) => false, { message: 'City is required.' })
])
2024-10-22 11:16:22 +00:00
})
),
countries: [
{
name: 'Australia',
code: 'AU',
states: [
{
name: 'New South Wales',
cities: [
{ cname: 'Sydney', code: 'A-SY' },
{ cname: 'Newcastle', code: 'A-NE' },
{ cname: 'Wollongong', code: 'A-WO' }
]
},
{
name: 'Queensland',
cities: [
{ cname: 'Brisbane', code: 'A-BR' },
{ cname: 'Townsville', code: 'A-TO' }
]
}
]
},
{
name: 'Canada',
code: 'CA',
states: [
{
name: 'Quebec',
cities: [
{ cname: 'Montreal', code: 'C-MO' },
{ cname: 'Quebec City', code: 'C-QU' }
]
},
{
name: 'Ontario',
cities: [
{ cname: 'Ottawa', code: 'C-OT' },
{ cname: 'Toronto', code: 'C-TO' }
]
}
]
},
{
name: 'United States',
code: 'US',
states: [
{
name: 'California',
cities: [
{ cname: 'Los Angeles', code: 'US-LA' },
{ cname: 'San Diego', code: 'US-SD' },
{ cname: 'San Francisco', code: 'US-SF' }
]
},
{
name: 'Florida',
cities: [
{ cname: 'Jacksonville', code: 'US-JA' },
{ cname: 'Miami', code: 'US-MI' },
{ cname: 'Tampa', code: 'US-TA' },
{ cname: 'Orlando', code: 'US-OR' }
]
},
{
name: 'Texas',
cities: [
{ cname: 'Austin', code: 'US-AU' },
{ cname: 'Dallas', code: 'US-DA' },
{ cname: 'Houston', code: 'US-HO' }
]
}
]
}
]
};
},
methods: {
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
2024-10-22 06:31:16 +00:00
}
}
2024-10-22 11:16:22 +00:00
};
2024-10-22 06:31:16 +00:00
<\/script>
`,
composition: `
<template>
<div class="card flex justify-center">
2024-10-22 11:16:22 +00:00
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
2024-10-30 10:26:41 +00:00
<div class="flex flex-col gap-1">
2024-10-22 11:16:22 +00:00
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
2024-10-30 10:26:41 +00:00
<Message v-if="$form.city?.invalid" severity="error" size="small" variant="simple">{{ $form.city.error?.message }}</Message>
2024-10-22 11:16:22 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
<Toast />
2024-10-22 06:31:16 +00:00
</div>
</template>
<script setup>
2024-10-22 11:16:22 +00:00
import { ref } from "vue";
import { zodResolver } from '@primevue/form/resolvers';
import { useToast } from "primevue/usetoast";
import { z } from 'zod';
2024-10-22 06:31:16 +00:00
2024-10-22 11:16:22 +00:00
const toast = useToast();
const initialValues = ref({
2024-10-23 11:15:03 +00:00
city: null
2024-10-22 11:16:22 +00:00
});
2024-10-30 10:26:41 +00:00
2024-10-22 11:16:22 +00:00
const resolver = ref(zodResolver(
z.object({
2024-10-30 11:28:01 +00:00
city: z.union([
z.object({
cname: z.string().min(1, 'City is required.')
}),
z.any().refine((val) => false, { message: 'City is required.' })
])
2024-10-22 11:16:22 +00:00
})
));
2024-10-30 10:26:41 +00:00
2024-10-22 11:16:22 +00:00
const countries = ref([
{
name: 'Australia',
code: 'AU',
states: [
{
name: 'New South Wales',
cities: [
{ cname: 'Sydney', code: 'A-SY' },
{ cname: 'Newcastle', code: 'A-NE' },
{ cname: 'Wollongong', code: 'A-WO' }
]
},
{
name: 'Queensland',
cities: [
{ cname: 'Brisbane', code: 'A-BR' },
{ cname: 'Townsville', code: 'A-TO' }
]
}
]
},
{
name: 'Canada',
code: 'CA',
states: [
{
name: 'Quebec',
cities: [
{ cname: 'Montreal', code: 'C-MO' },
{ cname: 'Quebec City', code: 'C-QU' }
]
},
{
name: 'Ontario',
cities: [
{ cname: 'Ottawa', code: 'C-OT' },
{ cname: 'Toronto', code: 'C-TO' }
]
}
]
},
{
name: 'United States',
code: 'US',
states: [
{
name: 'California',
cities: [
{ cname: 'Los Angeles', code: 'US-LA' },
{ cname: 'San Diego', code: 'US-SD' },
{ cname: 'San Francisco', code: 'US-SF' }
]
},
{
name: 'Florida',
cities: [
{ cname: 'Jacksonville', code: 'US-JA' },
{ cname: 'Miami', code: 'US-MI' },
{ cname: 'Tampa', code: 'US-TA' },
{ cname: 'Orlando', code: 'US-OR' }
]
},
{
name: 'Texas',
cities: [
{ cname: 'Austin', code: 'US-AU' },
{ cname: 'Dallas', code: 'US-DA' },
{ cname: 'Houston', code: 'US-HO' }
]
}
]
}
]);
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
2024-10-22 06:31:16 +00:00
<\/script>
2024-10-22 11:16:22 +00:00
`
2024-10-22 06:31:16 +00:00
}
};
},
methods: {
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>