Skip to content

Commit f12f381

Browse files
committed
Fix #67: Don't cache oneOf/anyOf subschema
1 parent a25896a commit f12f381

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

src/ui.js

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,11 @@ class OneOfTopLevel extends React.Component {
519519

520520
this.schemaName = this.props.schemaName || 'oneOf';
521521

522-
this.state = {
523-
option: this.findSelectedOption(),
524-
};
522+
// Uncomment when caching is implemented
523+
//
524+
// this.state = {
525+
// option: this.findSelectedOption(),
526+
// };
525527
}
526528

527529
findSelectedOption = () => {
@@ -560,25 +562,35 @@ class OneOfTopLevel extends React.Component {
560562
return schema;
561563
}
562564

563-
handleChange = (e) => {
564-
this.updateData(this.getSchema(), this.getSchema(e.target.value));
565-
this.setState({
566-
option: e.target.value
567-
});
568-
}
565+
handleOptionChange = (e) => {
566+
this.updateData(this.getSchema(e.target.value));
569567

570-
updateData(oldSchema, newSchema) {
571-
let oldType = getSchemaType(oldSchema);
572-
let newType = getSchemaType(newSchema);
568+
// Uncomment when caching is reimplemented
569+
//
570+
// this.setState({
571+
// option: e.target.value
572+
// });
573+
}
573574

575+
updateData(newSchema) {
574576
this.props.args.onChange(
575577
this.props.args.name,
576578
getBlankData(newSchema, this.props.args.getRef)
577579
);
578580
}
579581

580582
render() {
581-
let schema = this.getSchema();
583+
/* Perfomance note:
584+
*
585+
* In order to resolve https://github.com/bhch/react-json-form/issues/67,
586+
* we will not cache the selected option. Instead, we'll recalculate the
587+
* selected option on every render.
588+
*
589+
* If there're serious performance issues, we'll reconsider caching.
590+
*/
591+
let selectedOption = this.findSelectedOption();
592+
593+
let schema = this.getSchema(selectedOption);
582594
let type = getSchemaType(schema);
583595
let args = this.props.args;
584596
let rowFunc;
@@ -605,9 +617,9 @@ class OneOfTopLevel extends React.Component {
605617
<div className="rjf-form-group rjf-oneof-group rjf-oneof-group-top-level">
606618
<div className="rjf-oneof-selector">
607619
<FormSelectInput
608-
value={this.state.option}
620+
value={selectedOption}
609621
options={this.getOptions()}
610-
onChange={this.handleChange}
622+
onChange={this.handleOptionChange}
611623
className="rjf-oneof-selector-input"
612624
label={selectorLabel}
613625
/>
@@ -625,11 +637,15 @@ class OneOf extends React.Component {
625637

626638
this.schemaName = this.props.schemaName || 'oneOf';
627639

628-
this.state = {
629-
option: this.findSelectedOption(),
630-
};
640+
// Uncomment when caching is implemented
641+
//
642+
// this.state = {
643+
// option: this.findSelectedOption(),
644+
// };
631645
}
632646

647+
/* Uncomment when caching is implemente
648+
633649
componentDidUpdate(prevProps, prevState) {
634650
if (prevProps.nextArgs || this.props.nextArgs) {
635651
let prevDataType = 'string';
@@ -643,6 +659,7 @@ class OneOf extends React.Component {
643659
this.setState({option: this.findSelectedOption()});
644660
}
645661
}
662+
*/
646663

647664
findSelectedOption = () => {
648665
/* Returns index of currently selected option.
@@ -788,11 +805,13 @@ class OneOf extends React.Component {
788805
return getSchemaType(this.props.parentArgs.schema);
789806
}
790807

791-
handleChange = (e) => {
792-
this.updateData(this.getSchema(), this.getSchema(e.target.value));
793-
this.setState({
794-
option: e.target.value
795-
});
808+
handleOptionChange = (e, selectedOption) => {
809+
this.updateData(this.getSchema(selectedOption), this.getSchema(e.target.value));
810+
// Uncomment when caching is implemented
811+
//
812+
// this.setState({
813+
// option: e.target.value
814+
// });
796815
}
797816

798817
updateData(oldSchema, newSchema) {
@@ -847,7 +866,17 @@ class OneOf extends React.Component {
847866
}
848867

849868
render() {
850-
let schema = this.getSchema();
869+
/* Perfomance note:
870+
*
871+
* In order to resolve https://github.com/bhch/react-json-form/issues/67,
872+
* we will not cache the selected option. Instead, we'll recalculate the
873+
* selected option on every render.
874+
*
875+
* If there're serious performance issues, we'll reconsider caching.
876+
*/
877+
let selectedOption = this.findSelectedOption();
878+
879+
let schema = this.getSchema(selectedOption);
851880
let type = getSchemaType(schema);
852881
let args = this.props.nextArgs ? this.props.nextArgs : this.props.parentArgs;
853882
let rowFunc;
@@ -881,9 +910,9 @@ class OneOf extends React.Component {
881910
<div className="rjf-form-group rjf-oneof-group">
882911
<div className="rjf-oneof-selector">
883912
<FormSelectInput
884-
value={this.state.option}
913+
value={selectedOption}
885914
options={this.getOptions()}
886-
onChange={this.handleChange}
915+
onChange={(e) => this.handleOptionChange(e, selectedOption)}
887916
className="rjf-oneof-selector-input"
888917
label={selectorLabel}
889918
/>

0 commit comments

Comments
 (0)