Skip to content

Commit 5a3f45f

Browse files
committed
Use this.props.mask instead of this.props.pattern to avoid preventing use of the HTML 5 pattern attribute
Fixes insin#15
1 parent 94b95da commit 5a3f45f

File tree

5 files changed

+35
-47
lines changed

5 files changed

+35
-47
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
**Breaking change:** Now uses a `mask` prop to define the input mask instead of `pattern`, to avoid preventing use of the the HTML5 `pattern` attribute in conjunction with the input mask.
2+
13
**Breaking change:** React >= 0.14 is now required.
24

35
React 0.14 compatibility. [[jquense][jquense]]

README.md

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
# `MaskedInput`
22

3-
A [React](http://facebook.github.io/react/) component for `<input>` masking,
4-
built on top of [inputmask-core](https://github.com/insin/inputmask-core).
3+
A [React](http://facebook.github.io/react/) component for `<input>` masking, built on top of [inputmask-core](https://github.com/insin/inputmask-core).
54

65
## [Live Demo](http://insin.github.io/react-maskedinput/)
76

87
## Install
98

109
### npm
1110

12-
`MaskedInput` can be used on the server, or bundled for the client using an
13-
npm-compatible packaging system such as [Browserify](http://browserify.org/) or
14-
[webpack](http://webpack.github.io/).
11+
`MaskedInput` can be used on the server, or bundled for the client using an npm-compatible packaging system such as [Browserify](http://browserify.org/) or [webpack](http://webpack.github.io/).
1512

1613
```
1714
npm install react-maskedinput --save
1815
```
1916

2017
### Browser bundle
2118

22-
The browser bundle exposes a global `MaskedInput` variable and expects to find
23-
a global `React` variable to work with.
19+
The browser bundle exposes a global `MaskedInput` variable and expects to find a global `React` (>= 0.14.0) variable to work with.
2420

2521
You can find it in the [/dist directory](https://github.com/insin/react-maskedinput/tree/master/dist).
2622

2723
## Usage
2824

29-
Give `MaskedInput` a [`pattern`](#pattern-string) and an `onChange` callback:
25+
Give `MaskedInput` a [`mask`](#mask-string) and an `onChange` callback:
3026

3127
```javascript
3228
var React = require('react')
@@ -49,29 +45,28 @@ var CreditCardDetails = React.createClass({
4945
return <div className="CreditCardDetails">
5046
<label>
5147
Card Number:{' '}
52-
<MaskedInput pattern="1111 1111 1111 1111" name="card" size="20" onChange={this._onChange}/>
48+
<MaskedInput mask="1111 1111 1111 1111" name="card" size="20" onChange={this._onChange}/>
5349
</label>
5450
<label>
5551
Expiry Date:{' '}
56-
<MaskedInput pattern="11/1111" name="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
52+
<MaskedInput mask="11/1111" name="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
5753
</label>
5854
<label>
5955
CCV:{' '}
60-
<MaskedInput pattern="111" name="ccv" onChange={this._onChange}/>
56+
<MaskedInput mask="111" name="ccv" onChange={this._onChange}/>
6157
</label>
6258
</div>
6359
}
6460
})
6561
```
6662

67-
Create some wrapper components if you have a masking configuration which will be
68-
reused:
63+
Create some wrapper components if you have a masking configuration which will be reused:
6964

7065
```javascript
7166
var CustomInput = React.createClass({
7267
render() {
7368
return <MaskedInput
74-
pattern="1111-WW-11"
69+
mask="1111-WW-11"
7570
placeholder="1234-WW-12"
7671
size="11"
7772
{...this.props}
@@ -88,30 +83,25 @@ var CustomInput = React.createClass({
8883

8984
## Props
9085

91-
### `pattern` : `string`
86+
### `mask` : `string`
9287

9388
The masking pattern to be applied to the `<input>`.
9489

95-
See the [inputmask-core docs](https://github.com/insin/inputmask-core#pattern)
96-
for supported formatting characters.
90+
See the [inputmask-core docs](https://github.com/insin/inputmask-core#pattern) for supported formatting characters.
9791

9892
### `onChange` : `(event: SyntheticEvent) => any`
9993

10094
A callback which will be called any time the mask's value changes.
10195

102-
This will be passed a `SyntheticEvent` with the input accessible via
103-
`event.target` as usual.
96+
This will be passed a `SyntheticEvent` with the input accessible via `event.target` as usual.
10497

105-
**Note:** this component currently calls `onChange` directly, it does not
106-
generate an `onChange` event which will bubble up like a regular `<input>`
107-
component, so you *must* pass an `onChange` if you want to get a value back out.
98+
**Note:** this component currently calls `onChange` directly, it does not generate an `onChange` event which will bubble up like a regular `<input>` component, so you *must* pass an `onChange` if you want to get a value back out.
10899

109100
### `formatCharacters`: `Object`
110101

111102
Customised format character definitions for use in the pattern.
112103

113-
See the [inputmask-core docs](https://github.com/insin/inputmask-core#formatcharacters)
114-
for details of the structure of this object.
104+
See the [inputmask-core docs](https://github.com/insin/inputmask-core#formatcharacters) for details of the structure of this object.
115105

116106
### `placeholderChar`: `string`
117107

@@ -125,21 +115,17 @@ A default value for the mask.
125115

126116
### `placeholder` : `string`
127117

128-
A default `placeholder` will be generated from the mask's pattern, but you can
129-
pass a `placeholder` prop to provide your own.
118+
A default `placeholder` will be generated from the mask's pattern, but you can pass a `placeholder` prop to provide your own.
130119

131120
### `size` : `number | string`
132121

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

136124
### Other props
137125

138-
Any other props passed in will be passed as props to the rendered `<input>`,
139-
except for the following, which are managed by the component:
126+
Any other props passed in will be passed as props to the rendered `<input>`, except for the following, which are managed by the component:
140127

141128
* `maxLength` - will always be equal to the pattern's `.length`
142-
* `onKeyDown`, `onKeyPress` & `onPaste` - will each trigger a call to `onChange`
143-
when necessary
129+
* `onKeyDown`, `onKeyPress` & `onPaste` - will each trigger a call to `onChange` when necessary
144130

145131
## MIT Licensed

demo/index.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</head>
5353
<body>
5454
<div id="app"></div>
55-
<script type="text/jsx;harmony=true">void function() { 'use strict';
55+
<script type="text/jsx;harmony=true">void function() { 'use strict'
5656

5757
var PATTERNS = [
5858
'1111 1111',
@@ -92,35 +92,35 @@ <h1><code>&lt;<a href="https://github.com/insin/react-maskedinput">MaskedInput</
9292
<p className="lead">A React component which creates a masked <code>&lt;input/&gt;</code></p>
9393
<div className="form-field">
9494
<label htmlFor="card">Card Number:</label>
95-
<MaskedInput pattern="1111 1111 1111 1111" name="card" id="card" size="20" onChange={this._onChange}/>
95+
<MaskedInput mask="1111 1111 1111 1111" name="card" id="card" size="20" onChange={this._onChange}/>
9696
</div>
9797
<p>Placeholders are automatically generated but can be overridden with your own:</p>
9898
<div className="form-field">
9999
<label htmlFor="expiry">Expiry Date:</label>
100-
<MaskedInput pattern="11/1111" name="expiry" id="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
100+
<MaskedInput mask="11/1111" name="expiry" id="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
101101
</div>
102102
<div className="form-field">
103103
<label htmlFor="ccv">CCV:</label>
104-
<MaskedInput pattern="111" name="ccv" id="ccv" onChange={this._onChange}/>
104+
<MaskedInput mask="111" name="ccv" id="ccv" onChange={this._onChange}/>
105105
</div>
106106
<div className="form-field">
107107
<label htmlFor="plate">License Plate:</label>
108-
<MaskedInput pattern="AAA 1111" name="plate" id="plate" onChange={this._onChange} placeholder="ABC 1234"/>
108+
<MaskedInput mask="AAA 1111" name="plate" id="plate" onChange={this._onChange} placeholder="ABC 1234"/>
109109
</div>
110110
<p>Mask placeholder characters can be escaped with a leading <code>\</code> to use them as static contents:</p>
111111
<div className="form-field">
112112
<label htmlFor="escaped">Escaped:</label>
113-
<MaskedInput pattern="11 \* 11" name="escaped" id="escaped" onChange={this._onChange}/>
113+
<MaskedInput mask="11 \* 11" name="escaped" id="escaped" onChange={this._onChange}/>
114114
</div>
115115
<p>Leading static characters:</p>
116116
<div className="form-field">
117117
<label htmlFor="leading">Leading:</label>
118-
<MaskedInput pattern="(0) 111 1111" name="leading" id="leading" onChange={this._onChange}/>
118+
<MaskedInput mask="(0) 111 1111" name="leading" id="leading" onChange={this._onChange}/>
119119
</div>
120120
<p>Changing patterns:</p>
121121
<div className="form-field">
122122
<label htmlFor="changing">Input:</label>
123-
<MaskedInput pattern={this.state.pattern} name="changing" id="changing" onChange={this._onChange}/>
123+
<MaskedInput mask={this.state.pattern} name="changing" id="changing" onChange={this._onChange}/>
124124
</div>
125125
<div className="form-field">
126126
<label htmlFor="pattern">Pattern:</label>
@@ -144,7 +144,7 @@ <h1><code>&lt;<a href="https://github.com/insin/react-maskedinput">MaskedInput</
144144
var CustomInput = React.createClass({
145145
render() {
146146
return <MaskedInput
147-
pattern="1111-WW-11"
147+
mask="1111-WW-11"
148148
placeholder="1234-WW-12"
149149
placeholderChar=" "
150150
size="11"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-maskedinput",
33
"description": "Masked <input/> React component",
4-
"version": "2.0.0",
4+
"version": "3.0.0",
55
"main": "./lib/index.js",
66
"standalone": "MaskedInput",
77
"homepage": "https://github.com/insin/react-maskedinput",

src/index.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function isRedo(e) {
1818

1919
var MaskedInput = React.createClass({
2020
propTypes: {
21-
pattern: React.PropTypes.string.isRequired,
21+
mask: React.PropTypes.string.isRequired,
2222

2323
formatCharacters: React.PropTypes.object,
2424
placeholderChar: React.PropTypes.string
@@ -32,7 +32,7 @@ var MaskedInput = React.createClass({
3232

3333
componentWillMount() {
3434
var options = {
35-
pattern: this.props.pattern,
35+
pattern: this.props.mask,
3636
value: this.props.value,
3737
formatCharacters: this.props.formatCharacters
3838
}
@@ -43,8 +43,8 @@ var MaskedInput = React.createClass({
4343
},
4444

4545
componentWillReceiveProps(nextProps) {
46-
if (this.props.pattern !== nextProps.pattern) {
47-
this.mask.setPattern(nextProps.pattern, {value: this.mask.getRawValue()})
46+
if (this.props.mask !== nextProps.mask) {
47+
this.mask.setPattern(nextProps.mask, {value: this.mask.getRawValue()})
4848
}
4949
},
5050

@@ -150,7 +150,7 @@ var MaskedInput = React.createClass({
150150
},
151151

152152
render() {
153-
var {pattern, formatCharacters, size, placeholder, ...props} = this.props
153+
var {mask, formatCharacters, size, placeholder, ...props} = this.props
154154
var patternLength = this.mask.pattern.length
155155
return <input {...props}
156156
ref={r => this.input = r }

0 commit comments

Comments
 (0)