From 40f2b7715c6e6444e094f148afff215398f6fc9f Mon Sep 17 00:00:00 2001 From: Samuel Slotsky Date: Tue, 20 Sep 2016 13:27:47 -0500 Subject: [PATCH] Make debounce specific to the instance --- src/Codemirror.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Codemirror.js b/src/Codemirror.js index ebbe143..fb60216 100644 --- a/src/Codemirror.js +++ b/src/Codemirror.js @@ -33,6 +33,18 @@ const CodeMirror = React.createClass({ this.codeMirror.on('blur', this.focusChanged.bind(this, false)); this.codeMirror.on('scroll', this.scrollChanged); this.codeMirror.setValue(this.props.defaultValue || this.props.value || ''); + this.handleUpdate = debounce(function(nextProps) { + if (this.codeMirror && nextProps.value !== undefined && this.codeMirror.getValue() !== nextProps.value) { + this.codeMirror.setValue(nextProps.value); + } + if (typeof nextProps.options === 'object') { + for (let optionName in nextProps.options) { + if (nextProps.options.hasOwnProperty(optionName)) { + this.codeMirror.setOption(optionName, nextProps.options[optionName]); + } + } + } + }, 0); }, componentWillUnmount () { // is there a lighter-weight way to remove the cm instance? @@ -40,6 +52,11 @@ const CodeMirror = React.createClass({ this.codeMirror.toTextArea(); } }, + + componentWillReceiveProps: function componentWillReceiveProps(nextProps) { + this.handleUpdate(nextProps); + }, + componentWillReceiveProps: debounce(function (nextProps) { if (this.codeMirror && nextProps.value !== undefined && this.codeMirror.getValue() !== nextProps.value) { this.codeMirror.setValue(nextProps.value);