Skip to content

Commit

Permalink
fix: second of back-to-back forms does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
starpit committed Nov 30, 2022
1 parent ddcd9d8 commit 3b9a4d3
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions plugins/plugin-codeflare/src/components/Ask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,19 @@ type State = {
hasInlineFilter?: boolean

/** Current form state (if any) */
form?: Record<string, string>
form?: {
/**
* The question being asked by this form; so we can noticed when
* the question changes for back-to-back forms.
*/
ask: Ask

/**
* The current set of answers provided by the user, and
* initialized by the guidebook's `initial` value for each key.
*/
state: Record<string, string>
}
}

/**
Expand All @@ -91,20 +103,20 @@ export default class AskUI extends React.PureComponent<Props, State> {
public static getDerivedStateFromProps(props: Props, state: State) {
if (state.userSelection && props.ask.prompt.choices.find((_) => _.name === state.userSelection)) {
return state
} else if (state.form) {
} else if (state.form && state.form.ask === props.ask) {
// there has been an update to the form, nothing to do here
return state
} else {
const suggested = props.ask.prompt.choices.find((_) => (_ as any)["isSuggested"])
const form =
const state =
!props.ask || !Prompts.isForm(props.ask.prompt)
? undefined
: props.ask.prompt.choices.reduce((M, _) => {
M[_.name] = (_ as any)["initial"]
return M
}, {} as Record<string, string>)
return {
form,
form: { ask: props.ask, state },
userSelection: !suggested ? undefined : suggested.name,
}
}
Expand Down Expand Up @@ -186,7 +198,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
private readonly _onFormSubmit = (evt: React.SyntheticEvent) => {
if (this.props.ask && this.state.form) {
evt.preventDefault()
this.props.ask.onChoose(Promise.resolve(this.state.form))
this.props.ask.onChoose(Promise.resolve(this.state.form.state))
}
return false
}
Expand Down Expand Up @@ -355,7 +367,7 @@ export default class AskUI extends React.PureComponent<Props, State> {
aria-label={`text-input-${_.name}`}
data-name={_.name}
isRequired
value={form[_.name]}
value={form.state[_.name]}
onChange={this._onFormChange}
/>
</FormGroup>
Expand Down

0 comments on commit 3b9a4d3

Please sign in to comment.