Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(frontend): replace react-hook-form with @tanstack/form #524

Merged
merged 14 commits into from
Dec 20, 2024

Conversation

634750802
Copy link
Collaborator

@634750802 634750802 commented Dec 20, 2024

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:

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:

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

Example 1. Create a form using withCreateEntityForm hoc

The withCreateEntityForm creates a component and type bound field layout components.

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

Copy link

vercel bot commented Dec 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tidb-ai-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 20, 2024 9:33am
tidb-ai-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 20, 2024 9:33am

Copy link

github-actions bot commented Dec 20, 2024

@634750802 634750802 changed the title refine(frontend): replace react-hook-form with @tanstack/form refactor(frontend): replace react-hook-form with @tanstack/form Dec 20, 2024
@634750802 634750802 marked this pull request as ready for review December 20, 2024 09:50
@634750802 634750802 merged commit 547789d into main Dec 20, 2024
9 checks passed
@634750802 634750802 deleted the frontend/refactor-use-tanstack-form branch December 20, 2024 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant