primevue-mirror/apps/showcase/doc/treeselect/FormsDoc.vue

139 lines
4.8 KiB
Vue
Raw Normal View History

2024-10-23 09:54:05 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>TreeSelect is used with the <i>v-model</i> property.</p>
</DocSectionText>
<div class="card flex justify-center">
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-80">
<div class="flex flex-col gap-2">
<TreeSelect name="node" :options="nodes" placeholder="Select Item" fluid />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.node?.invalid" severity="error">{{ $form.node.error?.message }}</Message>
2024-10-23 09:54:05 +00:00
</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';
import { NodeService } from '/service/NodeService';
export default {
data() {
return {
initialValues: {
node: null
},
resolver: zodResolver(
z.object({
node: z.union([z.record(z.boolean()), z.literal(null)]).refine((obj) => obj !== null && Object.keys(obj).length > 0, { message: 'TreeSelect is required.' })
})
),
nodes: null,
code: {
basic: `
<Form v-slot="$form" :resolver="resolver" :initialValues="initialValues" @submit="onFormSubmit" class="flex flex-col gap-4 w-full md:w-80">
<div class="flex flex-col gap-2">
<TreeSelect name="node" :options="nodes" placeholder="Select Item" fluid />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.node?.invalid" severity="error">{{ $form.node.error?.message }}</Message>
2024-10-23 09:54:05 +00:00
</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 flex-col gap-4 w-full md:w-80">
<div class="flex flex-col gap-2">
<TreeSelect name="node" :options="nodes" placeholder="Select Item" fluid />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.node?.invalid" severity="error">{{ $form.node.error?.message }}</Message>
2024-10-23 09:54:05 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
</template>
<script>
import { zodResolver } from '@primevue/form/resolvers';
import { z } from 'zod';
import { NodeService } from '/service/NodeService';
export default {
data() {
return {
initialValues: {
node: null
},
resolver: zodResolver(
z.object({
node: z.union([z.record(z.boolean()), z.literal(null)]).refine((obj) => obj !== null && Object.keys(obj).length > 0, { message: 'TreeSelect is required.' })
})
),
nodes: null,
}
},
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 flex-col gap-4 w-full md:w-80">
<div class="flex flex-col gap-2">
<TreeSelect name="node" :options="nodes" placeholder="Select Item" fluid />
2024-10-23 11:17:49 +00:00
<Message v-if="$form.node?.invalid" severity="error">{{ $form.node.error?.message }}</Message>
2024-10-23 09:54:05 +00:00
</div>
<Button type="submit" severity="secondary" label="Submit" />
</Form>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { zodResolver } from '@primevue/form/resolvers';
import { useToast } from "primevue/usetoast";
import { z } from 'zod';
import { NodeService } from '/service/NodeService';
const toast = useToast();
const initialValues = ref({
node: null
});
const resolver = ref(zodResolver(
z.object({
node: z.union([z.record(z.boolean()), z.literal(null)]).refine((obj) => obj !== null && Object.keys(obj).length > 0, { message: 'TreeSelect is required.' })
})
));
const onFormSubmit = ({ valid }) => {
if (valid) {
toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
};
<\/script>
`
}
};
},
mounted() {
NodeService.getTreeNodes().then((data) => (this.nodes = data));
},
methods: {
onFormSubmit({ valid }) {
if (valid) {
this.$toast.add({ severity: 'success', summary: 'Form is submitted.', life: 3000 });
}
}
}
};
</script>