Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] feat(MultiStepDialog): backButtonProps #4567

Merged
merged 1 commit into from
Mar 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions packages/core/src/components/dialog/multistepDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import { DialogStep, DialogStepId, IDialogStepProps } from "./dialogStep";
type DialogStepElement = React.ReactElement<IDialogStepProps & { children: React.ReactNode }>;

export interface IMultistepDialogProps extends IDialogProps {
/**
* Props for the back button.
*/
backButtonProps?: Partial<Pick<IButtonProps, "disabled" | "text">>;

/**
* Props for the button to display on the final step.
*/
Expand Down Expand Up @@ -158,19 +163,27 @@ export class MultistepDialog extends AbstractPureComponent2<IMultistepDialogProp
}

private renderButtons() {
const { selectedIndex } = this.state;
const buttons = [];
if (this.state.selectedIndex > 0) {
buttons.push(<Button key="back" onClick={this.getBackClickHandler()} text="Back" />);
buttons.push(
<Button
key="back"
onClick={this.getDialogStepChangeHandler(selectedIndex - 1)}
text="Back"
{...this.props.backButtonProps}
/>,
);
}

if (this.state.selectedIndex === this.getDialogStepChildren().length - 1) {
buttons.push(this.renderFinalButton());
if (selectedIndex === this.getDialogStepChildren().length - 1) {
buttons.push(<Button intent="primary" key="final" text="Submit" {...this.props.finalButtonProps} />);
} else {
buttons.push(
<Button
intent="primary"
key="next"
onClick={this.getNextClickHandler()}
onClick={this.getDialogStepChangeHandler(selectedIndex + 1)}
text="Next"
{...this.props.nextButtonProps}
/>,
Expand All @@ -180,14 +193,6 @@ export class MultistepDialog extends AbstractPureComponent2<IMultistepDialogProp
return buttons;
}

private getBackClickHandler() {
return this.getDialogStepChangeHandler(this.state.selectedIndex - 1);
}

private getNextClickHandler() {
return this.getDialogStepChangeHandler(this.state.selectedIndex + 1);
}

private getDialogStepChangeHandler(index: number) {
return (event: React.MouseEvent<HTMLElement>) => {
if (this.props.onChange !== undefined) {
Expand All @@ -203,10 +208,6 @@ export class MultistepDialog extends AbstractPureComponent2<IMultistepDialogProp
};
}

private renderFinalButton() {
return <Button intent="primary" key="final" text="Submit" {...this.props.finalButtonProps} />;
}

/** Filters children to only `<DialogStep>`s */
private getDialogStepChildren(props: IMultistepDialogProps & { children?: React.ReactNode } = this.props) {
return React.Children.toArray(props.children).filter(isDialogStepElement);
Expand Down