![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
react-input-mask
Advanced tools
Readme
Yet another React component for input masking. Made with attention to UX. Compatible with IE8+.
npm install react-input-mask --save
Also you can use it without a module bundler
<!-- Load React first -->
<script src="https://unpkg.com/react/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom/dist/react-dom.min.js"></script>
<!-- Will be exported to window.ReactInputMask -->
<script src="https://unpkg.com/react-input-mask/dist/react-input-mask.min.js"></script>
mask
: string
Mask string. Default format characters are:
9
: 0-9
a
: A-Z, a-z
*
: A-Z, a-z, 0-9
Any character can be escaped with a backslash. It will appear as a double backslash in JS strings. For example, a German phone mask with unremoveable prefix +49 will look like mask="+4\9 99 999 99"
or mask={'+4\\9 99 999 99'}
maskChar
: string
Character to cover unfilled parts of the mask. Default character is "_". If set to null or empty string, unfilled parts will be empty as in ordinary input.
formatChars
: object
Defines format characters with characters as a keys and corresponding RegExp strings as a values. Default ones:
{
'9': '[0-9]',
'a': '[A-Za-z]',
'*': '[A-Za-z0-9]'
}
alwaysShowMask
: boolean
Show mask when input is empty and has no focus.
inputRef
: function
Use inputRef
instead of ref
if you need input node to manage focus, selection, etc.
beforeMaskedValueChange
: function
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:
value
and selection
fields. selection
is null on input blur. Example: { value: '12/1_/____', selection: { start: 4, end: 4 } }
value
and selection
fields. selection
is null on input focus.null
if user didn't enter anything (e.g. triggered by deletion or rerender due to props change).{
mask: '99/99/9999',
maskChar: '_',
alwaysShowMask: false,
formatChars: {
'9': '[0-9]',
'a': '[A-Za-z]',
'*': '[A-Za-z0-9]'
},
permanents: [2, 5] // permanents is an array of indexes of the non-editable characters in the mask
}
beforeMaskedValueChange
must return an object with following fields:
selection
in newState
argument is null, it must be null too.Please note that beforeMaskedValueChange
executes more often than onChange
and must be pure.
import React from 'react';
import InputMask from 'react-input-mask';
class PhoneInput extends React.Component {
render() {
return <InputMask {...this.props} mask="+4\9 99 999 99" maskChar=" " />;
}
}
Mask for ZIP Code. Uses beforeMaskedValueChange to omit trailing minus if it wasn't entered by user:
import React from 'react';
import InputMask from 'react-input-mask';
class Input extends React.Component {
state = {
value: ''
}
onChange = (event) => {
this.setState({
value: event.target.value
});
}
beforeMaskedValueChange = (newState, oldState, userInput) => {
var { value } = newState;
var selection = newState.selection;
var cursorPosition = selection ? selection.start : null;
// keep minus if entered by user
if (value.endsWith('-') && userInput !== '-' && !this.state.value.endsWith('-')) {
if (cursorPosition === value.length) {
cursorPosition--;
selection = { start: cursorPosition, end: cursorPosition };
}
value = value.slice(0, -1);
}
return {
value,
selection
};
}
render() {
return <InputMask mask="99999-9999" maskChar={null} value={this.state.value} onChange={this.onChange} beforeMaskedValueChange={this.beforeMaskedValueChange} />;
}
}
Thanks to BrowserStack for the help with testing on real devices
FAQs
Masked input component for React
The npm package react-input-mask receives a total of 435,568 weekly downloads. As such, react-input-mask popularity was classified as popular.
We found that react-input-mask demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.