367 lines
13 KiB
Vue
367 lines
13 KiB
Vue
|
<template>
|
||
|
<DocSectionText v-bind="$attrs">
|
||
|
<p>CascadeSelect can be used with the <NuxtLink to="/forms">PrimeVue Forms</NuxtLink> library.</p>
|
||
|
</DocSectionText>
|
||
|
<div class="card flex justify-center">
|
||
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
|
||
|
<div class="flex flex-col gap-2">
|
||
|
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
|
||
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.error?.message }}</Message>
|
||
|
</div>
|
||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||
|
</Form>
|
||
|
</div>
|
||
|
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
|
import { zodResolver } from '@primevue/form/resolvers';
|
||
|
import { z } from 'zod';
|
||
|
|
||
|
export default {
|
||
|
data() {
|
||
|
return {
|
||
|
initialValues: {
|
||
|
city: null
|
||
|
},
|
||
|
resolver: zodResolver(
|
||
|
z.object({
|
||
|
city: z.union([
|
||
|
z.object({
|
||
|
cname: z.string().min(1, 'City required.')
|
||
|
}),
|
||
|
z.any().refine((val) => false, { message: 'City required.' })
|
||
|
])
|
||
|
})
|
||
|
),
|
||
|
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">
|
||
|
<div class="flex flex-col gap-2">
|
||
|
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
|
||
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.error?.message }}</Message>
|
||
|
</div>
|
||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||
|
</Form>
|
||
|
`,
|
||
|
options: `
|
||
|
<template>
|
||
|
<div class="card flex justify-center">
|
||
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
|
||
|
<div class="flex flex-col gap-2">
|
||
|
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
|
||
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.error?.message }}</Message>
|
||
|
</div>
|
||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||
|
</Form>
|
||
|
<Toast />
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
|
export default {
|
||
|
data() {
|
||
|
return {
|
||
|
initialValues: {
|
||
|
city: null
|
||
|
},
|
||
|
resolver: zodResolver(
|
||
|
z.object({
|
||
|
city: z.object({
|
||
|
cname: z.string().min(1, 'City is required.')
|
||
|
})
|
||
|
})
|
||
|
),
|
||
|
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 });
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
<\/script>
|
||
|
`,
|
||
|
composition: `
|
||
|
<template>
|
||
|
<div class="card flex justify-center">
|
||
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
|
||
|
<div class="flex flex-col gap-2">
|
||
|
<CascadeSelect name="city" :options="countries" optionLabel="cname" optionGroupLabel="name" :optionGroupChildren="['states', 'cities']" class="w-56" placeholder="Select a City" />
|
||
|
<Message v-if="$form.city?.invalid" severity="error">{{ $form.city.error?.message }}</Message>
|
||
|
</div>
|
||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||
|
</Form>
|
||
|
<Toast />
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<script setup>
|
||
|
import { ref } from "vue";
|
||
|
import { zodResolver } from '@primevue/form/resolvers';
|
||
|
import { useToast } from "primevue/usetoast";
|
||
|
import { z } from 'zod';
|
||
|
|
||
|
const toast = useToast();
|
||
|
const initialValues = ref({
|
||
|
city: null
|
||
|
});
|
||
|
const resolver = ref(zodResolver(
|
||
|
z.object({
|
||
|
city: z.object({
|
||
|
cname: z.string().min(1, 'City is required.')
|
||
|
})
|
||
|
})
|
||
|
));
|
||
|
const resolver = ref(zodResolver(
|
||
|
z.object({
|
||
|
city: z.object({
|
||
|
cname: z.string().min(1, 'City is required.')
|
||
|
})
|
||
|
})
|
||
|
));
|
||
|
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 });
|
||
|
}
|
||
|
};
|
||
|
<\/script>
|
||
|
`
|
||
|
}
|
||
|
};
|
||
|
},
|
||
|
methods: {
|
||
|
onFormSubmit({ valid }) {
|
||
|
if (valid) {
|
||
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
</script>
|