Skip to content

Commit 64ea19e

Browse files
committed
Fixes rjsf-team#174: Fixed playground didn't update editors.
r=@leplatrem
1 parent 2979aaa commit 64ea19e

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

playground/app.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ import "codemirror/theme/solarized.css";
1818
import "codemirror/theme/monokai.css";
1919
import "codemirror/theme/eclipse.css";
2020

21+
// Patching CodeMirror#componentWillReceiveProps so it's executed synchronously
22+
// Ref https://github.com/mozilla-services/react-jsonschema-form/issues/174
23+
Codemirror.prototype.componentWillReceiveProps = function (nextProps) {
24+
if (this.codeMirror &&
25+
nextProps.value !== undefined &&
26+
this.codeMirror.getValue() != nextProps.value) {
27+
this.codeMirror.setValue(nextProps.value);
28+
}
29+
if (typeof nextProps.options === "object") {
30+
for (var optionName in nextProps.options) {
31+
if (nextProps.options.hasOwnProperty(optionName)) {
32+
this.codeMirror.setOption(optionName, nextProps.options[optionName]);
33+
}
34+
}
35+
}
36+
};
37+
2138
const log = (type) => console.log.bind(console, type);
2239
const fromJson = (json) => JSON.parse(json);
2340
const toJson = (val) => JSON.stringify(val, null, 2);
@@ -113,9 +130,8 @@ class GeoPosition extends Component {
113130

114131
onChange(name) {
115132
return (event) => {
116-
this.setState({
117-
[name]: parseFloat(event.target.value)
118-
}, () => this.props.onChange(this.state));
133+
this.setState({[name]: parseFloat(event.target.value)});
134+
setImmediate(() => this.props.onChange(this.state));
119135
};
120136
}
121137

@@ -145,27 +161,23 @@ class GeoPosition extends Component {
145161
}
146162

147163
class Editor extends Component {
148-
defaultProps = {
149-
forceRender: false
150-
};
151-
152164
constructor(props) {
153165
super(props);
154166
this.state = {valid: true, code: props.code};
155167
}
156168

157169
componentWillReceiveProps(props) {
158-
this.setState({code: props.code});
170+
this.setState({valid: true, code: props.code});
159171
}
160172

161173
shouldComponentUpdate(nextProps, nextState) {
162-
return this.props.forceRender || shouldRender(this, nextProps, nextState);
174+
return shouldRender(this, nextProps, nextState);
163175
}
164176

165177
onCodeChange = (code) => {
166178
try {
167179
this.setState({valid: true, code});
168-
this.props.onChange(fromJson(this.state.code));
180+
setImmediate(() => this.props.onChange(fromJson(this.state.code)));
169181
} catch(err) {
170182
console.error(err);
171183
this.setState({valid: false, code});
@@ -205,7 +217,7 @@ class Selector extends Component {
205217
return (event) => {
206218
event.preventDefault();
207219
this.setState({current: label});
208-
this.props.onSelected(samples[label]);
220+
setImmediate(() => this.props.onSelected(samples[label]));
209221
};
210222
};
211223

@@ -278,8 +290,9 @@ class App extends Component {
278290
onFormDataEdited = (formData) => this.setState({formData});
279291

280292
onThemeSelected = (theme, {stylesheet, editor}) => {
281-
// Side effect!
282-
this.setState({theme, editor: editor ? editor : "default"}, _ => {
293+
this.setState({theme, editor: editor ? editor : "default"});
294+
setImmediate(() => {
295+
// Side effect!
283296
document.getElementById("theme").setAttribute("href", stylesheet);
284297
});
285298
};
@@ -305,8 +318,7 @@ class App extends Component {
305318
<Editor title="JSONSchema"
306319
theme={this.state.editor}
307320
code={toJson(this.state.schema)}
308-
onChange={this.onSchemaEdited}
309-
forceRender={true} />
321+
onChange={this.onSchemaEdited} />
310322
<div className="row">
311323
<div className="col-sm-6">
312324
<Editor title="UISchema"

0 commit comments

Comments
 (0)