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

Make form of product of two data types be type equivalent to a product of two forms #11691

Closed
golergka opened this issue Mar 20, 2024 · 0 comments
Labels
feature request request a feature to be added waiting-up-vote Waiting for votes from the community.

Comments

@golergka
Copy link

Is your feature request related to a problem? Please describe.

I like to split my forms into separate components that group related and reusable sets of inputs. For example, in my database I have independent record for "Entity" which controls it's top-level settings and "EntityRevision", which controls the data which is updated in a versioned manner. When I create entity for the first time, I want to create a form that will encompass fields both for "Entity" and first "EntityRevision" in it's history. But when user edits the entity, I want to create separate form for creating new revision that will only have controls for the data that changes from version to version.

Describe the solution you'd like

I create components for two groups of entities:

interface EntityFormData {
	entityData: string
}

interface RevisionFormData {
	revisionData: string
}

export function EntityFormBody({ form }: { form: UseFormReturn<EntityFormData> }) { /*...*/ }
export function RevisionFormBody({ form }: { form: UseFormReturn<RevisionFormData> }) { /*...*/ }

And then use the library to create a shared form that will get passed to both components:

export function RevisionFormBody({ form }: { form: UseFormReturn });

Unfortunately, currently I get this type error:

Type 'UseFormReturn<EntityFormData | RevisionFormData, any, undefined>' is not assignable to type 'UseFormReturn<EntityFormData>'.
  The types returned by 'watch()' are incompatible between these types.
    Type 'EntityFormData | RevisionFormData' is not assignable to type 'EntityFormData'.
      Type 'RevisionFormData' is not assignable to type 'EntityFormData'.

However, when I cast it manually, everything works. I haven't investigated this issue in depth, but I suspect that either all usages of these entities should be comparable, or at least a large enough subset. In my case, I just use Controller component to extract data about particular fields.

Describe alternatives you've considered

For now, I write the same code, but just use casts to solve this problem.

@golergka golergka added feature request request a feature to be added waiting-up-vote Waiting for votes from the community. labels Mar 20, 2024
@bluebill1049 bluebill1049 added this to Pending Feature Requests in React Hook Form Mar 24, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature request request a feature to be added waiting-up-vote Waiting for votes from the community.
Projects
React Hook Form
  
Pending Feature Requests
Development

No branches or pull requests

2 participants