From 0cd3371e4aa74a921e6a9241cb514051729cd36c Mon Sep 17 00:00:00 2001 From: zernonia Date: Fri, 19 Apr 2024 22:23:16 +0800 Subject: [PATCH] feat: dependencies rendering --- .../registry/new-york/example/AutoForm.vue | 92 ++++++++++----- .../new-york/ui/auto-form/AutoForm.vue | 18 ++- .../new-york/ui/auto-form/AutoFormField.vue | 9 +- .../new-york/ui/auto-form/dependencies.ts | 110 ++++++++++++++++++ .../new-york/ui/auto-form/fields/Array.vue | 3 +- .../new-york/ui/auto-form/fields/Boolean.vue | 14 ++- .../new-york/ui/auto-form/fields/Date.vue | 2 +- .../new-york/ui/auto-form/fields/Enum.vue | 4 +- .../new-york/ui/auto-form/fields/File.vue | 1 + .../new-york/ui/auto-form/fields/Input.vue | 12 +- .../new-york/ui/auto-form/fields/Number.vue | 2 +- .../new-york/ui/auto-form/fields/Object.vue | 5 +- .../new-york/ui/auto-form/interface.ts | 1 + .../registry/new-york/ui/auto-form/utils.ts | 14 +++ 14 files changed, 236 insertions(+), 51 deletions(-) create mode 100644 apps/www/src/lib/registry/new-york/ui/auto-form/dependencies.ts diff --git a/apps/www/src/lib/registry/new-york/example/AutoForm.vue b/apps/www/src/lib/registry/new-york/example/AutoForm.vue index 6590c18d..8e88a1c9 100644 --- a/apps/www/src/lib/registry/new-york/example/AutoForm.vue +++ b/apps/www/src/lib/registry/new-york/example/AutoForm.vue @@ -3,25 +3,35 @@ import * as z from 'zod' import { h, reactive, ref } from 'vue' import { useForm } from 'vee-validate' import { toTypedSchema } from '@vee-validate/zod' +import { DependencyType } from '../ui/auto-form/interface' import { Button } from '@/lib/registry/new-york/ui/button' import { toast } from '@/lib/registry/new-york/ui/toast' import type { Config } from '@/lib/registry/new-york/ui/auto-form' -import { AutoForm as AutoFormComponent, AutoFormField } from '@/lib/registry/new-york/ui/auto-form' +import { AutoForm, AutoFormField } from '@/lib/registry/new-york/ui/auto-form' const schema = z.object({ - guestListName: z.string(), + age: z.number().default(20), + parentsAllowed: z.boolean().optional(), + vegetarian: z.boolean().default(true), + mealOptions: z.enum(['Pasta', 'Salad', 'Beef Wellington']).optional(), invitedGuests: z .array( z.object({ name: z.string(), age: z.coerce.number(), }), - ).default([ - { name: '123', age: 30 }, - { name: '456', age: 30 }, - ]).describe('How many guests'), + ).default([{ name: '123', age: 0 }]), - list: z.array(z.string()).describe('test the config').min(1, 'Please add some item').default([]), + subObject: z.object({ + subField: z.string().optional().default('Sub Field'), + numberField: z.number().optional().default(10), + + subSubObject: z + .object({ + subSubField: z.string().default('Sub Sub Field'), + }) + .describe('Sub Sub Object Description'), + }), }) function onSubmit(values: Record) { @@ -30,42 +40,64 @@ function onSubmit(values: Record) { description: h('pre', { class: 'mt-2 w-[340px] rounded-md bg-slate-950 p-4' }, h('code', { class: 'text-white' }, JSON.stringify(values, null, 2))), }) } - -const form = useForm({ - keepValuesOnUnmount: true, // make sure the array/object field doesn't destroy the linked field - validationSchema: toTypedSchema(schema), -}) - -form.setValues({ - guestListName: 'testing 123', -}) diff --git a/apps/www/src/lib/registry/new-york/ui/auto-form/AutoForm.vue b/apps/www/src/lib/registry/new-york/ui/auto-form/AutoForm.vue index d70a4c0b..c8b8cd90 100644 --- a/apps/www/src/lib/registry/new-york/ui/auto-form/AutoForm.vue +++ b/apps/www/src/lib/registry/new-york/ui/auto-form/AutoForm.vue @@ -1,30 +1,36 @@