@@ -74,7 +74,19 @@ type State = {
7474 hasInlineFilter ?: boolean
7575
7676 /** 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+ }
7890}
7991
8092/**
@@ -91,20 +103,20 @@ export default class AskUI extends React.PureComponent<Props, State> {
91103 public static getDerivedStateFromProps ( props : Props , state : State ) {
92104 if ( state . userSelection && props . ask . prompt . choices . find ( ( _ ) => _ . name === state . userSelection ) ) {
93105 return state
94- } else if ( state . form ) {
106+ } else if ( state . form && state . form . ask === props . ask ) {
95107 // there has been an update to the form, nothing to do here
96108 return state
97109 } else {
98110 const suggested = props . ask . prompt . choices . find ( ( _ ) => ( _ as any ) [ "isSuggested" ] )
99- const form =
111+ const state =
100112 ! props . ask || ! Prompts . isForm ( props . ask . prompt )
101113 ? undefined
102114 : props . ask . prompt . choices . reduce ( ( M , _ ) => {
103115 M [ _ . name ] = ( _ as any ) [ "initial" ]
104116 return M
105117 } , { } as Record < string , string > )
106118 return {
107- form,
119+ form : { ask : props . ask , state } ,
108120 userSelection : ! suggested ? undefined : suggested . name ,
109121 }
110122 }
@@ -186,7 +198,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
186198 private readonly _onFormSubmit = ( evt : React . SyntheticEvent ) => {
187199 if ( this . props . ask && this . state . form ) {
188200 evt . preventDefault ( )
189- this . props . ask . onChoose ( Promise . resolve ( this . state . form ) )
201+ this . props . ask . onChoose ( Promise . resolve ( this . state . form . state ) )
190202 }
191203 return false
192204 }
@@ -355,7 +367,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
355367 aria-label = { `text-input-${ _ . name } ` }
356368 data-name = { _ . name }
357369 isRequired
358- value = { form [ _ . name ] }
370+ value = { form . state [ _ . name ] }
359371 onChange = { this . _onFormChange }
360372 />
361373 </ FormGroup >
0 commit comments