diff --git a/demo/src/index.js b/demo/src/index.js index 54a38d6..5584c08 100644 --- a/demo/src/index.js +++ b/demo/src/index.js @@ -44,69 +44,71 @@ class App extends React.Component { } render() { - return
-

- <MaskedInput/> -

-

A React component which creates a masked <input/>

-
- - + return ( +
+

+ <MaskedInput/> +

+

A React component which creates a masked <input/>

+
+ + +
+

You can even externally update the card state like a standard input element:

+
+ + +
+

Placeholders are automatically generated but can be overridden with your own:

+
+ + +
+
+ + +
+
+ + +
+

Mask placeholder characters can be escaped with a leading \ to use them as static contents:

+
+ + +
+

Leading static characters:

+
+ + +
+

Changing patterns:

+
+ + +
+
+ + +
+

Dynamically changing the pattern as the user types:

+
+ + +
+

Custom format character (W=[a-zA-Z0-9_], transformed to uppercase) and placeholder character (en space):

+
+ + +
+
+
{JSON.stringify(this.state, null, 2)}
+
+
-

You can even externally update the card state like a standard input element:

-
- - -
-

Placeholders are automatically generated but can be overridden with your own:

-
- - -
-
- - -
-
- - -
-

Mask placeholder characters can be escaped with a leading \ to use them as static contents:

-
- - -
-

Leading static characters:

-
- - -
-

Changing patterns:

-
- - -
-
- - -
-

Dynamically changing the pattern as the user types:

-
- - -
-

Custom format character (W=[a-zA-Z0-9_], transformed to uppercase) and placeholder character (en space):

-
- - -
-
-
{JSON.stringify(this.state, null, 2)}
-
- -
+ ) } } diff --git a/src/index.js b/src/index.js index 80ebb94..a4cc054 100644 --- a/src/index.js +++ b/src/index.js @@ -67,48 +67,29 @@ class MaskedInput extends React.Component { value: '' } - componentWillMount() { - let options = { - pattern: this.props.mask, - value: this.props.value, - formatCharacters: this.props.formatCharacters - } - if (this.props.placeholderChar) { - options.placeholderChar = this.props.placeholderChar - } - this.mask = new InputMask(options) - } - - componentWillReceiveProps(nextProps) { - if (this.props.mask !== nextProps.mask && this.props.value !== nextProps.mask) { + componentDidUpdate(prevProps) { + if (prevProps.mask !== this.props.mask && prevProps.value !== this.props.mask) { // if we get a new value and a new mask at the same time // check if the mask.value is still the initial value - // - if so use the nextProps value + // - if so use the this.props value // - otherwise the `this.mask` has a value for us (most likely from paste action) if (this.mask.getValue() === this.mask.emptyValue) { - this.mask.setPattern(nextProps.mask, {value: nextProps.value}) + this.mask.setPattern(this.props.mask, {value: this.props.value}) } else { - this.mask.setPattern(nextProps.mask, {value: this.mask.getRawValue()}) + this.mask.setPattern(this.props.mask, {value: this.mask.getRawValue()}) } } - else if (this.props.mask !== nextProps.mask) { - this.mask.setPattern(nextProps.mask, {value: this.mask.getRawValue()}) + else if (this.props.mask !== this.props.mask) { + this.mask.setPattern(this.props.mask, {value: this.mask.getRawValue()}) } - else if (this.props.value !== nextProps.value) { - this.mask.setValue(nextProps.value) + else if (this.props.value !== this.props.value) { + this.mask.setValue(this.props.value) } - } - componentWillUpdate(nextProps, nextState) { - if (nextProps.mask !== this.props.mask) { - this._updatePattern(nextProps) - } - } - - componentDidUpdate(prevProps) { - if (prevProps.mask !== this.props.mask && this.mask.selection.start) { - this._updateInputSelection() + if (this.props.mask !== prevProps.mask) { + this.mask.selection.start && this._updateInputSelection() + this._updatePattern(this.props) } } @@ -252,6 +233,17 @@ class MaskedInput extends React.Component { } render() { + let options = { + pattern: this.props.mask, + value: this.props.value, + formatCharacters: this.props.formatCharacters + } + + if (this.props.placeholderChar) { + options.placeholderChar = this.props.placeholderChar + } + + this.mask = new InputMask(options) let ref = r => { this.input = r } let maxLength = this.mask.pattern.length let value = this._getDisplayValue()