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

[Feature Request]: Transform values on submit #418

Open
crutchcorn opened this issue Aug 30, 2023 · 5 comments
Open

[Feature Request]: Transform values on submit #418

crutchcorn opened this issue Aug 30, 2023 · 5 comments

Comments

@crutchcorn
Copy link
Member

This feature request comes from houseform/houseform#60, the older form library I maintained

Description

Ideally, I'd like to have a number, displayed as a string (say, a locale with 1,000,000) and then transform this number back during the field submission.

 <Field
  name="number"
  initialValue={'0'}
  // This is the magic
  transformOnSubmit={v => parseFloat(v)}
  onChangeValidate={z.coerce.number().min(1, 'Must be at least 1')}
/>
@CheRayLiu
Copy link
Contributor

Hey @crutchcorn, mind assigning me this for me to work on? 🙏

@crutchcorn
Copy link
Member Author

Done! Thanks for taking this on @CheRayLiu.

Something worth considering is how we will/should handle TypeScript typings for transformations.

@CheRayLiu CheRayLiu removed their assignment Dec 4, 2023
@CheRayLiu
Copy link
Contributor

Unassigned myself for now as I don't see myself working on it any time soon

@crutchcorn
Copy link
Member Author

No worries @CheRayLiu! Thanks for being willing in the first place :)

@gutentag2012
Copy link
Contributor

Just an idea for this feature.
I am currently working on my own form library based around signals. The way I solved this transformation issue, is not by transforming the values onSubmit, but by handling the transformation on a per Field and onChange basis.

In my case, every field can have a transformToBinding function which takes the form value and transforms it for a specific input element (e.g. a number to a string) and a transformFromBinding function which does the opposite (e.g. transform a string to a number)
There is also a special handleChangeBound function (transforms the value before setting it in the form) and transformedValue attribute (the form value just run through the transformer function) that both make use of these transformers.

The benefit of this is, that the typing would be a lot easier, since the FormValues would stay the same and only a per Field type has to change (also that type could then be inferred from the transformer functions).
The second benefit is, that the form always holds the "correct" data at every point in time.

Maybe that might be something worth considering implementing here as well.

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

No branches or pull requests

3 participants