Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to use 'isRevealingMask' option #98

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ A default `placeholder` will be generated from the mask's pattern, but you can p

By default, the rendered `<input>`'s `size` will be the length of the pattern, but you can pass a `size` prop to override this.

### `isRevealingMask` : `boolean`

An optional property that, if true, progressively shows the mask as input is entered. Defaults to `false`

Example:
Given an input with a mask of `111-1111 x 111`, a value of `47`, and `isRevealingMask` set to `true`, then the input's value is formatted as `47`
Given the same input but with a value of `476`, then the input's value is formatted as `476-`
Given the same input but with a value of `47 3191`, then the input's value is formatted as `47_-3191 x `

See the [inputmask-core docs](https://github.com/insin/inputmask-core#isrevealingmask--boolean) for details.

### Other props

Any other props passed in will be passed as props to the rendered `<input>`, except for the following, which are managed by the component:
Expand Down
5 changes: 5 additions & 0 deletions demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const App = React.createClass({
<label htmlFor="expiry">Expiry Date:</label>
<MaskedInput mask="11/1111" name="expiry" id="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
</div>
<p>You can set 'isRevealingMask' to true to make mask revealing on your typing</p>
<div className="form-field">
<label htmlFor="expiry">Expiry Date:</label>
<MaskedInput mask="11/1111" name="expiry" id="expiry" placeholder="mm/yyyy" onChange={this._onChange} isRevealingMask/>
</div>
<div className="form-field">
<label htmlFor="ccv">CCV:</label>
<MaskedInput mask="111" name="ccv" id="ccv" onChange={this._onChange}/>
Expand Down
37 changes: 18 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ var MaskedInput = React.createClass({
if (this.props.placeholderChar) {
options.placeholderChar = this.props.placeholderChar
}
if (this.props.isRevealingMask) {
options.isRevealingMask = this.props.isRevealingMask
}
this.mask = new InputMask(options)
},

Expand Down Expand Up @@ -149,9 +152,7 @@ var MaskedInput = React.createClass({
this._updateInputSelection()
}
}
if (this.props.onChange) {
this.props.onChange(e)
}
this._updateValue(e)
},

_onKeyDown(e) {
Expand All @@ -162,9 +163,7 @@ var MaskedInput = React.createClass({
if (this.mask.undo()) {
e.target.value = this._getDisplayValue()
this._updateInputSelection()
if (this.props.onChange) {
this.props.onChange(e)
}
this._updateValue(e)
}
return
}
Expand All @@ -173,9 +172,7 @@ var MaskedInput = React.createClass({
if (this.mask.redo()) {
e.target.value = this._getDisplayValue()
this._updateInputSelection()
if (this.props.onChange) {
this.props.onChange(e)
}
this._updateValue(e)
}
return
}
Expand All @@ -189,9 +186,7 @@ var MaskedInput = React.createClass({
if (value) {
this._updateInputSelection()
}
if (this.props.onChange) {
this.props.onChange(e)
}
this._updateValue(e)
}
}
},
Expand All @@ -207,10 +202,9 @@ var MaskedInput = React.createClass({
this._updateMaskSelection()
if (this.mask.input((e.key || e.data))) {
e.target.value = this.mask.getValue()
window.mask = this.mask

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this change? This is not correct to add a variable directly to window object

this._updateInputSelection()
if (this.props.onChange) {
this.props.onChange(e)
}
this._updateValue(e)
}
},

Expand All @@ -224,9 +218,14 @@ var MaskedInput = React.createClass({
e.target.value = this.mask.getValue()
// Timeout needed for IE
setTimeout(this._updateInputSelection, 0)
if (this.props.onChange) {
this.props.onChange(e)
}
this._updateValue(e)
}
},

_updateValue(e) {
this.mask.setValue(this.mask.getValue())
if (this.props.onChange) {
this.props.onChange(e)
}
},

Expand Down Expand Up @@ -268,7 +267,7 @@ var MaskedInput = React.createClass({
var eventHandlers = this._getEventHandlers()
var { size = maxLength, placeholder = this.mask.emptyValue } = this.props

var {placeholderChar, formatCharacters, ...cleanedProps} = this.props
var {placeholderChar, formatCharacters, isRevealingMask, ...cleanedProps} = this.props
var inputProps = { ...cleanedProps, ...eventHandlers, ref, maxLength, value, size, placeholder }
return <input {...inputProps} />
}
Expand Down
36 changes: 36 additions & 0 deletions tests/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,42 @@ describe('MaskedInput', () => {
cleanup(el)
})

it('should handle updating value with isRevealingMask option', () => {
const el = setup()
let ref = null
let defaultMask = '1111 1111 1111 1111'

function render(props) {
ReactDOM.render(
<MaskedInput
ref={(r) => ref = r}
{...props}
/>,
el
)
}

render({mask: defaultMask, value: '', isRevealingMask: true})
let input = ReactDOM.findDOMNode(ref)

// initial state
expect(input.value).toBe('')
expect(input.placeholder).toBe('')
expect(input.size).toBe(19)
expect(input.selectionStart).toBe(0)

// update value
render({mask: defaultMask, value: '4111'})
input = ReactDOM.findDOMNode(ref)

// initial state
expect(input.value).toBe('4111 ')
expect(input.size).toBe(19)
expect(input.selectionStart).toBe(5)

cleanup(el)
})

it('should handle updating mask and value', () => {
const el = setup()
let ref = null
Expand Down