Skip to content

Commit

Permalink
feat: add dynamic imputmask lib
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanOliveiraM committed Feb 27, 2023
1 parent 0061ee0 commit 187dad3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"react",
"typescript"
],
"version": "2.1.11",
"version": "2.1.12",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"types": "./dist/index.d.ts",
Expand Down
29 changes: 27 additions & 2 deletions src/utils/form-utils.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ Create a mask for React Hook Form based fields.
Using the function with React Hook Form:

```tsx
<Field {...withHookFormMask(register('name', ['(99) 9999 9999', '(99) 9 9999 9999']))} />
// Needs inputmask lib
// Import dynamically by the client if you are using Server Side Rendering
import Inputmask from 'inputmask'

const foo = () => (
<Field
{...withHookFormMask(
Inputmask,
register('name', ['(99) 9999 9999', '(99) 9 9999 9999']),
{} // WithMaskOptions
)}
/>
)
```

<br />
Expand All @@ -25,7 +37,20 @@ Create a mask for generic inputs.
Using the function without React Hook Form:

```tsx
<input type='text' ref={withMask('9999-9999')} />
// Needs inputmask lib
// Import dynamically by the client if you are using Server Side Rendering
import Inputmask from 'inputmask'

const foo = () => (
<input
type='text'
ref={withMask(
Inputmask,
'9999-9999',
{} // WithMaskOptions
)}
/>
)
```

## Exported Types
Expand Down
13 changes: 9 additions & 4 deletions src/utils/form-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { UseFormRegisterReturn } from 'react-hook-form'

import Inputmask from 'inputmask'
import type Inputmask from 'inputmask'
import flowright from 'lodash.flowright'

export const withHookFormMask = (
InputmaskFunction: typeof Inputmask,
registerReturn: UseFormRegisterReturn,
mask: Inputmask.Options['mask'],
options?: Inputmask.Options
Expand All @@ -13,7 +14,7 @@ export const withHookFormMask = (
if (registerReturn) {
const { ref } = registerReturn

const maskInput = Inputmask({
const maskInput = InputmaskFunction({
mask,
jitMasking: true,
...options,
Expand All @@ -32,10 +33,14 @@ export const withHookFormMask = (
}

export const withMask =
(mask: Inputmask.Options['mask'], options?: Inputmask.Options) =>
(
InputmaskFunction: typeof Inputmask,
mask: Inputmask.Options['mask'],
options?: Inputmask.Options
) =>
(input: HTMLElement | HTMLInputElement | null) => {
//
const maskInput = Inputmask({
const maskInput = InputmaskFunction({
mask,
...options,
})
Expand Down

0 comments on commit 187dad3

Please sign in to comment.