| 1 |
import { __rest } from "tslib"; |
| 2 |
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; |
| 3 |
import { useContext, forwardRef, useEffect } from 'react'; |
| 4 |
import styles from '@patternfly/react-styles/css/components/Tabs/tabs.mjs'; |
| 5 |
import { TabButton } from './TabButton'; |
| 6 |
import { TabsContext } from './TabsContext'; |
| 7 |
import { css } from '@patternfly/react-styles'; |
| 8 |
import { Tooltip } from '../Tooltip'; |
| 9 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 10 |
import { TabAction } from './TabAction'; |
| 11 |
const TabBase = (_a) => { |
| 12 |
var { title, eventKey, tabContentRef, id: childId, tabContentId, className: childClassName = '', ouiaId: childOuiaId, isDisabled, isAriaDisabled, inoperableEvents = ['onClick', 'onKeyPress'], href, innerRef, tooltip, closeButtonAriaLabel, isCloseDisabled = false, actions } = _a, props = __rest(_a, ["title", "eventKey", "tabContentRef", "id", "tabContentId", "className", "ouiaId", "isDisabled", "isAriaDisabled", "inoperableEvents", "href", "innerRef", "tooltip", "closeButtonAriaLabel", "isCloseDisabled", "actions"]); |
| 13 |
const preventedEvents = inoperableEvents.reduce((handlers, eventToPrevent) => (Object.assign(Object.assign({}, handlers), { [eventToPrevent]: (event) => { |
| 14 |
event.preventDefault(); |
| 15 |
} })), {}); |
| 16 |
const { mountOnEnter, localActiveKey, unmountOnExit, uniqueId, setAccentStyles, handleTabClick, handleTabClose } = useContext(TabsContext); |
| 17 |
let ariaControls = tabContentId ? `${tabContentId}` : `pf-tab-section-${eventKey}-${childId || uniqueId}`; |
| 18 |
if ((mountOnEnter || unmountOnExit) && eventKey !== localActiveKey) { |
| 19 |
ariaControls = undefined; |
| 20 |
} |
| 21 |
const isButtonElement = Boolean(!href); |
| 22 |
const getDefaultTabIdx = () => { |
| 23 |
if (isDisabled) { |
| 24 |
return isButtonElement ? null : -1; |
| 25 |
} |
| 26 |
else if (isAriaDisabled) { |
| 27 |
return null; |
| 28 |
} |
| 29 |
}; |
| 30 |
const tabButton = (_jsx(TabButton, Object.assign({ 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) => 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, { children: title }))); |
| 31 |
useEffect(() => { |
| 32 |
setAccentStyles(true); |
| 33 |
}, [title, actions]); |
| 34 |
return (_jsxs("li", { className: css(styles.tabsItem, eventKey === localActiveKey && styles.modifiers.current, (handleTabClose || actions) && styles.modifiers.action, (isDisabled || isAriaDisabled) && styles.modifiers.disabled, childClassName), role: "presentation", children: [tooltip ? _jsx(Tooltip, Object.assign({}, tooltip.props, { children: tabButton })) : tabButton, actions && actions, handleTabClose !== undefined && (_jsx(TabAction, { "aria-label": closeButtonAriaLabel || 'Close tab', onClick: (event) => handleTabClose(event, eventKey, tabContentRef), isDisabled: isCloseDisabled, children: _jsx(RhMicronsCloseIcon, {}) }))] })); |
| 35 |
}; |
| 36 |
export const Tab = forwardRef((props, ref) => _jsx(TabBase, Object.assign({ innerRef: ref }, props))); |
| 37 |
Tab.displayName = 'Tab'; |
| 38 |
//# sourceMappingURL=Tab.js.map |