Set of input tools for formatting
This project allow to create masked inputs easily. In real world you often need to create input for credit card, phone number or birthday date etc. Each of this usecases require to input value with some formatting (for example 0000-0000-000-0000 for credit card). This project could help you.
Watch demo: http://xnimorz.github.io/masked-input/
Components
- react-maskinput — react masked input,
- react-numberinput — react numeric input,
- input-core — the core module on top of which you can build any custom components,
- mask-input — vanilla masked input.
react-maskinput
A react component provide interface for creating inputs with custom mask. This component built on top of input-core.
React masked input was tested on desktop and mobile browsers:
- Desktop browsers:
- Safari >= 9
- Google Chrome
- EDGE, IE11
- Mozilla Firefox
- Opera, Yandex.browser etc.
- Mobile browsers:
- Android Chrome
- Safari IOS >= 9
Installation
npm install --save react-maskinput
or
yarn add react-maskinput
Usage
Simple usage:
; ReactDOM
Changing mask in runtime, getting value on input change:
;;; state = maskString: 'DD.MM.YYYY' mask: '00.00.0000' { // 2 — for example if > 2 this; else this; } { return <MaskInput onChange=thisonChange maskString=thisstatemaskString mask=thisstatemask alwaysShowMask /> ; } ;
If you need to get input's HtmlElement, you could use getReference
prop:
;;; { // Now we can work with HtmlElement thisinput = el; } { return <MaskInput getReference=thisgetInputRef alwaysShowMask maskChar='_' mask='0000-0000-0000-0000' /> ; } ;
Custom formatting function. You can create custom formatting function by setting reformat
prop.
As an example, you can see react-numberinput component:
;; { if selectionstart === selectionend return value; if selectionend < selectionstart const tmp = selectionend; selectionend = selectionstart; selectionstart = tmp; if valuelength > selectionstart return value; return value;} // In reformat function you need to set value and selection props { const newSelection = start: selectionstart end: selectionend ; let value = ; const inputValue = input; const oldLength = valuelength; value = value + inputValue + value; value = value; let index = newSelectionstart; if inputValue index = Math; newSelectionend = newSelectionstart = index; return value maskedValue: value visibleValue: value selection: newSelection ; } { return <MaskInput ...thisprops reformat=thisreformat /> ; } ;
Usage with styled-components
react-maskinput, react-numberinput pass props to input
element directly.
You can set up different input element properties: class, data-attributes, etc.
There components works well with another libraries, CSS-in-JS solutions, such as styled-components:
;;; const MaskedInput = ` border-radius: 10px; border-color: rgb(219,112,147);`; ;
Props
List of specific react-maskinput props:
- mask [String],
- reformat [Function],
- maskFormat [Array],
- maskChar [Empty string or String with one char],
- maskString [String],
- showMask [Boolean],
- alwaysShowMask [Boolean],
- getReference [Function],
- onChange [Function]
Important! All other props'll passed to input
element directly. So you can set up class name, data attributes, etc.
Let's see what's doing each of 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 etc.
If reformat
defined mask
will be ignored. Reformat function must receive object with several fields:
{ // realization return any value: // value that stored and called in input core functions (such as reformat). Field may have any format, String visibleValue: // value that displayed to user in input if showMask is false, String maskedValue: // value that displayed to user in input if showMask is true, integer start integer end selection: start end — // new selection range }
If reformat
and mask
is undefined, input allow 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 mask
regexp
: validation rule as regexp
type
: special
maskChar
: 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
getReference
: Callback to get native input ref
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -m 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
Changelog
1.0.0 several changes:
- From this moment all of tools will have similar version
- Added examples using components with another libs, such as
styled-components
- Improved demo page
- Improved readme
0.1.8 Use input-core@0.1.2
0.1.7 Remove reformat prop from the input element
0.1.6 Add e.which to input event callback to support iOS@9.4
0.1.5 Add applyValue method in case you need apply new value directly (by ref)
0.1.3 Add onFocus and onBlur callbacks. Add getReference function to examples
0.1.2 Add android support, remove transform-react-jsx from mask-input build
0.1.1 Fix bug with removing static symbol
0.1.0 First publish
TODO
- Cover all input-core with unit tests
- Dynamically change props in demo page
License
MIT