From 6b1f7c9b985c5f100fb00b44451f634b0b438753 Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Fri, 5 May 2017 15:02:58 +0200 Subject: [PATCH] Reduce calls to `onChange` of the parent form. --- src/FormsyText.jsx | 2 +- test/test.FormsyText.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/FormsyText.jsx b/src/FormsyText.jsx index 1a9fd78..a1bb326 100644 --- a/src/FormsyText.jsx +++ b/src/FormsyText.jsx @@ -47,7 +47,7 @@ const FormsyText = React.createClass({ if (isValueChanging || this.props.defaultValue === this.getValue()) { this.setState({ value, isValid }); - this.setValue(value); + if (this.getValue() !== value) this.setValue(value); } } }, diff --git a/test/test.FormsyText.js b/test/test.FormsyText.js index 2b1dd1e..79409b3 100644 --- a/test/test.FormsyText.js +++ b/test/test.FormsyText.js @@ -234,6 +234,20 @@ describe('FormsyText', () => { formValues = formsyForm.getCurrentValues(); expect(formValues.text).to.eq('some text'); }); + + it('calls onChange once per text update', () => { + const calls = []; + const { parent, formsyTextWrapper } = makeTestParent({ + onChange: (...args) => calls.push(args), + value: 'initial', + }); + + const inputDOM = formsyTextWrapper.find('input').node; + inputDOM.value = 'updated'; + parent.setState({ value: 'updated' }); + + expect(calls.length).to.eq(1); + }); }); describe('resetting to', () => {