Skip to content

Commit db7335b

Browse files
committed
Update syntax in component and examples
1 parent be50d5e commit db7335b

File tree

3 files changed

+61
-82
lines changed

3 files changed

+61
-82
lines changed

README.md

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ A [React](http://facebook.github.io/react/) component for `<input>` masking, bui
1010

1111
### npm
1212

13-
`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/).
14-
1513
```
1614
npm install react-maskedinput --save
1715
```
1816

1917
### Browser bundle
2018

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

2321
* [react-maskedinput.js](https://unpkg.com/react-maskedinput/umd/react-maskedinput.js) (development version)
2422
* [react-maskedinput.min.js](https://unpkg.com/react-maskedinput/umd/react-maskedinput.min.js) (compressed production version)
@@ -28,21 +26,19 @@ The browser bundle exposes a global `MaskedInput` variable and expects to find a
2826
Give `MaskedInput` a [`mask`](#mask-string) and an `onChange` callback:
2927

3028
```javascript
31-
var React = require('react')
32-
var MaskedInput = require('react-maskedinput')
29+
import React from 'react'
30+
import MaskedInput from 'react-maskedinput'
3331

34-
var CreditCardDetails = React.createClass({
35-
state: {
32+
class CreditCardDetails extends React.Component {
33+
state = {
3634
card: '',
3735
expiry: '',
3836
ccv: ''
39-
},
37+
}
4038

41-
_onChange(e) {
42-
var stateChange = {}
43-
stateChange[e.target.name] = e.target.value
44-
this.setState(stateChange)
45-
},
39+
_onChange = (e) => {
40+
this.setState({[e.target.name]: e.target.value})
41+
}
4642

4743
render() {
4844
return <div className="CreditCardDetails">
@@ -60,28 +56,26 @@ var CreditCardDetails = React.createClass({
6056
</label>
6157
</div>
6258
}
63-
})
59+
}
6460
```
6561

6662
Create some wrapper components if you have a masking configuration which will be reused:
6763

6864
```javascript
69-
var CustomInput = React.createClass({
70-
render() {
71-
return <MaskedInput
72-
mask="1111-WW-11"
73-
placeholder="1234-WW-12"
74-
size="11"
75-
{...this.props}
76-
formatCharacters={{
77-
'W': {
78-
validate(char) { return /\w/.test(char ) },
79-
transform(char) { return char.toUpperCase() }
80-
}
65+
function CustomInput(props) {
66+
return <MaskedInput
67+
mask="1111-WW-11"
68+
placeholder="1234-WW-12"
69+
size="11"
70+
{...props}
71+
formatCharacters={{
72+
'W': {
73+
validate(char) { return /\w/.test(char ) },
74+
transform(char) { return char.toUpperCase() }
8175
}
82-
}/>
83-
}
84-
})
76+
}}
77+
/>
78+
}
8579
```
8680

8781
## Props

demo/src/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ class App extends React.Component {
2727
}
2828

2929
_onChange = (e) => {
30-
const stateChange = {}
31-
stateChange[e.target.name] = e.target.value
32-
this.setState(stateChange)
30+
this.setState({[e.target.name]: e.target.value})
3331
}
3432

3533
_changePattern = (e) => {
@@ -99,7 +97,7 @@ class App extends React.Component {
9997
<label htmlFor="changing">Credit Card:</label>
10098
<MaskedInput mask={this.state.cardPattern} name="creditCard" id="creditCard" onChange={this._onCardChange}/>
10199
</div>
102-
<p>Custom format character (W=[a-zA-Z0-9_], transformed to uppercase) and placeholder character (en space):</p>
100+
<p>Custom format character (<code>W=[a-zA-Z0-9_]</code>, transformed to uppercase) and placeholder character (en space):</p>
103101
<div className="form-field">
104102
<label htmlFor="custom">Custom:</label>
105103
<CustomInput name="custom" id="custom" onChange={this._onChange}/>
@@ -112,7 +110,7 @@ class App extends React.Component {
112110
}
113111
}
114112

115-
const CustomInput = (props) =>
113+
let CustomInput = (props) =>
116114
<MaskedInput
117115
mask="1111-WW-11"
118116
placeholder="1234-WW-12"
@@ -121,8 +119,8 @@ const CustomInput = (props) =>
121119
{...props}
122120
formatCharacters={{
123121
'W': {
124-
validate(char) { return /\w/.test(char) },
125-
transform(char) { return char.toUpperCase() }
122+
validate: (char) => /\w/.test(char),
123+
transform: (char) => char.toUpperCase()
126124
}
127125
}}
128126
/>

src/index.js

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import React from 'react'
22
import PropTypes from 'prop-types'
33
import InputMask from 'inputmask-core'
44

5-
var KEYCODE_Z = 90
6-
var KEYCODE_Y = 89
5+
let KEYCODE_Z = 90
6+
let KEYCODE_Y = 89
77

88
function isUndo(e) {
99
return (e.ctrlKey || e.metaKey) && e.keyCode === (e.shiftKey ? KEYCODE_Y : KEYCODE_Z)
@@ -14,17 +14,16 @@ function isRedo(e) {
1414
}
1515

1616
function getSelection (el) {
17-
var start, end, rangeEl, clone
18-
17+
let start, end
1918
if (el.selectionStart !== undefined) {
2019
start = el.selectionStart
2120
end = el.selectionEnd
2221
}
2322
else {
2423
try {
2524
el.focus()
26-
rangeEl = el.createTextRange()
27-
clone = rangeEl.duplicate()
25+
let rangeEl = el.createTextRange()
26+
let clone = rangeEl.duplicate()
2827

2928
rangeEl.moveToBookmark(document.selection.createRange().getBookmark())
3029
clone.setEndPoint('EndToStart', rangeEl)
@@ -39,16 +38,14 @@ function getSelection (el) {
3938
}
4039

4140
function setSelection(el, selection) {
42-
var rangeEl
43-
4441
try {
4542
if (el.selectionStart !== undefined) {
4643
el.focus()
4744
el.setSelectionRange(selection.start, selection.end)
4845
}
4946
else {
5047
el.focus()
51-
rangeEl = el.createTextRange()
48+
let rangeEl = el.createTextRange()
5249
rangeEl.collapse(true)
5350
rangeEl.moveStart('character', selection.start)
5451
rangeEl.moveEnd('character', selection.end - selection.start)
@@ -59,18 +56,19 @@ function setSelection(el, selection) {
5956
}
6057

6158
class MaskedInput extends React.Component {
62-
constructor(props) {
63-
super(props)
64-
65-
this._onChange = this._onChange.bind(this)
66-
this._onKeyDown = this._onKeyDown.bind(this)
67-
this._onPaste = this._onPaste.bind(this)
68-
this._onKeyPress = this._onKeyPress.bind(this)
69-
this._updateInputSelection = this._updateInputSelection.bind(this)
59+
static propTypes = {
60+
mask: PropTypes.string.isRequired,
61+
62+
formatCharacters: PropTypes.object,
63+
placeholderChar: PropTypes.string
64+
}
65+
66+
static defaultProps = {
67+
value: ''
7068
}
7169

7270
componentWillMount() {
73-
var options = {
71+
let options = {
7472
pattern: this.props.mask,
7573
value: this.props.value,
7674
formatCharacters: this.props.formatCharacters
@@ -129,11 +127,11 @@ class MaskedInput extends React.Component {
129127
setSelection(this.input, this.mask.selection)
130128
}
131129

132-
_onChange(e) {
130+
_onChange = (e) => {
133131
// console.log('onChange', JSON.stringify(getSelection(this.input)), e.target.value)
134132

135-
var maskValue = this.mask.getValue()
136-
var incomingValue = e.target.value
133+
let maskValue = this.mask.getValue()
134+
let incomingValue = e.target.value
137135
if (incomingValue !== maskValue) { // only modify mask if form contents actually changed
138136
this._updateMaskSelection()
139137
this.mask.setValue(incomingValue) // write the whole updated value into the mask
@@ -146,7 +144,7 @@ class MaskedInput extends React.Component {
146144
}
147145
}
148146

149-
_onKeyDown(e) {
147+
_onKeyDown = (e) => {
150148
// console.log('onKeyDown', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
151149

152150
if (isUndo(e)) {
@@ -176,7 +174,7 @@ class MaskedInput extends React.Component {
176174
e.preventDefault()
177175
this._updateMaskSelection()
178176
if (this.mask.backspace()) {
179-
var value = this._getDisplayValue()
177+
let value = this._getDisplayValue()
180178
e.target.value = value
181179
if (value) {
182180
this._updateInputSelection()
@@ -188,7 +186,7 @@ class MaskedInput extends React.Component {
188186
}
189187
}
190188

191-
_onKeyPress(e) {
189+
_onKeyPress = (e) => {
192190
// console.log('onKeyPress', JSON.stringify(getSelection(this.input)), e.key, e.target.value)
193191

194192
// Ignore modified key presses
@@ -206,7 +204,7 @@ class MaskedInput extends React.Component {
206204
}
207205
}
208206

209-
_onPaste(e) {
207+
_onPaste = (e) => {
210208
// console.log('onPaste', JSON.stringify(getSelection(this.input)), e.clipboardData.getData('Text'), e.target.value)
211209

212210
e.preventDefault()
@@ -215,15 +213,15 @@ class MaskedInput extends React.Component {
215213
if (this.mask.paste(e.clipboardData.getData('Text'))) {
216214
e.target.value = this.mask.getValue()
217215
// Timeout needed for IE
218-
setTimeout(this._updateInputSelection.bind(this), 0)
216+
setTimeout(() => this._updateInputSelection(), 0)
219217
if (this.props.onChange) {
220218
this.props.onChange(e)
221219
}
222220
}
223221
}
224222

225223
_getDisplayValue() {
226-
var value = this.mask.getValue()
224+
let value = this.mask.getValue()
227225
return value === this.mask.emptyValue ? '' : value
228226
}
229227

@@ -254,27 +252,16 @@ class MaskedInput extends React.Component {
254252
}
255253

256254
render() {
257-
var ref = r => { this.input = r }
258-
var maxLength = this.mask.pattern.length
259-
var value = this._getDisplayValue()
260-
var eventHandlers = this._getEventHandlers()
261-
var { size = maxLength, placeholder = this.mask.emptyValue } = this.props
262-
263-
var { placeholderChar, formatCharacters, ...cleanedProps } = this.props // eslint-disable-line
264-
var inputProps = { ...cleanedProps, ...eventHandlers, ref, maxLength, value, size, placeholder }
255+
let ref = r => { this.input = r }
256+
let maxLength = this.mask.pattern.length
257+
let value = this._getDisplayValue()
258+
let eventHandlers = this._getEventHandlers()
259+
let { size = maxLength, placeholder = this.mask.emptyValue } = this.props
260+
261+
let { placeholderChar, formatCharacters, ...cleanedProps } = this.props // eslint-disable-line no-unused-vars
262+
let inputProps = { ...cleanedProps, ...eventHandlers, ref, maxLength, value, size, placeholder }
265263
return <input {...inputProps} />
266264
}
267265
}
268266

269-
MaskedInput.propTypes = {
270-
mask: PropTypes.string.isRequired,
271-
272-
formatCharacters: PropTypes.object,
273-
placeholderChar: PropTypes.string
274-
}
275-
276-
MaskedInput.defaultProps = {
277-
value: ''
278-
}
279-
280267
export default MaskedInput

0 commit comments

Comments
 (0)