AutoComplete form demo
parent
4523417309
commit
b9c477ca5f
|
@ -1,15 +1,17 @@
|
||||||
<template>
|
<template>
|
||||||
<DocSectionText v-bind="$attrs"> </DocSectionText>
|
<DocSectionText v-bind="$attrs">
|
||||||
|
<p>AutoComplete can be used with the <NuxtLink to="/forms">PrimeVue Forms</NuxtLink> library.</p>
|
||||||
|
</DocSectionText>
|
||||||
<div class="card flex justify-center">
|
<div class="card flex justify-center">
|
||||||
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
|
<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">
|
<div class="flex flex-col gap-2">
|
||||||
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
||||||
<Message v-if="$form.country?.invalid" severity="error">{{ $form.country.errors[0]?.message }}</Message>
|
<Message v-if="$form.country?.invalid" severity="error">{{ $form.country.errors[0]?.message }}</Message>
|
||||||
</div>
|
</div>
|
||||||
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button>
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
<DocSectionCode :code="code" />
|
<DocSectionCode :code="code" :dependencies="{ zod: '3.23.8' }" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
@ -25,12 +27,13 @@ export default {
|
||||||
},
|
},
|
||||||
countries: null,
|
countries: null,
|
||||||
filteredCountries: null,
|
filteredCountries: null,
|
||||||
resolver: null,
|
resolver: zodResolver(
|
||||||
schema: z.object({
|
z.object({
|
||||||
country: z.object({
|
country: z.object({
|
||||||
name: z.string().min(1, 'Country cannot be empty.')
|
name: z.string().min(1, 'Country cannot be empty.')
|
||||||
})
|
})
|
||||||
}),
|
})
|
||||||
|
),
|
||||||
code: {
|
code: {
|
||||||
basic: `
|
basic: `
|
||||||
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
|
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex justify-center flex-col gap-4">
|
||||||
|
@ -38,21 +41,64 @@ export default {
|
||||||
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
||||||
<Message v-if="$form.country?.invalid" severity="error">{{ $form.country.errors[0]?.message }}</Message>
|
<Message v-if="$form.country?.invalid" severity="error">{{ $form.country.errors[0]?.message }}</Message>
|
||||||
</div>
|
</div>
|
||||||
<Button type="submit" severity="secondary" class="self-center p-2">Submit</Button>
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
</Form>
|
</Form>
|
||||||
`,
|
`,
|
||||||
options: `
|
options: `
|
||||||
<template>
|
<template>
|
||||||
<div class="card flex justify-center">
|
<div class="card flex justify-center">
|
||||||
<InputText type="text" v-model="value" />
|
<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">
|
||||||
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
||||||
|
<Message v-if="$form.country?.invalid" severity="error">{{ $form.country.errors[0]?.message }}</Message>
|
||||||
|
</div>
|
||||||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
|
</Form>
|
||||||
|
<Toast />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { CountryService } from '@/service/CountryService';
|
||||||
|
import { zodResolver } from '@primevue/form/resolvers';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
value: null
|
initialValues: {
|
||||||
|
country: { name: '' }
|
||||||
|
},
|
||||||
|
countries: null,
|
||||||
|
filteredCountries: null,
|
||||||
|
resolver: zodResolver(
|
||||||
|
z.object({
|
||||||
|
country: z.object({
|
||||||
|
name: z.string().min(1, 'Country cannot be empty.')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
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 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,14 +108,61 @@ export default {
|
||||||
composition: `
|
composition: `
|
||||||
<template>
|
<template>
|
||||||
<div class="card flex justify-center">
|
<div class="card flex justify-center">
|
||||||
<InputText type="text" v-model="value" />
|
<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">
|
||||||
|
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
|
||||||
|
<Message v-if="$form.country?.invalid" severity="error">{{ $form.country.errors[0]?.message }}</Message>
|
||||||
|
</div>
|
||||||
|
<Button type="submit" severity="secondary" label="Submit" />
|
||||||
|
</Form>
|
||||||
|
<Toast />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue';
|
import { ref, onMounted } from "vue";
|
||||||
|
import { zodResolver } from '@primevue/form/resolvers';
|
||||||
|
import { useToast } from "primevue/usetoast";
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { CountryService } from "@/service/CountryService";
|
||||||
|
|
||||||
const value = ref(null);
|
onMounted(() => {
|
||||||
|
CountryService.getCountries().then((data) => (countries.value = data));
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialValues = ref({
|
||||||
|
country: { name: '' }
|
||||||
|
});
|
||||||
|
const resolver = ref(
|
||||||
|
zodResolver(
|
||||||
|
z.object({
|
||||||
|
country: z.object({
|
||||||
|
name: z.string().min(1, 'Country cannot be empty.')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
));
|
||||||
|
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 });
|
||||||
|
}
|
||||||
|
};
|
||||||
<\/script>
|
<\/script>
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
@ -77,7 +170,6 @@ const value = ref(null);
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
CountryService.getCountries().then((data) => (this.countries = data));
|
CountryService.getCountries().then((data) => (this.countries = data));
|
||||||
this.resolver = zodResolver(this.schema);
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
search(event) {
|
search(event) {
|
||||||
|
|
Loading…
Reference in New Issue