UNPKG

8.82 kBJavaScriptView Raw
1/*
2 * Copyright 2020 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __extends, __rest } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { AbstractPureComponent, Classes, Utils } from "../../common";
20import { DISPLAYNAME_PREFIX } from "../../common/props";
21import { clickElementOnKeyPress } from "../../common/utils";
22import { Dialog } from "./dialog";
23import { DialogFooter } from "./dialogFooter";
24import { DialogStep } from "./dialogStep";
25import { DialogStepButton } from "./dialogStepButton";
26var PADDING_BOTTOM = 0;
27var MIN_WIDTH = 800;
28/**
29 * Multi-step dialog component.
30 *
31 * @see https://blueprintjs.com/docs/#core/components/dialog.multistep-dialog
32 */
33var MultistepDialog = /** @class */ (function (_super) {
34 __extends(MultistepDialog, _super);
35 function MultistepDialog() {
36 var _this = _super !== null && _super.apply(this, arguments) || this;
37 _this.state = _this.getInitialIndexFromProps(_this.props);
38 _this.renderDialogStep = function (step, index) {
39 var _a;
40 var stepNumber = index + 1;
41 var hasBeenViewed = _this.state.lastViewedIndex >= index;
42 var currentlySelected = _this.state.selectedIndex === index;
43 var handleClickDialogStep = index > _this.state.lastViewedIndex ? undefined : _this.getDialogStepChangeHandler(index);
44 return (React.createElement("div", { className: classNames(Classes.DIALOG_STEP_CONTAINER, (_a = {},
45 _a[Classes.ACTIVE] = currentlySelected,
46 _a[Classes.DIALOG_STEP_VIEWED] = hasBeenViewed,
47 _a)), key: index, "aria-selected": currentlySelected, role: "tab" },
48 React.createElement("div", { className: Classes.DIALOG_STEP, onClick: handleClickDialogStep, tabIndex: handleClickDialogStep ? 0 : -1,
49 // enable enter key to take effect on the div as if it were a button
50 onKeyDown: clickElementOnKeyPress(["Enter", " "]) },
51 React.createElement("div", { className: Classes.DIALOG_STEP_ICON }, stepNumber),
52 React.createElement("div", { className: Classes.DIALOG_STEP_TITLE }, step.props.title))));
53 };
54 return _this;
55 }
56 MultistepDialog.prototype.render = function () {
57 var _a;
58 var _b = this.props, className = _b.className, navigationPosition = _b.navigationPosition, showCloseButtonInFooter = _b.showCloseButtonInFooter, isCloseButtonShown = _b.isCloseButtonShown, otherProps = __rest(_b, ["className", "navigationPosition", "showCloseButtonInFooter", "isCloseButtonShown"]);
59 return (React.createElement(Dialog, __assign({ isCloseButtonShown: isCloseButtonShown }, otherProps, { className: classNames((_a = {},
60 _a[Classes.MULTISTEP_DIALOG_NAV_RIGHT] = navigationPosition === "right",
61 _a[Classes.MULTISTEP_DIALOG_NAV_TOP] = navigationPosition === "top",
62 _a), className), style: this.getDialogStyle() }),
63 React.createElement("div", { className: Classes.MULTISTEP_DIALOG_PANELS },
64 this.renderLeftPanel(),
65 this.maybeRenderRightPanel())));
66 };
67 MultistepDialog.prototype.componentDidUpdate = function (prevProps) {
68 if ((prevProps.resetOnClose || prevProps.initialStepIndex !== this.props.initialStepIndex) &&
69 !prevProps.isOpen &&
70 this.props.isOpen) {
71 this.setState(this.getInitialIndexFromProps(this.props));
72 }
73 };
74 MultistepDialog.prototype.getDialogStyle = function () {
75 return __assign({ minWidth: MIN_WIDTH, paddingBottom: PADDING_BOTTOM }, this.props.style);
76 };
77 MultistepDialog.prototype.renderLeftPanel = function () {
78 return (React.createElement("div", { className: Classes.MULTISTEP_DIALOG_LEFT_PANEL, role: "tablist", "aria-label": "steps" }, this.getDialogStepChildren().filter(isDialogStepElement).map(this.renderDialogStep)));
79 };
80 MultistepDialog.prototype.maybeRenderRightPanel = function () {
81 var steps = this.getDialogStepChildren();
82 if (steps.length <= this.state.selectedIndex) {
83 return null;
84 }
85 var _a = steps[this.state.selectedIndex].props, className = _a.className, panel = _a.panel, panelClassName = _a.panelClassName;
86 return (React.createElement("div", { className: classNames(Classes.MULTISTEP_DIALOG_RIGHT_PANEL, className, panelClassName) },
87 panel,
88 this.renderFooter()));
89 };
90 MultistepDialog.prototype.renderFooter = function () {
91 var _a = this.props, closeButtonProps = _a.closeButtonProps, showCloseButtonInFooter = _a.showCloseButtonInFooter, onClose = _a.onClose;
92 var maybeCloseButton = !showCloseButtonInFooter ? undefined : (React.createElement(DialogStepButton, __assign({ text: "Close", onClick: onClose }, closeButtonProps)));
93 return React.createElement(DialogFooter, { actions: this.renderButtons() }, maybeCloseButton);
94 };
95 MultistepDialog.prototype.renderButtons = function () {
96 var _a, _b;
97 var selectedIndex = this.state.selectedIndex;
98 var steps = this.getDialogStepChildren();
99 var buttons = [];
100 if (this.state.selectedIndex > 0) {
101 var backButtonProps = (_a = steps[selectedIndex].props.backButtonProps) !== null && _a !== void 0 ? _a : this.props.backButtonProps;
102 buttons.push(React.createElement(DialogStepButton, __assign({ key: "back", onClick: this.getDialogStepChangeHandler(selectedIndex - 1), text: "Back" }, backButtonProps)));
103 }
104 if (selectedIndex === this.getDialogStepChildren().length - 1) {
105 buttons.push(React.createElement(DialogStepButton, __assign({ intent: "primary", key: "final", text: "Submit" }, this.props.finalButtonProps)));
106 }
107 else {
108 var nextButtonProps = (_b = steps[selectedIndex].props.nextButtonProps) !== null && _b !== void 0 ? _b : this.props.nextButtonProps;
109 buttons.push(React.createElement(DialogStepButton, __assign({ intent: "primary", key: "next", onClick: this.getDialogStepChangeHandler(selectedIndex + 1), text: "Next" }, nextButtonProps)));
110 }
111 return buttons;
112 };
113 MultistepDialog.prototype.getDialogStepChangeHandler = function (index) {
114 var _this = this;
115 return function (event) {
116 if (_this.props.onChange !== undefined) {
117 var steps = _this.getDialogStepChildren();
118 var prevStepId = steps[_this.state.selectedIndex].props.id;
119 var newStepId = steps[index].props.id;
120 _this.props.onChange(newStepId, prevStepId, event);
121 }
122 _this.setState({
123 lastViewedIndex: Math.max(_this.state.lastViewedIndex, index),
124 selectedIndex: index,
125 });
126 };
127 };
128 /** Filters children to only `<DialogStep>`s */
129 MultistepDialog.prototype.getDialogStepChildren = function (props) {
130 if (props === void 0) { props = this.props; }
131 return React.Children.toArray(props.children).filter(isDialogStepElement);
132 };
133 MultistepDialog.prototype.getInitialIndexFromProps = function (props) {
134 if (props.initialStepIndex !== undefined) {
135 var boundedInitialIndex = Math.max(0, Math.min(props.initialStepIndex, this.getDialogStepChildren(props).length - 1));
136 return {
137 lastViewedIndex: boundedInitialIndex,
138 selectedIndex: boundedInitialIndex,
139 };
140 }
141 else {
142 return {
143 lastViewedIndex: 0,
144 selectedIndex: 0,
145 };
146 }
147 };
148 MultistepDialog.displayName = "".concat(DISPLAYNAME_PREFIX, ".MultistepDialog");
149 MultistepDialog.defaultProps = {
150 canOutsideClickClose: true,
151 isOpen: false,
152 navigationPosition: "left",
153 resetOnClose: true,
154 showCloseButtonInFooter: false,
155 };
156 return MultistepDialog;
157}(AbstractPureComponent));
158export { MultistepDialog };
159function isDialogStepElement(child) {
160 return Utils.isElementOfType(child, DialogStep);
161}
162//# sourceMappingURL=multistepDialog.js.map
\No newline at end of file