1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __decorate, __extends, __spreadArrays } from "tslib";
|
17 | import classNames from "classnames";
|
18 | import * as React from "react";
|
19 | import { polyfill } from "react-lifecycles-compat";
|
20 | import { CSSTransition, TransitionGroup } from "react-transition-group";
|
21 | import { AbstractPureComponent2, Classes } from "../../common";
|
22 | import * as Errors from "../../common/errors";
|
23 | import { PanelView } from "./panelView";
|
24 |
|
25 | var PanelStack = (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 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
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 |
|
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 |
|
81 | if (this.props.stack !== prevProps.stack && prevProps.stack != null) {
|
82 | this.setState({ stack: this.props.stack.slice().reverse() });
|
83 | }
|
84 |
|
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));
|
121 | export { PanelStack };
|
122 |
|
\ | No newline at end of file |