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

fix: types and options #50

Merged
merged 6 commits into from
Aug 29, 2023
Merged

fix: types and options #50

merged 6 commits into from
Aug 29, 2023

Conversation

rafma0
Copy link
Contributor

@rafma0 rafma0 commented Aug 27, 2023

Hi, while trying your lib I noticed the 3rd param for registerWithMask wasn't getting the types correctly

image

I'm not sure its the best way to fix it, but I did it importing Inputmask to types.ts

image

Another thing is the options for react-hook-form were getting passed to inputmask, so I separated them to a 4th param

Hope those fixes are OK and thank you :)

@eduardoborges eduardoborges changed the title fix types and options fix: types and options Aug 27, 2023
Comment on lines 9 to 13
export function useHookFormMask<T extends FieldValues>(registerFn: UseFormRegister<T>) {
return (fieldName: Path<T>, mask: Mask, options?: Options, registerOptions?: RegisterOptions) => {
if (!registerFn) throw new Error('registerFn is required');

const { ref, ...restRegister } = registerFn(fieldName);
const { ref, ...restRegister } = registerFn(fieldName, registerOptions);
Copy link
Owner

Choose a reason for hiding this comment

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

I really dont like to use 3 params funcion. Not provide an good development experience. The secound funcion are an merge from Inputmask options and hook-form options. We dont need that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see but unless I'm missing something, I think you're merging only the types, and the params belonging to react-hook-form register are never really passed to it?

What I mean is, for example, it will auto complete options like setValueAs, valueAsNumber, valueAsDate, which belong to register, but if if pass them, it will have no effect.

with the original code (after we fix the types import), TS won't complain about this

{...registerWithMask(
  'price',
  'brl-currency',
  { rightAlign: false, autoUnmask: true, setValueAs: (val) => (val.replace(',', '.')) }
)}

but the setValueAs will have no effect

with the modified code, we could have something like

{...registerWithMask(
  'price',
  'brl-currency',
  { rightAlign: false, autoUnmask: true },
  { setValueAs: (val) => (val.replace(',', '.')) }
)}

It was the simplest way I could think without messing a lot with the original code.

Now let me explain how I use this specificaly. When I use a schema like Joi.number() for my price field, it validates numbers and strings that can be converted to numbers directly. So doing this is the simplest way to keep my schemas happy when I use commas for decimals, etc. I could also write a custom Joi validation, but passing setValueAs to register is simpler.

I guess what I don't understand is why join the options if you can't do anything with the joined options?

Copy link
Owner

Choose a reason for hiding this comment

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

Uh! Of course, make all sense to me now. But... this pr will be a break change :/

I think we can sure that will be a minor change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if we keep the merged types and pass only setValueAs to registerFn for now?

const { setValueAs } = options || {};

const { ref, ...restRegister } = registerFn(fieldName, { setValueAs });

Most of the other RegisterOptions we can pass direct to the component anyway.

I also tried passing valueAsNumber and valueAsDate and TS started yelling at me, but I'm ok converting to Number inside onSubmit, etc.

Copy link
Owner

Choose a reason for hiding this comment

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

Uh, check my new change request.

const { ref, ...restRegister } = registerFn(fieldName);
const { setValueAs } = options || {};

const { ref, ...restRegister } = registerFn(fieldName, { setValueAs });
Copy link
Owner

Choose a reason for hiding this comment

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

We can pass all the props to register, no problem.

Suggested change
const { ref, ...restRegister } = registerFn(fieldName, { setValueAs });
const { ref, ...restRegister } = registerFn(fieldName, options);

Copy link
Owner

Choose a reason for hiding this comment

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

Passing all props to Hook Form they works like a charm, ignoring the inputmask props.

Copy link
Contributor Author

@rafma0 rafma0 Aug 29, 2023

Choose a reason for hiding this comment

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

I didn't thought of passing everything to register as well :D

@@ -12,7 +12,9 @@ export function useHookFormMask<
return (fieldName: Path<T>, mask: Mask, options?: Options & D) => {
if (!registerFn) throw new Error('registerFn is required');

const { ref, ...restRegister } = registerFn(fieldName);
const { setValueAs } = options || {};
Copy link
Owner

Choose a reason for hiding this comment

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

You can remove that, of course.

Copy link
Owner

@eduardoborges eduardoborges left a comment

Choose a reason for hiding this comment

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

LGTM

@eduardoborges eduardoborges self-requested a review August 29, 2023 11:33
@eduardoborges eduardoborges enabled auto-merge (squash) August 29, 2023 11:33
@eduardoborges eduardoborges disabled auto-merge August 29, 2023 12:04
@eduardoborges eduardoborges merged commit 3bc502e into eduardoborges:main Aug 29, 2023
eduardoborges pushed a commit that referenced this pull request Aug 29, 2023
## [3.3.4](3.3.3...3.3.4) (2023-08-29)

### Bug Fixes

* types and options ([#50](#50)) ([3bc502e](3bc502e))
@eduardoborges
Copy link
Owner

🎉 This PR is included in version 3.3.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@rafma0 rafma0 deleted the fix/types-and-options branch August 29, 2023 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants