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

Preventing paste in disabled state #124

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/umd
npm-debug.log*
package-lock.json
/.vscode
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.0.2 / 2018-02-08

* Prevent paste in disabled state

## 4.0.1 / 2018-01-26 🇦🇺

* Fix auto-fill scenarios by using data from `onChange` events [[#112](https://github.com/insin/react-maskedinput/pull/112)]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ 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.

### `onBadInput` : `() => void`

A callback which will be called any time the user inputs or pastes invalid character(s).****

### 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
25 changes: 22 additions & 3 deletions demo/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './style.css'

import React from 'react'
import {render} from 'react-dom'
import {render, findDOMNode} from 'react-dom'

import MaskedInput from '../../src'

Expand All @@ -12,7 +12,7 @@ const PATTERNS = [
'1 1',
]

class App extends React.Component {
class App extends React.PureComponent {
state = {
card: '',
expiry: '',
Expand Down Expand Up @@ -43,6 +43,24 @@ class App extends React.Component {
}
}

_onBadInput = () => {
if (this.timerId) {
return;
}
this.inputCardNode.classList.add('is-invalid');
this.timerId = setTimeout(() => {
delete this.timerId;
this.inputCardNode.classList.remove('is-invalid');
}, 400);
};

componentWillUnmount() {
if (this.timerId) {
clearTimeout(this.timerId);
delete this.timerId;
}
};

render() {
return <div className="App">
<h1>
Expand All @@ -51,7 +69,8 @@ class App extends React.Component {
<p className="lead">A React component which creates a masked <code>&lt;input/&gt;</code></p>
<div className="form-field">
<label htmlFor="card">Card Number:</label>
<MaskedInput mask="1111 1111 1111 1111" name="card" id="card" size="20" value={this.state.card} onChange={this._onChange}/>
<MaskedInput mask="1111 1111 1111 1111" name="card" id="card" size="20" value={this.state.card} onChange={this._onChange}
ref={input => this.inputCardNode = findDOMNode(input)} onBadInput={this._onBadInput} />
</div>
<p>You can even externally update the card state like a standard input element:</p>
<div className="form-field">
Expand Down
5 changes: 5 additions & 0 deletions demo/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ input {
footer {
text-align: center;
}
.is-invalid {
outline-color: #efa2a9;
-webkit-transition: outline-color 0.2s;
transition: outline-color 0.2s;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-maskedinput",
"version": "4.0.1",
"version": "4.0.2",
"description": "Masked <input/> React component",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
25 changes: 20 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ function setSelection(el, selection) {
catch (e) { /* not focused or not visible */ }
}

class MaskedInput extends React.Component {
class MaskedInput extends React.PureComponent {
static propTypes = {
mask: PropTypes.string.isRequired,

onChange: PropTypes.func,
formatCharacters: PropTypes.object,
placeholderChar: PropTypes.string
placeholderChar: PropTypes.string,
value: PropTypes.any,
placeholder: PropTypes.string,
size: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
onBadInput: PropTypes.func,
}

static defaultProps = {
Expand Down Expand Up @@ -202,15 +206,19 @@ class MaskedInput extends React.Component {
this.props.onChange(e)
}
}
else {
this._fireBadInput()
}
}

_onPaste = (e) => {
// console.log('onPaste', JSON.stringify(getSelection(this.input)), e.clipboardData.getData('Text'), e.target.value)
const {disabled} = this.props

e.preventDefault()
this._updateMaskSelection()
// getData value needed for IE also works in FF & Chrome
if (this.mask.paste(e.clipboardData.getData('Text'))) {
if (!disabled && this.mask.paste(e.clipboardData.getData('Text'))) {
e.target.value = this.mask.getValue()
// Timeout needed for IE
setTimeout(() => this._updateInputSelection(), 0)
Expand Down Expand Up @@ -243,6 +251,13 @@ class MaskedInput extends React.Component {
}
}

_fireBadInput () {
const {onBadInput} = this.props
if (typeof onBadInput === 'function') {
onBadInput()
}
}

focus() {
this.input.focus()
}
Expand All @@ -258,7 +273,7 @@ class MaskedInput extends React.Component {
let eventHandlers = this._getEventHandlers()
let { size = maxLength, placeholder = this.mask.emptyValue } = this.props

let { placeholderChar, formatCharacters, ...cleanedProps } = this.props // eslint-disable-line no-unused-vars
let { placeholderChar, formatCharacters, onBadInput, ...cleanedProps } = this.props // eslint-disable-line no-unused-vars
let inputProps = { ...cleanedProps, ...eventHandlers, ref, maxLength, value, size, placeholder }
return <input {...inputProps} />
}
Expand Down