forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTab.tsx
More file actions
150 lines (144 loc) · 5.26 KB
/
Tab.tsx
File metadata and controls
150 lines (144 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { useContext, forwardRef, useEffect } from 'react';
import styles from '@patternfly/react-styles/css/components/Tabs/tabs';
import { OUIAProps } from '../../helpers';
import { TabButton } from './TabButton';
import { TabsContext } from './TabsContext';
import { css } from '@patternfly/react-styles';
import { Tooltip } from '../Tooltip';
import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon';
import { TabAction } from './TabAction';
export interface TabProps
extends Omit<React.HTMLProps<HTMLAnchorElement | HTMLButtonElement>, 'title' | 'action'>,
OUIAProps {
/** content rendered inside the Tab content area. */
children?: React.ReactNode;
/** additional classes added to the Tab */
className?: string;
/** URL associated with the Tab. A Tab with an href will render as an <a> instead of a <button>. A Tab inside a <Tabs component="nav"> should have an href. */
href?: string;
/** Content rendered in the tab title. Should be <TabTitleText> and/or <TabTitleIcon> for proper styling. */
title: React.ReactNode;
/** uniquely identifies the tab */
eventKey: number | string;
/** child id for case in which a TabContent section is defined outside of a Tabs component */
tabContentId?: string | number;
/** child reference for case in which a TabContent section is defined outside of a Tabs component */
tabContentRef?: React.RefObject<any>;
/** whether to render the tab or not */
isHidden?: boolean;
/** Adds disabled styling and disables the button using the disabled html attribute */
isDisabled?: boolean;
/** Adds disabled styling and communicates that the button is disabled using the aria-disabled html attribute */
isAriaDisabled?: boolean;
/** Events to prevent when the button is in an aria-disabled state */
inoperableEvents?: string[];
/** @hide Forwarded ref */
innerRef?: React.Ref<any>;
/** Optional Tooltip rendered to a Tab. Should be <Tooltip> with appropriate props for proper rendering. */
tooltip?: React.ReactElement<any>;
/** Aria-label for the close button added by passing the onClose property to Tabs. */
closeButtonAriaLabel?: string;
/** Flag indicating the close button should be disabled */
isCloseDisabled?: boolean;
/** Actions rendered beside the tab content */
actions?: React.ReactNode;
/** Value to set the data-ouia-component-id for the tab button.*/
ouiaId?: number | string;
}
const TabBase: React.FunctionComponent<TabProps> = ({
title,
eventKey,
tabContentRef,
id: childId,
tabContentId,
className: childClassName = '',
ouiaId: childOuiaId,
isDisabled,
isAriaDisabled,
inoperableEvents = ['onClick', 'onKeyPress'],
href,
innerRef,
tooltip,
closeButtonAriaLabel,
isCloseDisabled = false,
actions,
...props
}: TabProps) => {
const preventedEvents = inoperableEvents.reduce(
(handlers, eventToPrevent) => ({
...handlers,
[eventToPrevent]: (event: React.SyntheticEvent<HTMLButtonElement>) => {
event.preventDefault();
}
}),
{}
);
const { mountOnEnter, localActiveKey, unmountOnExit, uniqueId, setAccentStyles, handleTabClick, handleTabClose } =
useContext(TabsContext);
let ariaControls = tabContentId ? `${tabContentId}` : `pf-tab-section-${eventKey}-${childId || uniqueId}`;
if ((mountOnEnter || unmountOnExit) && eventKey !== localActiveKey) {
ariaControls = undefined;
}
const isButtonElement = Boolean(!href);
const getDefaultTabIdx = () => {
if (isDisabled) {
return isButtonElement ? null : -1;
} else if (isAriaDisabled) {
return null;
}
};
const tabButton = (
<TabButton
parentInnerRef={innerRef}
className={css(
styles.tabsLink,
isDisabled && href && styles.modifiers.disabled,
isAriaDisabled && styles.modifiers.ariaDisabled
)}
disabled={isButtonElement ? isDisabled : null}
aria-disabled={isDisabled || isAriaDisabled}
tabIndex={getDefaultTabIdx()}
onClick={(event: any) => handleTabClick(event, eventKey, tabContentRef)}
{...(isAriaDisabled ? preventedEvents : null)}
id={`pf-tab-${eventKey}-${childId || uniqueId}`}
aria-controls={ariaControls}
tabContentRef={tabContentRef}
ouiaId={childOuiaId}
href={href}
role="tab"
aria-selected={eventKey === localActiveKey}
{...props}
>
{title}
</TabButton>
);
useEffect(() => {
setAccentStyles(true);
}, [title, actions]);
return (
<li
className={css(
styles.tabsItem,
eventKey === localActiveKey && styles.modifiers.current,
(handleTabClose || actions) && styles.modifiers.action,
(isDisabled || isAriaDisabled) && styles.modifiers.disabled,
childClassName
)}
role="presentation"
>
{tooltip ? <Tooltip {...tooltip.props}>{tabButton}</Tooltip> : tabButton}
{actions && actions}
{handleTabClose !== undefined && (
<TabAction
aria-label={closeButtonAriaLabel || 'Close tab'}
onClick={(event: any) => handleTabClose(event, eventKey, tabContentRef)}
isDisabled={isCloseDisabled}
>
<TimesIcon />
</TabAction>
)}
</li>
);
};
export const Tab = forwardRef((props: TabProps, ref: React.Ref<any>) => <TabBase innerRef={ref} {...props} />);
Tab.displayName = 'Tab';