UNPKG

6.13 kBJavaScriptView Raw
1/*
2 * Copyright 2018 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 { __decorate, __extends, __spreadArrays } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { polyfill } from "react-lifecycles-compat";
20import { CSSTransition, TransitionGroup } from "react-transition-group";
21import { AbstractPureComponent2, Classes } from "../../common";
22import * as Errors from "../../common/errors";
23import { PanelView } from "./panelView";
24/** @deprecated use `PanelStack2<T>` */
25var PanelStack = /** @class */ (function (_super) {
26 __extends(PanelStack, _super);
27 function PanelStack() {
28 var _this = _super !== null && _super.apply(this, arguments) || this;
29 _this.state = {
30 direction: "push",
31 stack: _this.props.stack != null
32 ? _this.props.stack.slice().reverse()
33 : _this.props.initialPanel !== undefined
34 ? [_this.props.initialPanel]
35 : [],
36 };
37 _this.renderPanel = function (panel, index) {
38 var _a = _this.props, renderActivePanelOnly = _a.renderActivePanelOnly, _b = _a.showPanelHeader, showPanelHeader = _b === void 0 ? true : _b;
39 var stack = _this.state.stack;
40 // With renderActivePanelOnly={false} we would keep all the CSSTransitions rendered,
41 // therefore they would not trigger the "enter" transition event as they were entered.
42 // To force the enter event, we want to change the key, but stack.length is not enough
43 // and a single panel should not rerender as long as it's hidden.
44 // This key contains two parts: first one, stack.length - index is constant (and unique) for each panel,
45 // second one, active changes only when the panel becomes or stops being active.
46 var layer = stack.length - index;
47 var key = renderActivePanelOnly ? stack.length : layer;
48 return (React.createElement(CSSTransition, { classNames: Classes.PANEL_STACK, key: key, timeout: 400 },
49 React.createElement(PanelView, { onClose: _this.handlePanelClose, onOpen: _this.handlePanelOpen, panel: panel, previousPanel: stack[index + 1], showHeader: showPanelHeader })));
50 };
51 _this.handlePanelClose = function (panel) {
52 var _a, _b;
53 var stack = _this.state.stack;
54 // only remove this panel if it is at the top and not the only one.
55 if (stack[0] !== panel || stack.length <= 1) {
56 return;
57 }
58 (_b = (_a = _this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a, panel);
59 if (_this.props.stack == null) {
60 _this.setState(function (state) { return ({
61 direction: "pop",
62 stack: state.stack.slice(1),
63 }); });
64 }
65 };
66 _this.handlePanelOpen = function (panel) {
67 var _a, _b;
68 (_b = (_a = _this.props).onOpen) === null || _b === void 0 ? void 0 : _b.call(_a, panel);
69 if (_this.props.stack == null) {
70 _this.setState(function (state) { return ({
71 direction: "push",
72 stack: __spreadArrays([panel], state.stack),
73 }); });
74 }
75 };
76 return _this;
77 }
78 PanelStack.prototype.componentDidUpdate = function (prevProps, prevState) {
79 _super.prototype.componentDidUpdate.call(this, prevProps, prevState);
80 // Always update local stack if stack prop changes
81 if (this.props.stack !== prevProps.stack && prevProps.stack != null) {
82 this.setState({ stack: this.props.stack.slice().reverse() });
83 }
84 // Only update animation direction if stack length changes
85 var stackLength = this.props.stack != null ? this.props.stack.length : 0;
86 var prevStackLength = prevProps.stack != null ? prevProps.stack.length : 0;
87 if (stackLength !== prevStackLength && prevProps.stack != null) {
88 this.setState({
89 direction: prevProps.stack.length - this.props.stack.length < 0 ? "push" : "pop",
90 });
91 }
92 };
93 PanelStack.prototype.render = function () {
94 var classes = classNames(Classes.PANEL_STACK, Classes.PANEL_STACK + "-" + this.state.direction, this.props.className);
95 return (React.createElement(TransitionGroup, { className: classes, component: "div" }, this.renderPanels()));
96 };
97 PanelStack.prototype.validateProps = function (props) {
98 if ((props.initialPanel == null && props.stack == null) ||
99 (props.initialPanel != null && props.stack != null)) {
100 console.error(Errors.PANEL_STACK_INITIAL_PANEL_STACK_MUTEX);
101 }
102 if (props.stack != null && props.stack.length === 0) {
103 console.error(Errors.PANEL_STACK_REQUIRES_PANEL);
104 }
105 };
106 PanelStack.prototype.renderPanels = function () {
107 var _a = this.props.renderActivePanelOnly, renderActivePanelOnly = _a === void 0 ? true : _a;
108 var stack = this.state.stack;
109 if (stack.length === 0) {
110 return null;
111 }
112 var panelsToRender = renderActivePanelOnly ? [stack[0]] : stack;
113 var panelViews = panelsToRender.map(this.renderPanel).reverse();
114 return panelViews;
115 };
116 PanelStack = __decorate([
117 polyfill
118 ], PanelStack);
119 return PanelStack;
120}(AbstractPureComponent2));
121export { PanelStack };
122//# sourceMappingURL=panelStack.js.map
\No newline at end of file