use-mask-input 3.2.0 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { getMaskOptions } from './getMaskOptions';
3
+
4
+ describe('getMaskOptions', () => {
5
+ it('returns default options when no mask is provided', () => {
6
+ const options = getMaskOptions();
7
+ expect(options).toEqual({
8
+ jitMasking: false,
9
+ });
10
+ });
11
+
12
+ it('returns options for datetime mask', () => {
13
+ const options = getMaskOptions('datetime');
14
+ expect(options).toEqual({
15
+ alias: 'datetime',
16
+ inputFormat: 'dd/mm/yyyy',
17
+ placeholder: 'dd/mm/yyyy',
18
+ jitMasking: false,
19
+ });
20
+ });
21
+
22
+ it('returns options for cpf mask', () => {
23
+ const options = getMaskOptions('cpf');
24
+ expect(options).toEqual({
25
+ mask: '999.999.999-99',
26
+ placeholder: '___.___.___-__',
27
+ jitMasking: false,
28
+ });
29
+ });
30
+
31
+ it('returns options for email mask', () => {
32
+ const options = getMaskOptions('email');
33
+ expect(options).toEqual({
34
+ alias: 'email',
35
+ placeholder: '',
36
+ jitMasking: false,
37
+ });
38
+ });
39
+
40
+ it('returns options for numeric mask', () => {
41
+ const options = getMaskOptions('numeric');
42
+ expect(options).toEqual({
43
+ alias: 'numeric',
44
+ placeholder: '',
45
+ jitMasking: false,
46
+ });
47
+ });
48
+
49
+ it('returns options for currency mask', () => {
50
+ const options = getMaskOptions('currency');
51
+ expect(options).toEqual({
52
+ alias: 'currency',
53
+ prefix: '$ ',
54
+ placeholder: '',
55
+ jitMasking: false,
56
+ });
57
+ });
58
+
59
+ it('returns options for decimal mask', () => {
60
+ const options = getMaskOptions('decimal');
61
+ expect(options).toEqual({
62
+ alias: 'decimal',
63
+ placeholder: '',
64
+ jitMasking: false,
65
+ });
66
+ });
67
+
68
+ it('returns options for integer mask', () => {
69
+ const options = getMaskOptions('integer');
70
+ expect(options).toEqual({
71
+ alias: 'integer',
72
+ placeholder: '',
73
+ jitMasking: false,
74
+ });
75
+ });
76
+
77
+ it('returns options for percentage mask', () => {
78
+ const options = getMaskOptions('percentage');
79
+ expect(options).toEqual({
80
+ alias: 'percentage',
81
+ placeholder: ' %',
82
+ suffix: ' %',
83
+ jitMasking: false,
84
+ });
85
+ });
86
+
87
+ it('returns options for url mask', () => {
88
+ const options = getMaskOptions('url');
89
+ expect(options).toEqual({
90
+ alias: 'url',
91
+ placeholder: 'https://',
92
+ jitMasking: false,
93
+ });
94
+ });
95
+
96
+ it('returns options for ip mask', () => {
97
+ const options = getMaskOptions('ip');
98
+ expect(options).toEqual({
99
+ alias: 'ip',
100
+ jitMasking: false,
101
+ });
102
+ });
103
+
104
+ it('returns options for mac mask', () => {
105
+ const options = getMaskOptions('mac');
106
+ expect(options).toEqual({
107
+ alias: 'mac',
108
+ jitMasking: false,
109
+ });
110
+ });
111
+
112
+ it('returns options for ssn mask', () => {
113
+ const options = getMaskOptions('ssn');
114
+ expect(options).toEqual({
115
+ alias: 'ssn',
116
+ jitMasking: false,
117
+ });
118
+ });
119
+
120
+ it('returns options for custom mask', () => {
121
+ const options = getMaskOptions('999-999');
122
+ expect(options).toEqual({
123
+ mask: '999-999',
124
+ jitMasking: false,
125
+ });
126
+ });
127
+ });
@@ -0,0 +1,92 @@
1
+ import { Mask, Options } from '../types';
2
+
3
+ export const getMaskOptions = (mask?: Mask, _options?: Options): Options => {
4
+ const options: Options = {
5
+ ..._options,
6
+ jitMasking: false,
7
+ };
8
+ if (!mask) return options;
9
+
10
+ const masks: Record<string, Inputmask.Options> = {
11
+ datetime: {
12
+ alias: 'datetime',
13
+ inputFormat: 'dd/mm/yyyy',
14
+ placeholder: 'dd/mm/yyyy',
15
+ ...options,
16
+ },
17
+ cpf: {
18
+ mask: '999.999.999-99',
19
+ placeholder: '___.___.___-__',
20
+ ...options,
21
+ },
22
+ cnpj: {
23
+ mask: '99.999.999/9999-99',
24
+ placeholder: '__.___.___/____-__',
25
+ ...options,
26
+ },
27
+ email: {
28
+ alias: 'email',
29
+ placeholder: '',
30
+ ...options,
31
+ },
32
+ numeric: {
33
+ alias: 'numeric',
34
+ placeholder: '',
35
+ ...options,
36
+ },
37
+ currency: {
38
+ alias: 'currency',
39
+ prefix: '$ ',
40
+ placeholder: '',
41
+ ...options,
42
+ },
43
+ decimal: {
44
+ alias: 'decimal',
45
+ placeholder: '',
46
+ ...options,
47
+ },
48
+ integer: {
49
+ alias: 'integer',
50
+ placeholder: '',
51
+ ...options,
52
+ },
53
+ percentage: {
54
+ alias: 'percentage',
55
+ placeholder: ' %',
56
+ suffix: ' %',
57
+ ...options,
58
+ },
59
+ url: {
60
+ alias: 'url',
61
+ placeholder: 'https://',
62
+ ...options,
63
+ },
64
+ ip: {
65
+ alias: 'ip',
66
+ ...options,
67
+ },
68
+ mac: {
69
+ alias: 'mac',
70
+ ...options,
71
+ },
72
+ ssn: {
73
+ alias: 'ssn',
74
+ ...options,
75
+ },
76
+
77
+ };
78
+
79
+ if (typeof mask === 'string') {
80
+ if (masks[mask]) return masks[mask];
81
+ } else if (typeof mask === 'object') {
82
+ return {
83
+ ...mask,
84
+ ...options,
85
+ };
86
+ }
87
+
88
+ return {
89
+ mask,
90
+ ...options,
91
+ };
92
+ };
@@ -0,0 +1,3 @@
1
+ export * from './flow';
2
+ export * from './isServer';
3
+ export * from './getMaskOptions';
@@ -0,0 +1,5 @@
1
+ export const isServer = !(
2
+ typeof window !== 'undefined'
3
+ && window.document
4
+ && window.document.createElement
5
+ );
@@ -1,19 +1,20 @@
1
1
  import Inputmask from 'inputmask';
2
2
  import { RefCallback } from 'react';
3
- import { flow } from './utils';
4
- import { Mask, Options, Register } from './types';
3
+ import { flow, getMaskOptions } from './utils';
4
+ import { Mask, Options, UseFormRegisterReturn } from './types';
5
5
 
6
- export const withHookFormMask = (register: Register, mask: Mask, options?: Options): Register => {
6
+ export const withHookFormMask = (
7
+ register: UseFormRegisterReturn,
8
+ mask: Mask,
9
+ options?: Options,
10
+ ): UseFormRegisterReturn => {
7
11
  //
8
12
  let newRef;
9
13
 
10
14
  if (register) {
11
15
  const { ref } = register;
12
16
 
13
- const maskInput = Inputmask({
14
- mask: mask || undefined,
15
- ...options,
16
- });
17
+ const maskInput = Inputmask(getMaskOptions(mask, options));
17
18
 
18
19
  newRef = flow((_ref: HTMLElement) => {
19
20
  if (_ref) maskInput.mask(_ref);
package/src/withMask.ts CHANGED
@@ -1,15 +1,13 @@
1
1
  import Inputmask from 'inputmask';
2
- import { isServer } from './utils';
2
+ import { getMaskOptions, isServer } from './utils';
3
3
  import { Input, Mask, Options } from './types';
4
4
 
5
- export const withMask = (mask?: Mask, options?: Options) => (input: Input) => {
5
+ export const withMask = (mask: Mask, options?: Options) => (input: Input) => {
6
6
  //
7
7
  if (isServer) return input;
8
+ if (mask === null) return input;
8
9
 
9
- const maskInput = Inputmask({
10
- mask: mask || undefined,
11
- ...options,
12
- });
10
+ const maskInput = Inputmask(getMaskOptions(mask, options));
13
11
 
14
12
  if (input) {
15
13
  maskInput.mask(input);
package/src/utils.spec.ts DELETED
@@ -1,14 +0,0 @@
1
- import { describe } from 'vitest';
2
-
3
- import { flow } from './utils';
4
-
5
- describe('utils', (it) => {
6
- it('flow', ({ expect }) => {
7
- const res = flow(
8
- (a: number) => a + 1,
9
- (a:number) => a + 2,
10
- )(1);
11
-
12
- expect(res).toBe(4);
13
- });
14
- });