UNPKG

2.77 kBTypeScriptView Raw
1/*
2 * Copyright 2015 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
17import classNames from "classnames";
18import * as React from "react";
19import { polyfill } from "react-lifecycles-compat";
20
21import { AbstractPureComponent2, Classes } from "../../common";
22import { DISPLAYNAME_PREFIX, removeNonHTMLProps } from "../../common/props";
23import { TabProps, TabId } from "./tab";
24
25// eslint-disable-next-line deprecation/deprecation
26export type TabTitleProps = ITabTitleProps;
27/** @deprecated use TabTitleProps */
28export interface ITabTitleProps extends TabProps {
29 /** Handler invoked when this tab is clicked. */
30 onClick: (id: TabId, event: React.MouseEvent<HTMLElement>) => void;
31
32 /** ID of the parent `Tabs` to which this tab belongs. Used to generate ID for ARIA attributes. */
33 parentId: TabId;
34
35 /** Whether the tab is currently selected. */
36 selected: boolean;
37}
38
39@polyfill
40export class TabTitle extends AbstractPureComponent2<TabTitleProps> {
41 public static displayName = `${DISPLAYNAME_PREFIX}.TabTitle`;
42
43 public render() {
44 const { className, children, disabled, id, parentId, selected, title, ...htmlProps } = this.props;
45 return (
46 <div
47 {...removeNonHTMLProps(htmlProps)}
48 aria-controls={generateTabPanelId(parentId, id)}
49 aria-disabled={disabled}
50 aria-expanded={selected}
51 aria-selected={selected}
52 className={classNames(Classes.TAB, className)}
53 data-tab-id={id}
54 id={generateTabTitleId(parentId, id)}
55 onClick={disabled ? undefined : this.handleClick}
56 role="tab"
57 tabIndex={disabled ? undefined : selected ? 0 : -1}
58 >
59 {title}
60 {children}
61 </div>
62 );
63 }
64
65 private handleClick = (e: React.MouseEvent<HTMLElement>) => this.props.onClick(this.props.id, e);
66}
67
68export function generateTabPanelId(parentId: TabId, tabId: TabId) {
69 return `${Classes.TAB_PANEL}_${parentId}_${tabId}`;
70}
71
72export function generateTabTitleId(parentId: TabId, tabId: TabId) {
73 return `${Classes.TAB}-title_${parentId}_${tabId}`;
74}