Skip to content

Commit 12b3323

Browse files
committed
Update the mask's pattern if it changes
insin#8
1 parent 2174ed7 commit 12b3323

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

demo/index.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@
5353
<div id="app"></div>
5454
<script type="text/jsx;harmony=true">void function() { 'use strict';
5555

56+
var PATTERNS = [
57+
'1111 1111',
58+
'111 111',
59+
'11 11',
60+
'1 1'
61+
]
62+
5663
var App = React.createClass({
5764
getInitialState() {
5865
return {
@@ -61,7 +68,9 @@
6168
ccv: '',
6269
plate: '',
6370
escaped: '',
64-
custom: ''
71+
custom: '',
72+
changing: '',
73+
pattern: '1111 1111'
6574
}
6675
},
6776

@@ -71,6 +80,10 @@
7180
this.setState(stateChange)
7281
},
7382

83+
_changePattern(e) {
84+
this.setState({pattern: e.target.value})
85+
},
86+
7487
render() {
7588
return <div className="App">
7689
<h1><code>&lt;<a href="https://github.com/insin/react-maskedinput">MaskedInput</a>/&gt;</code></h1>
@@ -97,6 +110,17 @@ <h1><code>&lt;<a href="https://github.com/insin/react-maskedinput">MaskedInput</
97110
<label htmlFor="escaped">Escaped:</label>
98111
<MaskedInput pattern="11 \* 11" name="escaped" id="escaped" onChange={this._onChange}/>
99112
</div>
113+
<p>Changing patterns:</p>
114+
<div className="form-field">
115+
<label htmlFor="changing">Input:</label>
116+
<MaskedInput pattern={this.state.pattern} name="changing" id="changing" onChange={this._onChange}/>
117+
</div>
118+
<div className="form-field">
119+
<label htmlFor="pattern">Pattern:</label>
120+
<select onChange={this._changePattern}>
121+
{PATTERNS.map(pattern => <option value={pattern}>{pattern}</option>)}
122+
</select>
123+
</div>
100124
<p>Custom format character (W=[a-zA-Z0-9_], transformed to uppercase) and placeholder character (en space):</p>
101125
<div className="form-field">
102126
<label htmlFor="custom">Custom:</label>

src/index.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ var MaskedInput = React.createClass({
4242
this.mask = new InputMask(options)
4343
},
4444

45+
componentWillReceiveProps(nextProps) {
46+
if (this.props.pattern !== nextProps.pattern) {
47+
this.mask.setPattern(nextProps.pattern, {value: this.mask.getRawValue()})
48+
}
49+
},
50+
4551
_updateMaskSelection() {
4652
this.mask.selection = getSelection(this.getDOMNode())
4753
},

0 commit comments

Comments
 (0)