Gathering detailed insights and metrics for @salutejs/react-maskinput
Gathering detailed insights and metrics for @salutejs/react-maskinput
Gathering detailed insights and metrics for @salutejs/react-maskinput
Gathering detailed insights and metrics for @salutejs/react-maskinput
Mask input with simple API and rich customization
npm install @salutejs/react-maskinput
Typescript
Module System
Node Version
NPM Version
TypeScript (98.02%)
JavaScript (1.98%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
130 Commits
1 Branches
Updated on Sep 17, 2024
Latest Version
3.2.6
Package Id
@salutejs/react-maskinput@3.2.6
Unpacked Size
25.91 kB
Size
6.76 kB
File Count
4
NPM Version
8.19.0
Node Version
18.16.1
Published on
Sep 17, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
1
4
DEMO: https://xnimorz.github.io/masked-input/
Mask input with simple API and rich customization.
You often have to create input for a credit card, phone number, birthday, etc. Each of this usecases requires to input value with some formatting (for example 0000-0000-000-0000 for credit card).
react mask input could solve this problem without the tears.
1npm install --save react-maskinput
or if you use yarn
1yarn add react-maskinput
You can try all of this examples here: https://xnimorz.github.io/masked-input/
The most simple usage is a credit card (click to the show code
button):
1import MaskInput from 'react-maskinput'; 2 3<MaskInput alwaysShowMask maskChar="_" mask="0000-0000-0000-0000" size={20} />;
All this examples you can try here: https://xnimorz.github.io/masked-input/
All props you can change right during the runtime. Credit card with automatic switching between visa and american express format (for amex format write 34 or 37):
1import MaskInput from 'react-maskinput'; 2const [mask, setMask] = React.useState('0000-0000-0000-0000'); 3 4const onChange = e => { 5 if (e.target.value.indexOf('34') === 0 || e.target.value.indexOf('37') === 0) { 6 setMask('0000-000000-00000'); 7 return; 8 } 9 10 setMask('0000-0000-0000-0000'); 11}; 12 13<MaskInput onChange={onChange} maskChar="_" mask={mask} alwaysShowMask size={20} />;
Date input with custom year (2 or 4 numbers):
1import MaskInput from 'react-maskinput'; 2const [mask, setMask] = React.useState('00.00.0000'); 3const [maskString, setMaskString] = React.useState('DD.MM.YYYY'); 4const onChange = e => { 5 if (parseInt(e.target.value[6], 10) > 2) { 6 setMaskString('DD.MM.YY'); 7 setMask('00.00.00'); 8 } else { 9 setMaskString('DD.MM.YYYY'); 10 setMask('00.00.0000'); 11 } 12}; 13<MaskInput alwaysShowMask onChange={onChange} maskString={maskString} mask={mask} size={20} />;
You can use any regular input fields, like placeholder:
1import MaskInput from 'react-maskinput'; 2const [mask, setMask] = React.useState('00.00.0000'); 3const [maskString, setMaskString] = React.useState('DD.MM.YYYY'); 4const onChange = e => { 5 if (parseInt(e.target.value[6], 10) > 2) { 6 setMaskString('DD.MM.YY'); 7 setMask('00.00.00'); 8 } else { 9 setMaskString('DD.MM.YYYY'); 10 setMask('00.00.0000'); 11 } 12}; 13<MaskInput 14 onChange={onChange} 15 maskString={maskString} 16 mask={mask} 17 size={35} 18 showMask 19 placeholder="Enter your birthdate DD.MM.YYYY" 20/>;
The USA phone format:
1import MaskInput from 'react-maskinput'; 2 3<MaskInput 4 alwaysShowMask 5 mask={'+1 (000) 000 - 0000'} 6 size={20} 7 showMask 8 maskChar="_" 9 placeholder="Enter your birthdate DD.MM.YYYY" 10/>;
You can use defaultValue to set up componant as uncontrolled input:
1import MaskInput from 'react-maskinput'; 2 3<MaskInput alwaysShowMask maskChar="_" mask="0000-{0}-0000" defaultValue="123456789" />;
In onChange | onChangeValue callback you can receive on processing value. This example also shows how to create controlled input:
1import MaskInput from 'react-maskinput'; 2const [onChange, setOnChange] = React.useState(''); 3const [onValueChange, setOnValueChange] = React.useState(''); 4<div> 5 <p>Value from onChange event is: "{onChange}"</p> 6 <p>Value from onChange with replace is: "{onChange.replace('-', '')}"</p> 7 <p>maskedValue from onValueChange is: "{onValueChange.maskedValue}"</p> 8 <p>Value from onValueChange is: "{onValueChange.value}"</p> 9 <MaskInput 10 onChange={e => setOnChange(e.target.value)} 11 onValueChange={setOnValueChange} 12 mask={'0000-0000'} 13 value={onChange} 14 size={20} 15 /> 16</div>;
Custom classes:
1import MaskInput from 'react-maskinput'; 2<MaskInput className="custom-input" alwaysShowMask maskString="0000-(TEXT)-0000" mask="0000-(aaaa)-0000" />;
Get HTMLElement from the component:
1import MaskInput from 'react-maskinput'; 2const [el, setEl] = React.useState(null); 3<div> 4 <p> 5 <code>el is: `{el && el.outerHTML}`</code> 6 </p> 7 <MaskInput 8 getReference={el => setEl(el)} /* Now in el is storing input HtmlElement */ 9 alwaysShowMask 10 size={20} 11 maskChar="_" 12 mask="0000-0000-0000" 13 /> 14</div>;
List of specific react-maskinput props:
mask
: String. Format:
0 — any number 0-9
* — any symbol
a — A-Z, a-z
q — "q" letter, 2 — "2" letter etc.
\a — "a" letter
default is undefined
[function] reformat
: user function, if you want use custom reformat logic. It's userfull for numeric inputs, decimal numbers, emails, etc.
If reformat
defined mask
will be ignored. Reformat function must receive object with several fields:
1function reformat({value: string, selection: {start, end}, input: string}) { 2 // realization 3 4 return { 5 [any] value: // value that stored and called in input core functions (such as reformat). Field may have any format, 6 [String] visibleValue: // value that displayed to user in input if showMask is false, 7 [String] maskedValue: // value that displayed to user in input if showMask is true, 8 [{[integer] start, [integer] end}] selection: {start, end} — // new selection range 9 } 10}
If reformat
and mask
is undefined, input allows to enter any values.
You can define custom mask by passing maskFormat
. This prop must be an array,
each object in array have several fields:
str
: matched char for maskregexp
: validation rule as regexptype
: specialmaskChar
: Character to cover unfilled editable parts of mask. Default value is ''.
maskString
: String to cover unfilled editable parts of mask. Default is undefined. If maskString
define maskChar
ignored.
showMask
: show mask in input. It's possible only if mask have not cyclic. Default value = false
alwaysShowMask
: show mask when input inactive
Callbacks
:
onChange
(event). Event is synthetic react-event. If you want access to input value, you may use: event.target.value
onValueChange
(event). fires, when value was changed. Provides 2 fields: value
and maskedValue
getReference
: Callback to get native input ref
MIT
The code of react-maskinput was rewritten to react hooks. It should be compatible with the prevoius version (2.x), but in some complicated cases behavior could be changed.
No vulnerabilities found.
No security vulnerabilities found.