From 4d5a36f8b97f3ff4b51628f88d53b764697d72a3 Mon Sep 17 00:00:00 2001 From: Ben-hur Santos Ott Date: Fri, 17 Feb 2017 20:51:39 -0200 Subject: [PATCH] 1.3.1: improve performance --- README.md | 3 +++ lib/base-text-component.js | 26 ++++++++++++++++++-------- package.json | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index df821f79..56b83920 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,9 @@ var money = MaskService.toMask('money', '123', { # Changelog +## 1.3.1 +* Performance: adding check if the next value to be applied to the mask is equal as actual and prevent state change. + ## 1.3.0 * Feat: now you can check and prevent input text on `TextInputMask` using `checkText` prop. diff --git a/lib/base-text-component.js b/lib/base-text-component.js index 3648d57b..a9242497 100644 --- a/lib/base-text-component.js +++ b/lib/base-text-component.js @@ -26,11 +26,17 @@ export default class BaseTextComponent extends Component { return new Promise((resolve, reject) => { let maskedText = self._getMaskedValue(text); - self.setState({ - value: maskedText - }, () => { - resolve(maskedText); - }); + + if(self._mustUpdateValue(maskedText)) { + self.setState({ + value: maskedText + }, () => { + resolve(maskedText); + }); + } + else { + resolve(this.state.value); + } }); } @@ -48,6 +54,10 @@ export default class BaseTextComponent extends Component { ); } + _mustUpdateValue(newValue) { + return this.state.value !== newValue; + } + _resolveMaskHandler() { this._maskHandler = MaskResolver.resolve(this.state.type); } @@ -63,7 +73,7 @@ export default class BaseTextComponent extends Component { if(changeMaskHandler) { self._resolveMaskHandler(); } - + self.setState({ value: self._getMaskedValue(props.value) }); @@ -72,7 +82,7 @@ export default class BaseTextComponent extends Component { _getMaskedValue(value) { return this._maskHandler.getValue( - this._getDefaultValue(value), + this._getDefaultValue(value), this.state.options); } @@ -83,4 +93,4 @@ export default class BaseTextComponent extends Component { return value; } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 128c0077..1db8bd8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-masked-text", - "version": "1.3.0", + "version": "1.3.1", "description": "Text and TextInput with mask for React Native applications", "main": "index.js", "scripts": {