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