@@ -62,6 +62,15 @@ export class ParentControl extends React.Component {
62
62
this . props . onChange ( newPath ) ;
63
63
}
64
64
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
+
65
74
getValue ( ) {
66
75
return this . props . value || '' ;
67
76
}
@@ -107,18 +116,8 @@ export class ParentControl extends React.Component {
107
116
}
108
117
}
109
118
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 ;
122
121
const fullPath = this . getFullPath ( ) ;
123
122
const parentPath = this . getParent ( fullPath ) || '' ;
124
123
const parent = hits . find ( ( e ) => this . getPath ( e . path ) === parentPath ) ;
@@ -133,6 +132,22 @@ export class ParentControl extends React.Component {
133
132
} ,
134
133
] ;
135
134
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 ) ;
136
151
this . setState ( {
137
152
optionsLoaded : true ,
138
153
options,
0 commit comments