Skip to content

Commit 66ca242

Browse files
committed
beforeChange -> beforeMaskedValueChange
1 parent fa31afe commit 66ca242

File tree

5 files changed

+207
-152
lines changed

5 files changed

+207
-152
lines changed

Diff for: .size-snapshot.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"dist/react-input-mask.js": {
3-
"bundled": 29027,
4-
"minified": 10919,
5-
"gzipped": 3920
3+
"bundled": 31396,
4+
"minified": 11741,
5+
"gzipped": 4140
66
},
77
"lib/index.js": {
8-
"bundled": 27325,
9-
"minified": 12469,
10-
"gzipped": 4038
8+
"bundled": 29568,
9+
"minified": 13417,
10+
"gzipped": 4232
1111
}
1212
}

Diff for: README.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ Show mask when input is empty and has no focus.
5151

5252
Use `inputRef` instead of `ref` if you need input node to manage focus, selection, etc.
5353

54-
### `beforeChange` : `function`
54+
### `beforeMaskedValueChange` : `function`
5555

56-
In case you need to implement more complex masking behavior, you can provide `beforeChange` function to change masked value and cursor position before it will be applied to the input. `beforeChange` receives following arguments:
56+
In case you need to implement more complex masking behavior, you can provide `beforeMaskedValueChange` function to change masked value and cursor position before it will be applied to the input. `beforeMaskedValueChange` receives following arguments:
5757
1. **value** (string): New masked value.
5858
2. **cursorPosition** (number): New cursor position. `null` if change was triggered by the `blur` event.
5959
3. **userInput** (string): Raw entered or pasted string. `null` if user didn't enter anything (e.g. triggered by deletion or rerender due to props change).
@@ -72,11 +72,11 @@ In case you need to implement more complex masking behavior, you can provide `be
7272
}
7373
```
7474

75-
`beforeChange` must return an object with the following fields:
75+
`beforeMaskedValueChange` must return an object with the following fields:
7676
1. **value** (string): New value.
7777
2. **cursorPosition** (number): New cursor position.
7878

79-
Please note that `beforeChange` executes more often than `onChange`, so it's recommended to make it pure.
79+
Please note that `beforeMaskedValueChange` executes more often than `onChange`, so it's recommended to make it pure.
8080

8181

8282
## Example
@@ -91,7 +91,7 @@ class PhoneInput extends React.Component {
9191
}
9292
```
9393

94-
Mask for ZIP Code. Uses beforeChange to omit trailing minus if it wasn't entered by user:
94+
Mask for ZIP Code. Uses beforeMaskedValueChange to omit trailing minus if it wasn't entered by user:
9595
```jsx
9696
import React from 'react';
9797
import InputMask from 'react-input-mask';
@@ -107,23 +107,28 @@ class Input extends React.Component {
107107
});
108108
}
109109

110-
beforeChange = (value, cursorPosition, enteredString) => {
110+
beforeMaskedValueChange = (newState, oldState, userInput) => {
111+
var { value } = newState;
112+
var selection = newState.selection;
113+
var cursorPosition = selection ? selection.start : null;
114+
111115
// keep minus if entered by user
112-
if (value.endsWith('-') && enteredString !== '-' && !this.state.value.endsWith('-')) {
116+
if (value.endsWith('-') && userInput !== '-' && !this.state.value.endsWith('-')) {
113117
if (cursorPosition === value.length) {
114118
cursorPosition--;
119+
selection = { start: cursorPosition, end: cursorPosition };
115120
}
116121
value = value.slice(0, -1);
117122
}
118123

119124
return {
120-
value: value,
121-
cursorPosition: cursorPosition
125+
value,
126+
selection
122127
};
123128
}
124129

125130
render() {
126-
return <InputMask mask="99999-9999" maskChar={null} value={this.state.value} onChange={this.onChange} beforeChange={this.beforeChange} />;
131+
return <InputMask mask="99999-9999" maskChar={null} value={this.state.value} onChange={this.onChange} beforeMaskedValueChange={this.beforeMaskedValueChange} />;
127132
}
128133
}
129134
```

0 commit comments

Comments
 (0)