@@ -74,7 +74,19 @@ type State = {
74
74
hasInlineFilter ?: boolean
75
75
76
76
/** Current form state (if any) */
77
- form ?: Record < string , string >
77
+ form ?: {
78
+ /**
79
+ * The question being asked by this form; so we can noticed when
80
+ * the question changes for back-to-back forms.
81
+ */
82
+ ask : Ask
83
+
84
+ /**
85
+ * The current set of answers provided by the user, and
86
+ * initialized by the guidebook's `initial` value for each key.
87
+ */
88
+ state : Record < string , string >
89
+ }
78
90
}
79
91
80
92
/**
@@ -91,20 +103,20 @@ export default class AskUI extends React.PureComponent<Props, State> {
91
103
public static getDerivedStateFromProps ( props : Props , state : State ) {
92
104
if ( state . userSelection && props . ask . prompt . choices . find ( ( _ ) => _ . name === state . userSelection ) ) {
93
105
return state
94
- } else if ( state . form ) {
106
+ } else if ( state . form && state . form . ask === props . ask ) {
95
107
// there has been an update to the form, nothing to do here
96
108
return state
97
109
} else {
98
110
const suggested = props . ask . prompt . choices . find ( ( _ ) => ( _ as any ) [ "isSuggested" ] )
99
- const form =
111
+ const state =
100
112
! props . ask || ! Prompts . isForm ( props . ask . prompt )
101
113
? undefined
102
114
: props . ask . prompt . choices . reduce ( ( M , _ ) => {
103
115
M [ _ . name ] = ( _ as any ) [ "initial" ]
104
116
return M
105
117
} , { } as Record < string , string > )
106
118
return {
107
- form,
119
+ form : { ask : props . ask , state } ,
108
120
userSelection : ! suggested ? undefined : suggested . name ,
109
121
}
110
122
}
@@ -186,7 +198,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
186
198
private readonly _onFormSubmit = ( evt : React . SyntheticEvent ) => {
187
199
if ( this . props . ask && this . state . form ) {
188
200
evt . preventDefault ( )
189
- this . props . ask . onChoose ( Promise . resolve ( this . state . form ) )
201
+ this . props . ask . onChoose ( Promise . resolve ( this . state . form . state ) )
190
202
}
191
203
return false
192
204
}
@@ -355,7 +367,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
355
367
aria-label = { `text-input-${ _ . name } ` }
356
368
data-name = { _ . name }
357
369
isRequired
358
- value = { form [ _ . name ] }
370
+ value = { form . state [ _ . name ] }
359
371
onChange = { this . _onFormChange }
360
372
/>
361
373
</ FormGroup >
0 commit comments