-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(frontend): replace react-hook-form with @tanstack/form (#524)
refactor major management forms after #519 ### How to write a standard management form #### Type Validator (Zod) zod is a commonly used well-typed validating library. Example for creating a zod type: ```ts import { z } from zod; const createEntitySchema = z.object({ name: z.string().min(1), description: z.string(), is_default: z.boolean(), }); ``` There are two type utils to get the zod validator's type: ```ts import { z } from zod; type Input = z.input<typeof createEntitySchema> type Output = z.infer<typeof createEntitySchema> // Typing example const input: Input = { ... }; const output = createEntitySchema.parse(input); // output satisfies Output ``` #### @tanstack/form See [documents](https://tanstack.com/form/latest) #### Example 1. Create a form using `withCreateEntityForm` hoc The withCreateEntityForm creates a component and type bound field layout components. ```ts import { withCreateEntityForm } from '@/components/form/create-entity-form'; import { FormInput, FormSwitch, FormTextarea } from '@/components/form/control-widget'; const Form = withCreateEntityForm( createEntitySchema, // The form schema async data => { // The submit handler const entity = await create(data); return entity; } ) const form = ( <Form onCreated={entity => { startTransition(() => { router.push('/entities/${entity.id}'); }) }} transitioning={transitioning} > <Form.Basic name='name' label='Name'> <FormInput /> </Form.Basic> <Form.Basic name='description' label='Description'> <FormTextarea /> </Form.Basic> <Form.Contained name='is_default' label='Is Default' description='xxx...'> <FormSwitch /> </Form.Contained> </Form> ) ``` #### Example 2. Create fully controlled complex form from scratch See [frontend/app/src/components/llm/CreateLLMForm.tsx](https://github.com/pingcap/tidb.ai/blob/afc16505e40625ab303d7b8d01eff78b48eaca1f/frontend/app/src/components/llm/CreateLLMForm.tsx)
- Loading branch information
Showing
55 changed files
with
1,261 additions
and
1,370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.