primevue-mirror/apps/showcase/doc/autocomplete/FormDoc.vue

203 lines
7.0 KiB
Vue
Raw Normal View History

2024-10-21 14:04:15 +00:00
<template>
2024-10-22 11:16:05 +00:00
<DocSectionText v-bind="$attrs">
<p>AutoComplete can be used with the <NuxtLink to="/forms">PrimeVue Forms</NuxtLink> library.</p>
</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-21 14:04:15 +00:00
<div class="flex flex-col gap-2">
2024-10-23 06:36:02 +00:00
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" fluid />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.country?.invalid" severity="error">{{ $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';
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
export default {
data() {
return {
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({
name: z.string().min(1, 'Country required.')
}),
z.any().refine((val) => false, { message: 'Country required.' })
])
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-21 14:04:15 +00:00
<div class="flex flex-col gap-2">
2024-10-22 06:31:03 +00:00
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.country?.invalid" severity="error">{{ $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-22 11:16:05 +00:00
<div class="flex flex-col gap-2">
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.country?.invalid" severity="error">{{ $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';
import { zodResolver } from '@primevue/form/resolvers';
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({
name: z.string().min(1, 'Country required.')
}),
z.any().refine((val) => false, { message: 'Country required.' })
])
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-22 11:16:05 +00:00
<div class="flex flex-col gap-2">
<AutoComplete name="country" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.country?.invalid" severity="error">{{ $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";
import { zodResolver } from '@primevue/form/resolvers';
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: '' }
});
const resolver = ref(
zodResolver(
z.object({
2024-10-23 06:36:02 +00:00
country: z.union([
z.object({
name: z.string().min(1, 'Country required.')
}),
z.any().refine((val) => false, { message: 'Country required.' })
])
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>