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

Improve the on* callback type signatures #14

Merged
merged 2 commits into from
Aug 17, 2021
Merged

Conversation

gaku-sei
Copy link
Contributor

First pr related to the big refactor: Improve the on callback type signatures*

The new types are closer to the actual code and allows for more flexibility. Not only is onChange easier to call even without event, but it can now be used to update the value, no need for setValue!

@@ -57,7 +57,9 @@ module Form = {
render={({field: {name, onBlur, onChange, ref, value}}) =>
<div>
<label> {name->React.string} </label>
<input name onBlur onChange ref value />
<input
name onBlur={_event => onBlur()} onChange={Controller.handleEvent(onChange)} ref value
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Example is not relevant as in a "real world" project most of the native inputs would be nested in a few components. So the onBlur={_event => onBlur()} onChange={Controller.handleEvent(onChange)} boilerplate shouldn't be that annoying and is worth it considering the benefits.

Js.Json.string(
value->Js.String2.split("")->Js.Array2.reverseInPlace->Js.Array2.joinWith(""),
),
),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Look ma, no setValue!

Copy link
Contributor

Choose a reason for hiding this comment

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

it will reduce some hard time for ATS, of course I do know you dont really like this api from react-hook-form

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a big fan of how inconsistent the code is 😕
I would have loved to receive 2 different functions instead of a mysterious one XD

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed ! We hard to know the behavior of this one exactly if we dont check the implementation ( based on the doc, it just said event is any =)))

@gaku-sei gaku-sei force-pushed the improve-on_-callback-types branch from e578944 to 8831786 Compare August 17, 2021 07:53
/>
```
")
let handleEvent = (onChange, event) => onChange(OnChangeArg.event(event))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To me this one is really not necessary and kinda defy the original goal: to be runtime free.

Though, the runtime cost being very small I think we can keep it 😄

What do you guys think @vtrofin @infothien ?

Copy link
Contributor

Choose a reason for hiding this comment

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

For me I prefer to just write

<Controller
  render={({field: {onChange}}) =>
    <input onChange={event => onChange(Controller.OnChangeArg.event(event))} />
  }
/>

=)))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Me too actually, i added this one as a compromise just in case XD

If @vtrofin is ok as well, let's drop this function 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@infothien Actually I miss the good old days of onChange={onChange << Controller.OnChangeArg.event} that was much, much clearer 😞

Copy link

Choose a reason for hiding this comment

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

thank you @gaku-sei and @infothien for pointing this out. I'm onboard with the change as well

@gaku-sei gaku-sei force-pushed the improve-on_-callback-types branch from 8831786 to c4688be Compare August 17, 2021 07:56
Copy link
Contributor

@infothien infothien left a comment

Choose a reason for hiding this comment

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

LGTM

Js.Json.string(
value->Js.String2.split("")->Js.Array2.reverseInPlace->Js.Array2.joinWith(""),
),
),
Copy link
Contributor

Choose a reason for hiding this comment

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

it will reduce some hard time for ATS, of course I do know you dont really like this api from react-hook-form

@gaku-sei
Copy link
Contributor Author

Dropped handleEvent 👍

@gaku-sei gaku-sei merged commit 7e2255d into main Aug 17, 2021
@gaku-sei gaku-sei deleted the improve-on_-callback-types branch August 17, 2021 08:30
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.

3 participants