@@ -2832,27 +2832,36 @@ class OneOfTopLevel extends React__default["default"].Component {
28322832 return schema ;
28332833 } ;
28342834
2835- this . handleChange = e => {
2836- this . updateData ( this . getSchema ( ) , this . getSchema ( e . target . value ) ) ;
2837- this . setState ( {
2838- option : e . target . value
2839- } ) ;
2835+ this . handleOptionChange = e => {
2836+ this . updateData ( this . getSchema ( e . target . value ) ) ; // Uncomment when caching is reimplemented
2837+ //
2838+ // this.setState({
2839+ // option: e.target.value
2840+ // });
28402841 } ;
28412842
2842- this . schemaName = this . props . schemaName || 'oneOf' ;
2843- this . state = {
2844- option : this . findSelectedOption ( )
2845- } ;
2843+ this . schemaName = this . props . schemaName || 'oneOf' ; // Uncomment when caching is implemented
2844+ //
2845+ // this.state = {
2846+ // option: this.findSelectedOption(),
2847+ // };
28462848 }
28472849
2848- updateData ( oldSchema , newSchema ) {
2849- getSchemaType ( oldSchema ) ;
2850- getSchemaType ( newSchema ) ;
2850+ updateData ( newSchema ) {
28512851 this . props . args . onChange ( this . props . args . name , getBlankData ( newSchema , this . props . args . getRef ) ) ;
28522852 }
28532853
28542854 render ( ) {
2855- let schema = this . getSchema ( ) ;
2855+ /* Perfomance note:
2856+ *
2857+ * In order to resolve https://github.com/bhch/react-json-form/issues/67,
2858+ * we will not cache the selected option. Instead, we'll recalculate the
2859+ * selected option on every render.
2860+ *
2861+ * If there're serious performance issues, we'll reconsider caching.
2862+ */
2863+ let selectedOption = this . findSelectedOption ( ) ;
2864+ let schema = this . getSchema ( selectedOption ) ;
28562865 let type = getSchemaType ( schema ) ;
28572866 let args = this . props . args ;
28582867 let rowFunc ;
@@ -2878,9 +2887,9 @@ class OneOfTopLevel extends React__default["default"].Component {
28782887 } , /*#__PURE__*/ React__default [ "default" ] . createElement ( "div" , {
28792888 className : "rjf-oneof-selector"
28802889 } , /*#__PURE__*/ React__default [ "default" ] . createElement ( FormSelectInput , {
2881- value : this . state . option ,
2890+ value : selectedOption ,
28822891 options : this . getOptions ( ) ,
2883- onChange : this . handleChange ,
2892+ onChange : this . handleOptionChange ,
28842893 className : "rjf-oneof-selector-input" ,
28852894 label : selectorLabel
28862895 } ) ) , rows ) ;
@@ -3025,30 +3034,35 @@ class OneOf extends React__default["default"].Component {
30253034 return getSchemaType ( this . props . parentArgs . schema ) ;
30263035 } ;
30273036
3028- this . handleChange = e => {
3029- this . updateData ( this . getSchema ( ) , this . getSchema ( e . target . value ) ) ;
3030- this . setState ( {
3031- option : e . target . value
3032- } ) ;
3037+ this . handleOptionChange = ( e , selectedOption ) => {
3038+ this . updateData ( this . getSchema ( selectedOption ) , this . getSchema ( e . target . value ) ) ; // Uncomment when caching is implemented
3039+ //
3040+ // this.setState({
3041+ // option: e.target.value
3042+ // });
30333043 } ;
30343044
3035- this . schemaName = this . props . schemaName || 'oneOf' ;
3036- this . state = {
3037- option : this . findSelectedOption ( )
3038- } ;
3045+ this . schemaName = this . props . schemaName || 'oneOf' ; // Uncomment when caching is implemented
3046+ //
3047+ // this.state = {
3048+ // option: this.findSelectedOption(),
3049+ // };
30393050 }
3040-
3041- componentDidUpdate ( prevProps , prevState ) {
3042- if ( prevProps . nextArgs || this . props . nextArgs ) {
3043- let prevDataType = 'string' ;
3044- let newDataType = 'string' ;
3045- if ( prevProps . nextArgs ) prevDataType = actualType ( prevProps . nextArgs . data ) ;
3046- if ( this . props . nextArgs ) newDataType = actualType ( this . props . nextArgs . data ) ;
3047- if ( prevDataType !== newDataType ) this . setState ( {
3048- option : this . findSelectedOption ( )
3049- } ) ;
3050- }
3051+ /* Uncomment when caching is implemente
3052+ componentDidUpdate(prevProps, prevState) {
3053+ if (prevProps.nextArgs || this.props.nextArgs) {
3054+ let prevDataType = 'string';
3055+ let newDataType = 'string';
3056+ if (prevProps.nextArgs)
3057+ prevDataType = actualType(prevProps.nextArgs.data);
3058+ if (this.props.nextArgs)
3059+ newDataType = actualType(this.props.nextArgs.data);
3060+ if (prevDataType !== newDataType)
3061+ this.setState({option: this.findSelectedOption()});
3062+ }
30513063 }
3064+ */
3065+
30523066
30533067 updateData ( oldSchema , newSchema ) {
30543068 let parentType = this . getParentType ( ) ;
@@ -3090,7 +3104,16 @@ class OneOf extends React__default["default"].Component {
30903104 }
30913105
30923106 render ( ) {
3093- let schema = this . getSchema ( ) ;
3107+ /* Perfomance note:
3108+ *
3109+ * In order to resolve https://github.com/bhch/react-json-form/issues/67,
3110+ * we will not cache the selected option. Instead, we'll recalculate the
3111+ * selected option on every render.
3112+ *
3113+ * If there're serious performance issues, we'll reconsider caching.
3114+ */
3115+ let selectedOption = this . findSelectedOption ( ) ;
3116+ let schema = this . getSchema ( selectedOption ) ;
30943117 let type = getSchemaType ( schema ) ;
30953118 let args = this . props . nextArgs ? this . props . nextArgs : this . props . parentArgs ;
30963119 let rowFunc ;
@@ -3119,9 +3142,9 @@ class OneOf extends React__default["default"].Component {
31193142 } , /*#__PURE__*/ React__default [ "default" ] . createElement ( "div" , {
31203143 className : "rjf-oneof-selector"
31213144 } , /*#__PURE__*/ React__default [ "default" ] . createElement ( FormSelectInput , {
3122- value : this . state . option ,
3145+ value : selectedOption ,
31233146 options : this . getOptions ( ) ,
3124- onChange : this . handleChange ,
3147+ onChange : e => this . handleOptionChange ( e , selectedOption ) ,
31253148 className : "rjf-oneof-selector-input" ,
31263149 label : selectorLabel
31273150 } ) ) , rows ) ;
0 commit comments