2024-10-22 06:31:16 +00:00
< template >
2024-10-22 11:16:22 +00:00
< DocSectionText v-bind ="$attrs" >
< p > CascadeSelect can be used with the < NuxtLink to = "/forms" > PrimeVue Forms < / NuxtLink > library . < / p >
< / DocSectionText >
2024-10-22 06:31:16 +00:00
< div class = "card flex justify-center" >
2024-10-22 09:14:17 +00:00
< Form v-slot ="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4" >
2024-10-22 06:31:16 +00:00
< 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.errors [ 0 ] ? .message }} < / Message >
< / 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 {
2024-10-22 09:14:17 +00:00
initialValues : {
2024-10-22 06:31:16 +00:00
city : { cname : '' }
} ,
2024-10-22 11:16:22 +00:00
resolver : zodResolver (
z . object ( {
city : z . object ( {
cname : z . string ( ) . min ( 1 , 'City cannot be empty.' )
} )
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 : `
2024-10-22 09:14:17 +00:00
< Form v-slot ="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4" >
2024-10-22 06:31:16 +00:00
< 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.errors [ 0 ] ? .message }} < / Message >
< / 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" >
< 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.errors [ 0 ] ? .message }} < / Message >
< / 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 : {
city : { cname : '' }
} ,
resolver : zodResolver (
z . object ( {
city : z . object ( {
cname : z . string ( ) . min ( 1 , 'City cannot be empty.' )
} )
} )
) ,
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" >
< 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.errors [ 0 ] ? .message }} < / Message >
< / 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 ( {
city : { name : '' }
} ) ;
const resolver = ref ( zodResolver (
z . object ( {
city : z . object ( {
cname : z . string ( ) . min ( 1 , 'City cannot be empty.' )
} )
} )
) ) ;
const resolver = ref ( zodResolver (
z . object ( {
city : z . object ( {
cname : z . string ( ) . min ( 1 , 'City cannot be empty.' )
} )
} )
) ) ;
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 >