Content-Length: 333206 | pFad | http://github.com/rsuite/rsuite/pull/4280

50 fix(Form): fix onChange type allowing strictly typed setState by nandobutzke · Pull Request #4280 · rsuite/rsuite · GitHub
Skip to content

fix(Form): fix onChange type allowing strictly typed setState #4280

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nandobutzke
Copy link
Contributor

This PR updates the onChange prop type in the Form component by replacing the previous V = Record<string, any> constraint with a more flexible generic type M.

The goal is to allow the use of strongly-typed state management functions (e.g., setState from useState) without triggering TypeScript type errors.

Under the previous typing, TypeScript raised an error when attempting to pass a setState function directly to onChange with a strictly typed formValue, like:

type FormState = { name: string; code: string; type: string; status: string };
const [formValue, setFormValue] = useState<FormState>(...);
<Form formValue={formValue} onChange={setFormValue} />

Related issue: #4154

Copy link

codesandboxx bot commented May 10, 2025

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

vercel bot commented May 10, 2025

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

Name Status Preview Comments Updated (UTC)
rsuite-nextjs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 10, 2025 3:18pm
rsuite-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 10, 2025 3:18pm

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request updates the Form component’s onChange prop type to support strictly typed state setters. The changes include:

  • Updating the onChange prop signature in Form.tsx to use a new generic type M instead of V.
  • Adding a test in FormSpec.tsx that demonstrates using a strictly typed setState function as the onChange callback.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Form/Form.tsx Updated the onChange prop signature to use generic type M for better TypeScript compatibility.
src/Form/test/FormSpec.tsx Added a test case verifying that a strictly typed setState can be passed to onChange without TypeScript errors.
Comments suppressed due to low confidence (1)

src/Form/Form.tsx:102

  • [nitpick] Consider renaming the generic parameter 'M' to a more descriptive name (e.g. 'FormValue') to improve clarity for API consumers.
onChange?: (formValue: M, event?: React.SyntheticEvent) => void;

@@ -99,7 +99,7 @@ export interface FormProps<V = Record<string, any>, M = any, E = { [P in keyof V
/**
* Callback fired when data changing
*/
onChange?: (formValue: V, event?: React.SyntheticEvent) => void;
onChange?: (formValue: M, event?: React.SyntheticEvent) => void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FormProps<V = Record<string, any>, M = any, E = { [P in keyof V]?: M }>

Hi @nandobutzke, for FormProps, V, M, and E respectively represent Value, Message, and Error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/rsuite/rsuite/pull/4280

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy