@blueprintjs/core
Version:
Core styles & components
149 lines • 7.44 kB
JavaScript
/*
* Copyright 2020 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import classNames from "classnames";
import * as React from "react";
import { AbstractPureComponent, Classes, Utils } from "../../common";
import { DISPLAYNAME_PREFIX } from "../../common/props";
import { clickElementOnKeyPress } from "../../common/utils";
import { Dialog } from "./dialog";
import { DialogFooter } from "./dialogFooter";
import { DialogStep } from "./dialogStep";
import { DialogStepButton } from "./dialogStepButton";
const PADDING_BOTTOM = 0;
const MIN_WIDTH = 800;
/**
* Multi-step dialog component.
*
* @see https://blueprintjs.com/docs/#core/components/dialog.multistep-dialog
*/
export class MultistepDialog extends AbstractPureComponent {
static displayName = `${DISPLAYNAME_PREFIX}.MultistepDialog`;
static defaultProps = {
canOutsideClickClose: true,
isOpen: false,
navigationPosition: "left",
resetOnClose: true,
showCloseButtonInFooter: false,
};
state = this.getInitialIndexFromProps(this.props);
render() {
const { className, navigationPosition, showCloseButtonInFooter, isCloseButtonShown, ...otherProps } = this.props;
return (React.createElement(Dialog, { isCloseButtonShown: isCloseButtonShown, ...otherProps, className: classNames({
[Classes.MULTISTEP_DIALOG_NAV_RIGHT]: navigationPosition === "right",
[Classes.MULTISTEP_DIALOG_NAV_TOP]: navigationPosition === "top",
}, className), style: this.getDialogStyle() },
React.createElement("div", { className: Classes.MULTISTEP_DIALOG_PANELS },
this.renderLeftPanel(),
this.maybeRenderRightPanel())));
}
componentDidUpdate(prevProps) {
if ((prevProps.resetOnClose || prevProps.initialStepIndex !== this.props.initialStepIndex) &&
!prevProps.isOpen &&
this.props.isOpen) {
this.setState(this.getInitialIndexFromProps(this.props));
}
}
getDialogStyle() {
return { minWidth: MIN_WIDTH, paddingBottom: PADDING_BOTTOM, ...this.props.style };
}
renderLeftPanel() {
return (React.createElement("div", { className: Classes.MULTISTEP_DIALOG_LEFT_PANEL, role: "tablist", "aria-label": "steps" }, this.getDialogStepChildren().filter(isDialogStepElement).map(this.renderDialogStep)));
}
renderDialogStep = (step, index) => {
const stepNumber = index + 1;
const hasBeenViewed = this.state.lastViewedIndex >= index;
const currentlySelected = this.state.selectedIndex === index;
const handleClickDialogStep = index > this.state.lastViewedIndex ? undefined : this.getDialogStepChangeHandler(index);
return (React.createElement("div", { className: classNames(Classes.DIALOG_STEP_CONTAINER, {
[Classes.ACTIVE]: currentlySelected,
[Classes.DIALOG_STEP_VIEWED]: hasBeenViewed,
}), key: index, "aria-selected": currentlySelected, role: "tab" },
React.createElement("div", { className: Classes.DIALOG_STEP, onClick: handleClickDialogStep, tabIndex: handleClickDialogStep ? 0 : -1,
// enable enter key to take effect on the div as if it were a button
onKeyDown: clickElementOnKeyPress(["Enter", " "]) },
React.createElement("div", { className: Classes.DIALOG_STEP_ICON }, stepNumber),
React.createElement("div", { className: Classes.DIALOG_STEP_TITLE }, step.props.title))));
};
maybeRenderRightPanel() {
const steps = this.getDialogStepChildren();
if (steps.length <= this.state.selectedIndex) {
return null;
}
const { className, panel, panelClassName } = steps[this.state.selectedIndex].props;
return (React.createElement("div", { className: classNames(Classes.MULTISTEP_DIALOG_RIGHT_PANEL, className, panelClassName) },
panel,
this.renderFooter()));
}
renderFooter() {
const { closeButtonProps, showCloseButtonInFooter, onClose } = this.props;
const maybeCloseButton = !showCloseButtonInFooter ? undefined : (React.createElement(DialogStepButton, { text: "Close", onClick: onClose, ...closeButtonProps }));
return React.createElement(DialogFooter, { actions: this.renderButtons() }, maybeCloseButton);
}
renderButtons() {
const { selectedIndex } = this.state;
const steps = this.getDialogStepChildren();
const buttons = [];
if (this.state.selectedIndex > 0) {
const backButtonProps = steps[selectedIndex].props.backButtonProps ?? this.props.backButtonProps;
buttons.push(React.createElement(DialogStepButton, { key: "back", onClick: this.getDialogStepChangeHandler(selectedIndex - 1), text: "Back", ...backButtonProps }));
}
if (selectedIndex === this.getDialogStepChildren().length - 1) {
buttons.push(React.createElement(DialogStepButton, { intent: "primary", key: "final", text: "Submit", ...this.props.finalButtonProps }));
}
else {
const nextButtonProps = steps[selectedIndex].props.nextButtonProps ?? this.props.nextButtonProps;
buttons.push(React.createElement(DialogStepButton, { intent: "primary", key: "next", onClick: this.getDialogStepChangeHandler(selectedIndex + 1), text: "Next", ...nextButtonProps }));
}
return buttons;
}
getDialogStepChangeHandler(index) {
return (event) => {
if (this.props.onChange !== undefined) {
const steps = this.getDialogStepChildren();
const prevStepId = steps[this.state.selectedIndex].props.id;
const newStepId = steps[index].props.id;
this.props.onChange(newStepId, prevStepId, event);
}
this.setState({
lastViewedIndex: Math.max(this.state.lastViewedIndex, index),
selectedIndex: index,
});
};
}
/** Filters children to only `<DialogStep>`s */
getDialogStepChildren(props = this.props) {
return React.Children.toArray(props.children).filter(isDialogStepElement);
}
getInitialIndexFromProps(props) {
if (props.initialStepIndex !== undefined) {
const boundedInitialIndex = Math.max(0, Math.min(props.initialStepIndex, this.getDialogStepChildren(props).length - 1));
return {
lastViewedIndex: boundedInitialIndex,
selectedIndex: boundedInitialIndex,
};
}
else {
return {
lastViewedIndex: 0,
selectedIndex: 0,
};
}
}
}
function isDialogStepElement(child) {
return Utils.isElementOfType(child, DialogStep);
}
//# sourceMappingURL=multistepDialog.js.map