react-input-mask-format 1.0.5 → 2.0.0
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.
- package/README.md +94 -44
- package/dist/index.cjs +883 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +56 -0
- package/dist/index.d.ts +49 -49
- package/dist/index.js +852 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -81
- package/dist/react-input-mask.js +0 -2381
- package/dist/react-input-mask.min.js +0 -1
- package/index.js +0 -7
- package/lib/react-input-mask.development.js +0 -1017
- package/lib/react-input-mask.production.min.js +0 -1
package/README.md
CHANGED
|
@@ -1,52 +1,63 @@
|
|
|
1
1
|
# react-input-mask-format
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/react-input-mask-format)
|
|
4
|
+
[](https://www.npmjs.com/package/react-input-mask-format)
|
|
5
|
+
[](https://github.com/Temirtator/react-input-mask-format/actions/workflows/ci.yml)
|
|
4
6
|
|
|
5
|
-
Input masking component for React. Made with attention to UX.
|
|
7
|
+
Input masking component for React. Made with attention to UX.
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
A maintained fork of [react-input-mask](https://github.com/sanniassin/react-input-mask).
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
## Why this fork
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
* [Installation](#installation)
|
|
13
|
-
* [Usage](#usage)
|
|
14
|
-
* [Properties](#properties)
|
|
15
|
-
* [Known Issues](#known-issues)
|
|
13
|
+
The original `react-input-mask` has been unmaintained since 2021. This fork is:
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
- **React 16.8 – 19 compatible** — no `defaultProps`, no `findDOMNode`, StrictMode-safe
|
|
16
|
+
- **TypeScript-first** — types generated from source
|
|
17
|
+
- **Modern packaging** — ESM + CJS, `exports` map, tree-shakable
|
|
18
|
+
- **Zero runtime dependencies**
|
|
19
|
+
- **Custom `children` support restored** — passing a component as children
|
|
20
|
+
(e.g. to reuse another UI library's input) was broken in every release
|
|
21
|
+
since v1.0.x; it works again here
|
|
22
|
+
- **Drop-in compatible** with both `react-input-mask` v2 (`maskChar`, `formatChars`,
|
|
23
|
+
`beforeMaskedValueChange`) and v3-alpha (`maskPlaceholder`, `beforeMaskedStateChange`) APIs
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
npm install react-input-mask-format
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Requires React 16.8.0 or later.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
21
34
|
|
|
22
|
-
# Usage
|
|
23
35
|
```jsx
|
|
24
|
-
import
|
|
25
|
-
import InputMask from "react-input-mask";
|
|
36
|
+
import InputMask from "react-input-mask-format";
|
|
26
37
|
|
|
27
38
|
function DateInput(props) {
|
|
28
39
|
return <InputMask mask="99/99/9999" onChange={props.onChange} value={props.value} />;
|
|
29
40
|
}
|
|
30
41
|
```
|
|
31
42
|
|
|
32
|
-
|
|
33
|
-
| Name | Type | Default | Description |
|
|
34
|
-
| :-----------------------------------------: | :-------------------------: | :-----: | :--------------------------------------------------------------------- |
|
|
35
|
-
| **[`mask`](#mask)** | `{String\|Array<String, RegExp>}` | | Mask format |
|
|
36
|
-
| **[`maskPlaceholder`](#maskplaceholder)** | `{String}` | `_` | Placeholder to cover unfilled parts of the mask |
|
|
37
|
-
| **[`alwaysShowMask`](#alwaysshowmask)** | `{Boolean}` | `false` | Whether mask prefix and placeholder should be displayed when input is empty and has no focus |
|
|
38
|
-
| **[`beforeMaskedStateChange`](#beforemaskedstatechange)** | `{Function}` | | Function to modify value and selection before applying mask |
|
|
39
|
-
| **[`children`](#children)** | `{ReactElement}` | | Custom render function for integration with other input components |
|
|
43
|
+
## Properties
|
|
40
44
|
|
|
45
|
+
| Name | Type | Default | Description |
|
|
46
|
+
| :---------------------------------------------------------: | :--------------------------------: | :-----: | :--- |
|
|
47
|
+
| **[`mask`](#mask)** | `{String\|Array<String, RegExp>}` | | Mask format |
|
|
48
|
+
| **[`maskPlaceholder`](#maskplaceholder)** | `{String}` | `_` | Placeholder to cover unfilled parts of the mask |
|
|
49
|
+
| **[`alwaysShowMask`](#alwaysshowmask)** | `{Boolean}` | `false` | Whether mask prefix and placeholder should be displayed when input is empty and has no focus |
|
|
50
|
+
| **[`beforeMaskedStateChange`](#beforemaskedstatechange)** | `{Function}` | | Function to modify value and selection before applying mask |
|
|
51
|
+
| **[`children`](#children)** | `{ReactElement}` | | Custom render function for integration with other input components |
|
|
41
52
|
|
|
42
53
|
### `mask`
|
|
43
54
|
|
|
44
|
-
Mask format. Can be either a string or array of characters and regular expressions
|
|
45
|
-
|
|
55
|
+
Mask format. Can be either a string or array of characters and regular expressions.
|
|
46
56
|
|
|
47
57
|
```jsx
|
|
48
58
|
<InputMask mask="99/99/99" />
|
|
49
59
|
```
|
|
60
|
+
|
|
50
61
|
Simple masks can be defined as strings. The following characters will define mask format:
|
|
51
62
|
|
|
52
63
|
| Character | Allowed input |
|
|
@@ -55,10 +66,10 @@ Simple masks can be defined as strings. The following characters will define mas
|
|
|
55
66
|
| a | a-z, A-Z |
|
|
56
67
|
| * | 0-9, a-z, A-Z |
|
|
57
68
|
|
|
58
|
-
Any format character can be escaped with a backslash
|
|
59
|
-
|
|
69
|
+
Any format character can be escaped with a backslash.
|
|
60
70
|
|
|
61
71
|
More complex masks can be defined as an array of regular expressions and constant characters.
|
|
72
|
+
|
|
62
73
|
```jsx
|
|
63
74
|
// Canadian postal code mask
|
|
64
75
|
const firstLetter = /(?!.*[DFIOQU])[A-VXY]/i;
|
|
@@ -68,8 +79,8 @@ const mask = [firstLetter, digit, letter, " ", digit, letter, digit];
|
|
|
68
79
|
return <InputMask mask={mask} />;
|
|
69
80
|
```
|
|
70
81
|
|
|
71
|
-
|
|
72
82
|
### `maskPlaceholder`
|
|
83
|
+
|
|
73
84
|
```jsx
|
|
74
85
|
// Will be rendered as 12/--/--
|
|
75
86
|
<InputMask mask="99/99/99" maskPlaceholder="-" value="12" />
|
|
@@ -80,18 +91,19 @@ return <InputMask mask={mask} />;
|
|
|
80
91
|
// Will be rendered as 12/
|
|
81
92
|
<InputMask mask="99/99/99" maskPlaceholder={null} value="12" />
|
|
82
93
|
```
|
|
83
|
-
Character or string to cover unfilled parts of the mask. Default character is "\_". If set to `null` or empty string, unfilled parts will be empty as in a regular input.
|
|
84
94
|
|
|
95
|
+
Character or string to cover unfilled parts of the mask. Default character is "\_". If set to `null` or empty string, unfilled parts will be empty as in a regular input.
|
|
85
96
|
|
|
86
97
|
### `alwaysShowMask`
|
|
87
98
|
|
|
88
99
|
If enabled, mask prefix and placeholder will be displayed even when input is empty and has no focus.
|
|
89
100
|
|
|
90
|
-
|
|
91
101
|
### `beforeMaskedStateChange`
|
|
102
|
+
|
|
92
103
|
In case you need to customize masking behavior, you can provide `beforeMaskedStateChange` function to change masked value and cursor position before it's applied to the input.
|
|
93
104
|
|
|
94
|
-
It
|
|
105
|
+
It receives an object with `previousState`, `currentState` and `nextState` properties. Each state is an object with `value` and `selection` properties where `value` is a string and `selection` is an object containing `start` and `end` positions of the selection.
|
|
106
|
+
|
|
95
107
|
1. **previousState:** Input state before change. Only defined on `change` event.
|
|
96
108
|
2. **currentState:** Current raw input state. Not defined during component render.
|
|
97
109
|
3. **nextState:** Input state with applied mask. Contains `value` and `selection` fields.
|
|
@@ -119,19 +131,25 @@ return <InputMask mask="99/99/99" maskPlaceholder={null} beforeMaskedStateChange
|
|
|
119
131
|
|
|
120
132
|
Please note that `beforeMaskedStateChange` executes more often than `onChange` and must be pure.
|
|
121
133
|
|
|
122
|
-
|
|
123
134
|
### `children`
|
|
124
|
-
|
|
135
|
+
|
|
136
|
+
To use another component instead of a regular `<input />`, provide it as children. The following properties, if used, should always be defined on the `InputMask` component itself: `onChange`, `onMouseDown`, `onFocus`, `onBlur`, `value`, `disabled`, `readOnly`.
|
|
137
|
+
|
|
138
|
+
The child component must forward its ref to the underlying `<input>` DOM node (or to a wrapper element that contains one, e.g. a Material-style input with an internal `<input>`) so `InputMask` can read and control its value and selection.
|
|
139
|
+
|
|
125
140
|
```jsx
|
|
126
|
-
import React from
|
|
127
|
-
import InputMask from
|
|
128
|
-
|
|
141
|
+
import React from "react";
|
|
142
|
+
import InputMask from "react-input-mask-format";
|
|
143
|
+
|
|
144
|
+
const CustomInput = React.forwardRef((props, ref) => (
|
|
145
|
+
<input ref={ref} {...props} style={{ borderColor: "rebeccapurple" }} />
|
|
146
|
+
));
|
|
129
147
|
|
|
130
148
|
// Will work fine
|
|
131
149
|
function Input(props) {
|
|
132
150
|
return (
|
|
133
151
|
<InputMask mask="99/99/9999" value={props.value} onChange={props.onChange}>
|
|
134
|
-
<
|
|
152
|
+
<CustomInput />
|
|
135
153
|
</InputMask>
|
|
136
154
|
);
|
|
137
155
|
}
|
|
@@ -140,33 +158,46 @@ function Input(props) {
|
|
|
140
158
|
function InvalidInput(props) {
|
|
141
159
|
return (
|
|
142
160
|
<InputMask mask="99/99/9999" value={props.value}>
|
|
143
|
-
<
|
|
161
|
+
<CustomInput onChange={props.onChange} />
|
|
144
162
|
</InputMask>
|
|
145
163
|
);
|
|
146
164
|
}
|
|
147
165
|
```
|
|
148
166
|
|
|
149
|
-
|
|
167
|
+
> **Note:** `InputMask` clones the child element and injects its own `ref`
|
|
168
|
+
> callback into it, so a `ref` placed directly on the child element will be
|
|
169
|
+
> replaced. Attach your ref to `<InputMask>` itself instead. The ref you attach
|
|
170
|
+
> to `<InputMask>` receives whatever node the child component forwards its ref to;
|
|
171
|
+
> if the child forwards to a wrapper element rather than the actual `<input>`,
|
|
172
|
+
> you get that wrapper (the library still finds the inner input internally for masking).
|
|
173
|
+
|
|
174
|
+
## Known Issues
|
|
175
|
+
|
|
150
176
|
### Autofill
|
|
177
|
+
|
|
151
178
|
Browser's autofill requires either empty value in input or value which exactly matches beginning of the autofilled value. I.e. autofilled value "+1 (555) 123-4567" will work with "+1" or "+1 (5", but won't work with "+1 (\_\_\_) \_\_\_-\_\_\_\_" or "1 (555)". There are several possible solutions:
|
|
152
|
-
|
|
179
|
+
|
|
180
|
+
1. Set `maskPlaceholder` to null and trim space after "+1" with `beforeMaskedStateChange` if no more digits are entered.
|
|
153
181
|
2. Apply mask only if value is not empty. In general, this is the most reliable solution because we can't be sure about formatting in autofilled value.
|
|
154
182
|
3. Use less formatting in the mask.
|
|
155
183
|
|
|
156
184
|
Please note that it might lead to worse user experience (should I enter +1 if input is empty?). You should choose what's more important to your users — smooth typing experience or autofill. Phone and ZIP code inputs are very likely to be autofilled and it's a good idea to care about it, while security confirmation code in two-factor authorization shouldn't care about autofill at all.
|
|
157
185
|
|
|
158
186
|
### Cypress tests
|
|
187
|
+
|
|
159
188
|
The following sequence could fail
|
|
189
|
+
|
|
160
190
|
```js
|
|
161
191
|
cy.get("input")
|
|
162
192
|
.focus()
|
|
163
193
|
.type("12345")
|
|
164
194
|
.should("have.value", "12/34/5___"); // expected <input> to have value 12/34/5___, but the value was 23/45/____
|
|
165
|
-
|
|
195
|
+
```
|
|
166
196
|
|
|
167
197
|
Since [focus is not an action command](https://docs.cypress.io/api/commands/focus.html#Focus-is-not-an-action-command), it behaves differently than the real user interaction and, therefore, less reliable.
|
|
168
198
|
|
|
169
199
|
There is a few possible workarounds
|
|
200
|
+
|
|
170
201
|
```js
|
|
171
202
|
// Start typing without calling focus() explicitly.
|
|
172
203
|
// type() is an action command and focuses input anyway
|
|
@@ -186,7 +217,26 @@ cy.get("input")
|
|
|
186
217
|
.wait(50)
|
|
187
218
|
.type("12345")
|
|
188
219
|
.should("have.value", "12/34/5___");
|
|
189
|
-
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Migrating from react-input-mask
|
|
223
|
+
|
|
224
|
+
### From v2 (2.0.4 and earlier)
|
|
225
|
+
|
|
226
|
+
Your code keeps working as is — `maskChar`, `formatChars` and
|
|
227
|
+
`beforeMaskedValueChange` are supported as deprecated aliases (a one-time
|
|
228
|
+
console warning is emitted in development). Recommended renames:
|
|
229
|
+
|
|
230
|
+
| v2 | v2.x of this package |
|
|
231
|
+
| --- | --- |
|
|
232
|
+
| `maskChar="-"` | `maskPlaceholder="-"` |
|
|
233
|
+
| `maskChar={null}` | `maskPlaceholder={null}` |
|
|
234
|
+
| `formatChars={{ "#": "[0-9]" }}` | array mask: `mask={[/[0-9]/, …]}` (or keep `formatChars`) |
|
|
235
|
+
| `beforeMaskedValueChange={(newState, oldState, userInput, options) => …}` | `beforeMaskedStateChange={({ previousState, currentState, nextState }) => …}` |
|
|
236
|
+
| `inputRef={el => …}` | `ref` (standard forwarded ref) |
|
|
237
|
+
| `alwaysShowMask` | unchanged |
|
|
238
|
+
| custom `children` (e.g. wrapping another input library) | works if the child forwards its ref to the input (or a wrapper containing one) — see [children](#children); upstream v2 accepted any child via findDOMNode |
|
|
239
|
+
|
|
240
|
+
### From v3-alpha
|
|
190
241
|
|
|
191
|
-
|
|
192
|
-
Thanks to [BrowserStack](https://www.browserstack.com/) for the help with testing on real devices
|
|
242
|
+
API is identical — change the import to `react-input-mask-format` and you are done.
|