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 | */
|
16 |
|
17 | import * as React from "react";
|
18 | import { polyfill } from "react-lifecycles-compat";
|
19 |
|
20 | import { AbstractPureComponent2, Classes } from "../../common";
|
21 | import { Button } from "../button/buttons";
|
22 | import { Text } from "../text/text";
|
23 | import { IPanel } from "./panelProps";
|
24 |
|
25 | /* eslint-disable deprecation/deprecation */
|
26 |
|
27 | export interface IPanelViewProps {
|
28 | /**
|
29 | * Callback invoked when the user presses the back button or a panel invokes
|
30 | * the `closePanel()` injected prop method.
|
31 | */
|
32 | onClose: (removedPanel: IPanel<any>) => void;
|
33 |
|
34 | /**
|
35 | * Callback invoked when a panel invokes the `openPanel(panel)` injected
|
36 | * prop method.
|
37 | */
|
38 | onOpen: (addedPanel: IPanel<any>) => void;
|
39 |
|
40 | /** The panel to be displayed. */
|
41 | panel: IPanel;
|
42 |
|
43 | /** The previous panel in the stack, for rendering the "back" button. */
|
44 | previousPanel?: IPanel;
|
45 |
|
46 | /** Whether to show the header with the "back" button. */
|
47 | showHeader: boolean;
|
48 | }
|
49 |
|
50 |
|
51 | export class PanelView extends AbstractPureComponent2<IPanelViewProps> {
|
52 | public render() {
|
53 | const { panel, onOpen } = this.props;
|
54 | // two <span> tags in header ensure title is centered as long as
|
55 | // possible, due to `flex: 1` magic.
|
56 | return (
|
57 | <div className={Classes.PANEL_STACK_VIEW}>
|
58 | {this.maybeRenderHeader()}
|
59 | <panel.component openPanel={onOpen} closePanel={this.handleClose} {...panel.props} />
|
60 | </div>
|
61 | );
|
62 | }
|
63 |
|
64 | private maybeRenderHeader() {
|
65 | if (!this.props.showHeader) {
|
66 | return null;
|
67 | }
|
68 | return (
|
69 | <div className={Classes.PANEL_STACK_HEADER}>
|
70 | <span>{this.maybeRenderBack()}</span>
|
71 | <Text className={Classes.HEADING} ellipsize={true} title={this.props.panel.htmlTitle}>
|
72 | {this.props.panel.title}
|
73 | </Text>
|
74 | <span />
|
75 | </div>
|
76 | );
|
77 | }
|
78 |
|
79 | private maybeRenderBack() {
|
80 | if (this.props.previousPanel === undefined) {
|
81 | return null;
|
82 | }
|
83 | return (
|
84 | <Button
|
85 | aria-label="Back"
|
86 | className={Classes.PANEL_STACK_HEADER_BACK}
|
87 | icon="chevron-left"
|
88 | minimal={true}
|
89 | onClick={this.handleClose}
|
90 | small={true}
|
91 | text={this.props.previousPanel.title}
|
92 | title={this.props.previousPanel.htmlTitle}
|
93 | />
|
94 | );
|
95 | }
|
96 |
|
97 | private handleClose = () => this.props.onClose(this.props.panel);
|
98 | }
|