2024-10-21 14:04:15 +00:00
|
|
|
<template>
|
2024-10-22 11:16:05 +00:00
|
|
|
<DocSectionText v-bind="$attrs">
|
2024-10-30 10:26:41 +00:00
|
|
|
<p>AutoComplete integrates seamlessly with the <NuxtLink to="/forms">PrimeVue Forms</NuxtLink> library.</p>
|
2024-10-22 11:16:05 +00:00
|
|
|
</DocSectionText>
|
2024-10-21 14:04:15 +00:00
|
|
|
<div class="card flex justify-center">
|
2024-10-23 11:17:49 +00:00
|
|
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4 w-full md:w-56">
|
2024-10-30 10:26:41 +00:00
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-23 06:36:02 +00:00
|
|
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" fluid />
|
2024-10-30 10:26:41 +00:00
|
|
|
<Message v-if="$form.country?.invalid" severity="error" size="small" variant="simple">{{ $form.country.error?.message }}</Message>
|
2024-10-21 14:04:15 +00:00
|
|
|
</div>
|
2024-10-22 11:16:05 +00:00
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
2024-10-21 14:04:15 +00:00
|
|
|
</Form>
|
|
|
|
</div>
|
2024-10-22 11:16:05 +00:00
|
|
|
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
|
2024-10-21 14:04:15 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import { CountryService } from '@/service/CountryService';
|
2024-11-01 21:54:30 +00:00
|
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
2024-10-21 14:04:15 +00:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2024-10-22 09:14:17 +00:00
|
|
|
initialValues: {
|
2024-10-22 06:31:03 +00:00
|
|
|
country: { name: '' }
|
2024-10-21 14:04:15 +00:00
|
|
|
},
|
|
|
|
countries: null,
|
|
|
|
filteredCountries: null,
|
2024-10-22 11:16:05 +00:00
|
|
|
resolver: zodResolver(
|
|
|
|
z.object({
|
2024-10-23 06:36:02 +00:00
|
|
|
country: z.union([
|
|
|
|
z.object({
|
2024-10-30 10:26:41 +00:00
|
|
|
name: z.string().min(1, 'Country is required.')
|
2024-10-23 06:36:02 +00:00
|
|
|
}),
|
2024-10-30 10:26:41 +00:00
|
|
|
z.any().refine((val) => false, { message: 'Country is required.' })
|
2024-10-23 06:36:02 +00:00
|
|
|
])
|
2024-10-21 14:04:15 +00:00
|
|
|
})
|
2024-10-22 11:16:05 +00:00
|
|
|
),
|
2024-10-21 14:04:15 +00:00
|
|
|
code: {
|
|
|
|
basic: `
|
2024-10-23 11:17:49 +00:00
|
|
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4 w-full md:w-56">
|
2024-10-30 10:26:41 +00:00
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-22 06:31:03 +00:00
|
|
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
2024-10-30 10:26:41 +00:00
|
|
|
<Message v-if="$form.country?.invalid" severity="error" size="small" variant="simple">{{ $form.country.error?.message }}</Message>
|
2024-10-21 14:04:15 +00:00
|
|
|
</div>
|
2024-10-22 11:16:05 +00:00
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
2024-10-21 14:04:15 +00:00
|
|
|
</Form>
|
|
|
|
`,
|
|
|
|
options: `
|
|
|
|
<template>
|
|
|
|
<div class="card flex justify-center">
|
2024-10-23 11:17:49 +00:00
|
|
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4 w-full md:w-56">
|
2024-10-30 10:26:41 +00:00
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-22 11:16:05 +00:00
|
|
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
2024-10-30 10:26:41 +00:00
|
|
|
<Message v-if="$form.country?.invalid" severity="error" size="small" variant="simple">{{ $form.country.error?.message }}</Message>
|
2024-10-22 11:16:05 +00:00
|
|
|
</div>
|
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
|
|
</Form>
|
|
|
|
<Toast />
|
2024-10-21 14:04:15 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2024-10-22 11:16:05 +00:00
|
|
|
import { CountryService } from '@/service/CountryService';
|
2024-11-01 21:54:30 +00:00
|
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
2024-10-22 11:16:05 +00:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
2024-10-21 14:04:15 +00:00
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2024-10-22 11:16:05 +00:00
|
|
|
initialValues: {
|
|
|
|
country: { name: '' }
|
|
|
|
},
|
|
|
|
countries: null,
|
|
|
|
filteredCountries: null,
|
|
|
|
resolver: zodResolver(
|
|
|
|
z.object({
|
2024-10-23 06:36:02 +00:00
|
|
|
country: z.union([
|
|
|
|
z.object({
|
2024-10-30 10:26:41 +00:00
|
|
|
name: z.string().min(1, 'Country is required.')
|
2024-10-23 06:36:02 +00:00
|
|
|
}),
|
2024-10-30 10:26:41 +00:00
|
|
|
z.any().refine((val) => false, { message: 'Country is required.' })
|
2024-10-23 06:36:02 +00:00
|
|
|
])
|
2024-10-22 11:16:05 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
CountryService.getCountries().then((data) => (this.countries = data));
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
search(event) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!event.query.trim().length) {
|
|
|
|
this.filteredCountries = [...this.countries];
|
|
|
|
} else {
|
|
|
|
this.filteredCountries = this.countries.filter((country) => {
|
|
|
|
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
},
|
|
|
|
onFormSubmit({ valid }) {
|
|
|
|
if (valid) {
|
|
|
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
|
|
}
|
2024-10-21 14:04:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
<\/script>
|
|
|
|
|
|
|
|
`,
|
|
|
|
composition: `
|
|
|
|
<template>
|
|
|
|
<div class="card flex justify-center">
|
2024-10-23 11:17:49 +00:00
|
|
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4 w-full md:w-56">
|
2024-10-30 10:26:41 +00:00
|
|
|
<div class="flex flex-col gap-1">
|
2024-10-22 11:16:05 +00:00
|
|
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
2024-10-30 10:26:41 +00:00
|
|
|
<Message v-if="$form.country?.invalid" severity="error" size="small" variant="simple">{{ $form.country.error?.message }}</Message>
|
2024-10-22 11:16:05 +00:00
|
|
|
</div>
|
|
|
|
<Button type="submit" severity="secondary" label="Submit" />
|
|
|
|
</Form>
|
|
|
|
<Toast />
|
2024-10-21 14:04:15 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
2024-10-22 11:16:05 +00:00
|
|
|
import { ref, onMounted } from "vue";
|
2024-11-01 21:54:30 +00:00
|
|
|
import { zodResolver } from '@primevue/forms/resolvers/zod';
|
2024-10-22 11:16:05 +00:00
|
|
|
import { useToast } from "primevue/usetoast";
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { CountryService } from "@/service/CountryService";
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
CountryService.getCountries().then((data) => (countries.value = data));
|
|
|
|
});
|
2024-10-21 14:04:15 +00:00
|
|
|
|
2024-10-22 11:16:05 +00:00
|
|
|
const initialValues = ref({
|
|
|
|
country: { name: '' }
|
|
|
|
});
|
2024-10-30 10:26:41 +00:00
|
|
|
const resolver = ref(zodResolver(
|
2024-10-22 11:16:05 +00:00
|
|
|
z.object({
|
2024-10-23 06:36:02 +00:00
|
|
|
country: z.union([
|
|
|
|
z.object({
|
2024-10-30 10:26:41 +00:00
|
|
|
name: z.string().min(1, 'Country is required.')
|
2024-10-23 06:36:02 +00:00
|
|
|
}),
|
2024-10-30 10:26:41 +00:00
|
|
|
z.any().refine((val) => false, { message: 'Country is required.' })
|
2024-10-23 06:36:02 +00:00
|
|
|
])
|
2024-10-22 11:16:05 +00:00
|
|
|
})
|
|
|
|
));
|
|
|
|
const countries = ref();
|
|
|
|
const selectedCountry = ref();
|
|
|
|
const filteredCountries = ref();
|
|
|
|
const toast = useToast();
|
|
|
|
|
|
|
|
const search = (event) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!event.query.trim().length) {
|
|
|
|
filteredCountries.value = [...countries.value];
|
|
|
|
} else {
|
|
|
|
filteredCountries.value = countries.value.filter((country) => {
|
|
|
|
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onFormSubmit = ({ valid }) => {
|
|
|
|
if (valid) {
|
|
|
|
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
|
|
}
|
|
|
|
};
|
2024-10-21 14:04:15 +00:00
|
|
|
<\/script>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
CountryService.getCountries().then((data) => (this.countries = data));
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
search(event) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!event.query.trim().length) {
|
|
|
|
this.filteredCountries = [...this.countries];
|
|
|
|
} else {
|
|
|
|
this.filteredCountries = this.countries.filter((country) => {
|
|
|
|
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
},
|
2024-10-22 06:31:03 +00:00
|
|
|
onFormSubmit({ valid }) {
|
|
|
|
if (valid) {
|
2024-10-21 14:04:15 +00:00
|
|
|
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|