UNPKG

6.38 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2018 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.PanelStack = void 0;
19var tslib_1 = require("tslib");
20var classnames_1 = tslib_1.__importDefault(require("classnames"));
21var React = tslib_1.__importStar(require("react"));
22var react_transition_group_1 = require("react-transition-group");
23var common_1 = require("../../common");
24var Errors = tslib_1.__importStar(require("../../common/errors"));
25var panelView_1 = require("./panelView");
26/**
27 * Panel stack component.
28 *
29 * @see https://blueprintjs.com/docs/#core/components/panel-stack
30 * @deprecated use `PanelStack2<T>`
31 */
32var PanelStack = /** @class */ (function (_super) {
33 tslib_1.__extends(PanelStack, _super);
34 function PanelStack() {
35 var _this = _super !== null && _super.apply(this, arguments) || this;
36 _this.state = {
37 direction: "push",
38 stack: _this.props.stack != null
39 ? _this.props.stack.slice().reverse()
40 : _this.props.initialPanel !== undefined
41 ? [_this.props.initialPanel]
42 : [],
43 };
44 _this.renderPanel = function (panel, index) {
45 var _a = _this.props, renderActivePanelOnly = _a.renderActivePanelOnly, _b = _a.showPanelHeader, showPanelHeader = _b === void 0 ? true : _b;
46 var stack = _this.state.stack;
47 // With renderActivePanelOnly={false} we would keep all the CSSTransitions rendered,
48 // therefore they would not trigger the "enter" transition event as they were entered.
49 // To force the enter event, we want to change the key, but stack.length is not enough
50 // and a single panel should not rerender as long as it's hidden.
51 // This key contains two parts: first one, stack.length - index is constant (and unique) for each panel,
52 // second one, active changes only when the panel becomes or stops being active.
53 var layer = stack.length - index;
54 var key = renderActivePanelOnly ? stack.length : layer;
55 return (React.createElement(react_transition_group_1.CSSTransition, { classNames: common_1.Classes.PANEL_STACK, key: key, timeout: 400 },
56 React.createElement(panelView_1.PanelView, { onClose: _this.handlePanelClose, onOpen: _this.handlePanelOpen, panel: panel, previousPanel: stack[index + 1], showHeader: showPanelHeader })));
57 };
58 _this.handlePanelClose = function (panel) {
59 var _a, _b;
60 var stack = _this.state.stack;
61 // only remove this panel if it is at the top and not the only one.
62 if (stack[0] !== panel || stack.length <= 1) {
63 return;
64 }
65 (_b = (_a = _this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, panel);
66 if (_this.props.stack == null) {
67 _this.setState(function (state) { return ({
68 direction: "pop",
69 stack: state.stack.slice(1),
70 }); });
71 }
72 };
73 _this.handlePanelOpen = function (panel) {
74 var _a, _b;
75 (_b = (_a = _this.props).onOpen) === null || _b === void 0 ? void 0 : _b.call(_a, panel);
76 if (_this.props.stack == null) {
77 _this.setState(function (state) { return ({
78 direction: "push",
79 stack: tslib_1.__spreadArray([panel], state.stack, true),
80 }); });
81 }
82 };
83 return _this;
84 }
85 PanelStack.prototype.componentDidUpdate = function (prevProps, prevState) {
86 _super.prototype.componentDidUpdate.call(this, prevProps, prevState);
87 // Always update local stack if stack prop changes
88 if (this.props.stack !== prevProps.stack && prevProps.stack != null) {
89 this.setState({ stack: this.props.stack.slice().reverse() });
90 }
91 // Only update animation direction if stack length changes
92 var stackLength = this.props.stack != null ? this.props.stack.length : 0;
93 var prevStackLength = prevProps.stack != null ? prevProps.stack.length : 0;
94 if (stackLength !== prevStackLength && prevProps.stack != null) {
95 this.setState({
96 direction: prevProps.stack.length - this.props.stack.length < 0 ? "push" : "pop",
97 });
98 }
99 };
100 PanelStack.prototype.render = function () {
101 var classes = (0, classnames_1.default)(common_1.Classes.PANEL_STACK, "".concat(common_1.Classes.PANEL_STACK, "-").concat(this.state.direction), this.props.className);
102 return (React.createElement(react_transition_group_1.TransitionGroup, { className: classes, component: "div" }, this.renderPanels()));
103 };
104 PanelStack.prototype.validateProps = function (props) {
105 if ((props.initialPanel == null && props.stack == null) ||
106 (props.initialPanel != null && props.stack != null)) {
107 console.error(Errors.PANEL_STACK_INITIAL_PANEL_STACK_MUTEX);
108 }
109 if (props.stack != null && props.stack.length === 0) {
110 console.error(Errors.PANEL_STACK_REQUIRES_PANEL);
111 }
112 };
113 PanelStack.prototype.renderPanels = function () {
114 var _a = this.props.renderActivePanelOnly, renderActivePanelOnly = _a === void 0 ? true : _a;
115 var stack = this.state.stack;
116 if (stack.length === 0) {
117 return null;
118 }
119 var panelsToRender = renderActivePanelOnly ? [stack[0]] : stack;
120 var panelViews = panelsToRender.map(this.renderPanel).reverse();
121 return panelViews;
122 };
123 return PanelStack;
124}(common_1.AbstractPureComponent2));
125exports.PanelStack = PanelStack;
126//# sourceMappingURL=panelStack.js.map
\No newline at end of file