@@ -62,6 +62,15 @@ export class ParentControl extends React.Component {
6262 this . props . onChange ( newPath ) ;
6363 }
6464
65+ componentDidUpdate ( prevProps ) {
66+ if ( prevProps . value !== this . props . value ) {
67+ // update options with the new parent
68+ this . setState ( {
69+ options : this . getOptions ( this . state . options [ 0 ] . entries ) ,
70+ } ) ;
71+ }
72+ }
73+
6574 getValue ( ) {
6675 return this . props . value || '' ;
6776 }
@@ -107,18 +116,8 @@ export class ParentControl extends React.Component {
107116 }
108117 }
109118
110- async loadOptions ( ) {
111- if ( this . state . optionsLoaded ) {
112- return this . state . options ;
113- }
114- const { forID, query, collection } = this . props ;
115- const collectionName = collection . get ( 'name' ) ;
116- const {
117- payload : {
118- response : { hits = [ ] } ,
119- } ,
120- } = await query ( forID , collectionName , [ 'path' ] , '' ) ;
121-
119+ getOptions ( hits ) {
120+ const { collection } = this . props ;
122121 const fullPath = this . getFullPath ( ) ;
123122 const parentPath = this . getParent ( fullPath ) || '' ;
124123 const parent = hits . find ( ( e ) => this . getPath ( e . path ) === parentPath ) ;
@@ -133,6 +132,22 @@ export class ParentControl extends React.Component {
133132 } ,
134133 ] ;
135134
135+ return options ;
136+ }
137+
138+ async loadOptions ( ) {
139+ if ( this . state . optionsLoaded ) {
140+ return this . state . options ;
141+ }
142+ const { forID, query, collection } = this . props ;
143+ const collectionName = collection . get ( 'name' ) ;
144+ const {
145+ payload : {
146+ response : { hits = [ ] } ,
147+ } ,
148+ } = await query ( forID , collectionName , [ 'path' ] , '' ) ;
149+
150+ const options = this . getOptions ( hits ) ;
136151 this . setState ( {
137152 optionsLoaded : true ,
138153 options,
0 commit comments