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 · 6 comments · May be fixed by #893
Open

[Feature Request]: Transform values on submit #418

crutchcorn opened this issue Aug 30, 2023 · 6 comments · May be fixed by #893

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.

@t-rosa
Copy link
Contributor

t-rosa commented Aug 7, 2024

Hey @crutchcorn is this issue still up to date? If so, I think I can try to implement a small part of it.

@t-rosa t-rosa linked a pull request Aug 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants