| 1 |
import { Children, Component, createRef, isValidElement } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Tabs/tabs'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import { PickOptional } from '../../helpers/typeUtils'; |
| 5 |
import RhMicronsCaretLeftIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-left-icon'; |
| 6 |
import RhMicronsCaretRightIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-right-icon'; |
| 7 |
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon'; |
| 8 |
import PlusIcon from '@patternfly/react-icons/dist/esm/icons/plus-icon'; |
| 9 |
import { |
| 10 |
isElementInView, |
| 11 |
formatBreakpointMods, |
| 12 |
getLanguageDirection, |
| 13 |
getInlineStartProperty |
| 14 |
} from '../../helpers/util'; |
| 15 |
import { TabContent } from './TabContent'; |
| 16 |
import { TabProps } from './Tab'; |
| 17 |
import { TabsContextProvider } from './TabsContext'; |
| 18 |
import { OverflowTab, HorizontalOverflowPopperProps } from './OverflowTab'; |
| 19 |
import { Button } from '../Button'; |
| 20 |
import { getOUIAProps, OUIAProps, canUseDOM } from '../../helpers'; |
| 21 |
import { SSRSafeIds } from '../../helpers/SSRSafeIds/SSRSafeIds'; |
| 22 |
import { GenerateId } from '../../helpers/GenerateId/GenerateId'; |
| 23 |
import linkAccentLength from '@patternfly/react-tokens/dist/esm/c_tabs_link_accent_length'; |
| 24 |
import linkAccentStart from '@patternfly/react-tokens/dist/esm/c_tabs_link_accent_start'; |
| 25 |
|
| 26 |
export enum TabsComponent { |
| 27 |
div = 'div', |
| 28 |
nav = 'nav' |
| 29 |
} |
| 30 |
export interface HorizontalOverflowObject { |
| 31 |
/** Flag which shows the count of overflowing tabs when enabled */ |
| 32 |
showTabCount?: boolean; |
| 33 |
/** The text which displays when an overflowing tab isn't selected */ |
| 34 |
defaultTitleText?: string; |
| 35 |
/** The aria label applied to the button which toggles the tab overflow menu */ |
| 36 |
toggleAriaLabel?: string; |
| 37 |
/** Additional props to spread to the popper menu. */ |
| 38 |
popperProps?: HorizontalOverflowPopperProps; |
| 39 |
} |
| 40 |
|
| 41 |
type TabElement = React.ReactElement<TabProps, React.JSXElementConstructor<TabProps>>; |
| 42 |
type TabsChild = TabElement | boolean | null | undefined; |
| 43 |
|
| 44 |
export interface TabsProps |
| 45 |
extends Omit<React.HTMLProps<HTMLElement | HTMLDivElement>, 'onSelect' | 'onToggle'>, OUIAProps { |
| 46 |
/** Content rendered inside the tabs component. Only `Tab` components or expressions resulting in a falsy value are allowed here. */ |
| 47 |
children: TabsChild | TabsChild[]; |
| 48 |
/** Additional classes added to the tabs */ |
| 49 |
className?: string; |
| 50 |
/** Tabs background color variant */ |
| 51 |
variant?: 'default' | 'secondary'; |
| 52 |
/** The index of the active tab */ |
| 53 |
activeKey?: number | string; |
| 54 |
/** The index of the default active tab. Set this for uncontrolled Tabs */ |
| 55 |
defaultActiveKey?: number | string; |
| 56 |
/** Callback to handle tab selection */ |
| 57 |
onSelect?: (event: React.MouseEvent<HTMLElement, MouseEvent>, eventKey: TabProps['eventKey']) => void; |
| 58 |
/** Callback to handle tab closing and adds a basic close button to all tabs. This is overridden by the tab actions property. */ |
| 59 |
onClose?: (event: React.MouseEvent<HTMLElement, MouseEvent>, eventKey: TabProps['eventKey']) => void; |
| 60 |
/** Callback for the add button. Passing this property inserts the add button */ |
| 61 |
onAdd?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void; |
| 62 |
/** Aria-label for the add button */ |
| 63 |
addButtonAriaLabel?: string; |
| 64 |
/** A readable string to create an accessible name for the tablist element. This can be used to differentiate multiple tablists on a page, and should be used for subtabs. */ |
| 65 |
tabListAriaLabel?: string; |
| 66 |
/** Id of an element that provides an accessible name for the tablist. Use this when a visible label already exists on the page. */ |
| 67 |
tabListAriaLabelledBy?: string; |
| 68 |
/** Uniquely identifies the tabs */ |
| 69 |
id?: string; |
| 70 |
/** Flag indicating that the add button is disabled when onAdd is passed in */ |
| 71 |
isAddButtonDisabled?: boolean; |
| 72 |
/** Enables the filled tab list layout */ |
| 73 |
isFilled?: boolean; |
| 74 |
/** Enables subtab tab styling */ |
| 75 |
isSubtab?: boolean; |
| 76 |
/** @beta Enables horizontal nav tab styling */ |
| 77 |
isNav?: boolean; |
| 78 |
/** Enables box styling to the tab component */ |
| 79 |
isBox?: boolean; |
| 80 |
/** Enables vertical tab styling */ |
| 81 |
isVertical?: boolean; |
| 82 |
/** Disables border bottom tab styling on tabs. Defaults to false. To remove the bottom border, set this prop to true. */ |
| 83 |
hasNoBorderBottom?: boolean; |
| 84 |
/** @deprecated Please use backScrollAriaLabel. Aria-label for the left scroll button */ |
| 85 |
leftScrollAriaLabel?: string; |
| 86 |
/** @deprecated Please use forwardScrollAriaLabel. Aria-label for the right scroll button */ |
| 87 |
rightScrollAriaLabel?: string; |
| 88 |
/** Aria-label for the back scroll button */ |
| 89 |
backScrollAriaLabel?: string; |
| 90 |
/** Aria-label for the forward scroll button */ |
| 91 |
forwardScrollAriaLabel?: string; |
| 92 |
/** Determines what tag is used around the tabs. Use "nav" to define the tabs inside a navigation region */ |
| 93 |
component?: 'div' | 'nav'; |
| 94 |
/** Provides an accessible label for the tabs. Labels should be unique for each set of tabs that are present on a page. When component is set to nav, this prop should be defined to differentiate the tabs from other navigation regions on the page. */ |
| 95 |
'aria-label'?: string; |
| 96 |
/** Waits until the first "enter" transition to mount tab children (add them to the DOM) */ |
| 97 |
mountOnEnter?: boolean; |
| 98 |
/** Unmounts tab children (removes them from the DOM) when they are no longer visible */ |
| 99 |
unmountOnExit?: boolean; |
| 100 |
/** Flag indicates that the tabs should use page insets. */ |
| 101 |
usePageInsets?: boolean; |
| 102 |
/** Insets at various breakpoints. */ |
| 103 |
inset?: { |
| 104 |
default?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 105 |
sm?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 106 |
md?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 107 |
lg?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 108 |
xl?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 109 |
'2xl'?: 'insetNone' | 'insetSm' | 'insetMd' | 'insetLg' | 'insetXl' | 'inset2xl'; |
| 110 |
}; |
| 111 |
/** Enable expandable vertical tabs at various breakpoints. (isVertical should be set to true for this to work) */ |
| 112 |
expandable?: { |
| 113 |
default?: 'expandable' | 'nonExpandable'; |
| 114 |
sm?: 'expandable' | 'nonExpandable'; |
| 115 |
md?: 'expandable' | 'nonExpandable'; |
| 116 |
lg?: 'expandable' | 'nonExpandable'; |
| 117 |
xl?: 'expandable' | 'nonExpandable'; |
| 118 |
'2xl'?: 'expandable' | 'nonExpandable'; |
| 119 |
}; |
| 120 |
/** Flag to indicate if the vertical tabs are expanded */ |
| 121 |
isExpanded?: boolean; |
| 122 |
/** Flag indicating the default expanded state for uncontrolled expand/collapse of */ |
| 123 |
defaultIsExpanded?: boolean; |
| 124 |
/** Text that appears in the expandable toggle */ |
| 125 |
toggleText?: string; |
| 126 |
/** Aria-label for the expandable toggle */ |
| 127 |
toggleAriaLabel?: string; |
| 128 |
/** Callback function to toggle the expandable tabs. */ |
| 129 |
onToggle?: (event: React.MouseEvent, isExpanded: boolean) => void; |
| 130 |
/** Flag which places overflowing tabs into a menu triggered by the last tab. Additionally an object can be passed with custom settings for the overflow tab. */ |
| 131 |
isOverflowHorizontal?: boolean | HorizontalOverflowObject; |
| 132 |
/** Value to overwrite the randomly generated data-ouia-component-id.*/ |
| 133 |
ouiaId?: number | string; |
| 134 |
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */ |
| 135 |
ouiaSafe?: boolean; |
| 136 |
} |
| 137 |
|
| 138 |
const variantStyle = { |
| 139 |
default: '', |
| 140 |
secondary: styles.modifiers.secondary |
| 141 |
}; |
| 142 |
|
| 143 |
interface TabsState { |
| 144 |
/** Used to signal if the scroll buttons should be used */ |
| 145 |
enableScrollButtons: boolean; |
| 146 |
/** Used to control if the scroll buttons should be shown to the user via the pf-m-scrollable class */ |
| 147 |
showScrollButtons: boolean; |
| 148 |
/** Used to control if the scroll buttons should be rendered. Rendering must occur before the scroll buttons are |
| 149 |
* shown and rendering must be stopped after they stop being shown to preserve CSS transitions. |
| 150 |
*/ |
| 151 |
renderScrollButtons: boolean; |
| 152 |
disableBackScrollButton: boolean; |
| 153 |
disableForwardScrollButton: boolean; |
| 154 |
shownKeys: (string | number)[]; |
| 155 |
uncontrolledActiveKey: number | string; |
| 156 |
uncontrolledIsExpandedLocal: boolean; |
| 157 |
overflowingTabCount: number; |
| 158 |
isInitializingAccent: boolean; |
| 159 |
currentLinkAccentLength: string; |
| 160 |
currentLinkAccentStart: string; |
| 161 |
} |
| 162 |
|
| 163 |
class Tabs extends Component<TabsProps, TabsState> { |
| 164 |
static displayName = 'Tabs'; |
| 165 |
tabList = createRef<HTMLUListElement>(); |
| 166 |
leftScrollButtonRef = createRef<HTMLButtonElement>(); |
| 167 |
private direction = 'ltr'; |
| 168 |
constructor(props: TabsProps) { |
| 169 |
super(props); |
| 170 |
this.state = { |
| 171 |
enableScrollButtons: false, |
| 172 |
showScrollButtons: false, |
| 173 |
renderScrollButtons: false, |
| 174 |
disableBackScrollButton: true, |
| 175 |
disableForwardScrollButton: true, |
| 176 |
shownKeys: this.props.defaultActiveKey !== undefined ? [this.props.defaultActiveKey] : [this.props.activeKey], // only for mountOnEnter case |
| 177 |
uncontrolledActiveKey: this.props.defaultActiveKey, |
| 178 |
uncontrolledIsExpandedLocal: this.props.defaultIsExpanded, |
| 179 |
overflowingTabCount: 0, |
| 180 |
isInitializingAccent: true, |
| 181 |
currentLinkAccentLength: linkAccentLength.value, |
| 182 |
currentLinkAccentStart: linkAccentStart.value |
| 183 |
}; |
| 184 |
|
| 185 |
if (this.props.isVertical && this.props.expandable !== undefined) { |
| 186 |
if (!this.props.toggleAriaLabel && !this.props.toggleText) { |
| 187 |
// eslint-disable-next-line no-console |
| 188 |
console.error( |
| 189 |
'Tabs:', |
| 190 |
'toggleAriaLabel or the toggleText prop is required to make the toggle button accessible' |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
scrollTimeout: NodeJS.Timeout = null; |
| 197 |
|
| 198 |
static defaultProps: PickOptional<TabsProps> = { |
| 199 |
activeKey: 0, |
| 200 |
onSelect: () => undefined as any, |
| 201 |
isFilled: false, |
| 202 |
isSubtab: false, |
| 203 |
isNav: false, |
| 204 |
isVertical: false, |
| 205 |
isBox: false, |
| 206 |
hasNoBorderBottom: false, |
| 207 |
leftScrollAriaLabel: 'Scroll left', |
| 208 |
backScrollAriaLabel: 'Scroll back', |
| 209 |
rightScrollAriaLabel: 'Scroll right', |
| 210 |
forwardScrollAriaLabel: 'Scroll forward', |
| 211 |
mountOnEnter: false, |
| 212 |
unmountOnExit: false, |
| 213 |
ouiaSafe: true, |
| 214 |
variant: 'default', |
| 215 |
|
| 216 |
onToggle: (_event: React.MouseEvent, _isExpanded: boolean): void => undefined |
| 217 |
}; |
| 218 |
|
| 219 |
handleTabClick( |
| 220 |
event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 221 |
eventKey: number | string, |
| 222 |
tabContentRef: React.RefObject<any> |
| 223 |
) { |
| 224 |
const { shownKeys } = this.state; |
| 225 |
const { onSelect, defaultActiveKey } = this.props; |
| 226 |
// if defaultActiveKey Tabs are uncontrolled, set new active key internally |
| 227 |
if (defaultActiveKey !== undefined) { |
| 228 |
this.setState({ |
| 229 |
uncontrolledActiveKey: eventKey |
| 230 |
}); |
| 231 |
} else { |
| 232 |
onSelect(event, eventKey); |
| 233 |
} |
| 234 |
|
| 235 |
// process any tab content sections outside of the component |
| 236 |
if (tabContentRef) { |
| 237 |
Children.toArray(this.props.children) |
| 238 |
.filter((child): child is TabElement => isValidElement(child)) |
| 239 |
.filter(({ props }) => props.tabContentRef && props.tabContentRef.current) |
| 240 |
.forEach((child) => (child.props.tabContentRef.current.hidden = true)); |
| 241 |
// most recently selected tabContent |
| 242 |
if (tabContentRef.current) { |
| 243 |
tabContentRef.current.hidden = false; |
| 244 |
} |
| 245 |
} |
| 246 |
if (this.props.mountOnEnter) { |
| 247 |
this.setState({ |
| 248 |
shownKeys: shownKeys.concat(eventKey) |
| 249 |
}); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
countOverflowingElements = (container: HTMLUListElement) => { |
| 254 |
const elements = Array.from(container.children); |
| 255 |
return elements.filter((element) => !isElementInView(container, element as HTMLElement, false)).length; |
| 256 |
}; |
| 257 |
|
| 258 |
handleScrollButtons = () => { |
| 259 |
const { isOverflowHorizontal: isOverflowHorizontal } = this.props; |
| 260 |
// add debounce to the scroll event |
| 261 |
clearTimeout(this.scrollTimeout); |
| 262 |
this.scrollTimeout = setTimeout(() => { |
| 263 |
const container = this.tabList.current; |
| 264 |
let disableBackScrollButton = true; |
| 265 |
let disableForwardScrollButton = true; |
| 266 |
let enableScrollButtons = false; |
| 267 |
let overflowingTabCount = 0; |
| 268 |
|
| 269 |
if (container && !this.props.isVertical && !isOverflowHorizontal) { |
| 270 |
// get first element and check if it is in view |
| 271 |
const overflowOnLeft = !isElementInView(container, container.firstChild as HTMLElement, false); |
| 272 |
// get last element and check if it is in view |
| 273 |
const overflowOnRight = !isElementInView(container, container.lastChild as HTMLElement, false); |
| 274 |
|
| 275 |
enableScrollButtons = overflowOnLeft || overflowOnRight; |
| 276 |
|
| 277 |
disableBackScrollButton = !overflowOnLeft; |
| 278 |
disableForwardScrollButton = !overflowOnRight; |
| 279 |
} |
| 280 |
|
| 281 |
if (isOverflowHorizontal) { |
| 282 |
overflowingTabCount = this.countOverflowingElements(container); |
| 283 |
} |
| 284 |
|
| 285 |
this.setState({ |
| 286 |
enableScrollButtons, |
| 287 |
disableBackScrollButton, |
| 288 |
disableForwardScrollButton, |
| 289 |
overflowingTabCount |
| 290 |
}); |
| 291 |
}, 100); |
| 292 |
}; |
| 293 |
|
| 294 |
scrollBack = () => { |
| 295 |
// find first Element that is fully in view on the left, then scroll to the element before it |
| 296 |
if (this.tabList.current) { |
| 297 |
const container = this.tabList.current; |
| 298 |
const childrenArr = Array.from(container.children); |
| 299 |
let firstElementInView: any; |
| 300 |
let lastElementOutOfView: any; |
| 301 |
let i; |
| 302 |
for (i = 0; i < childrenArr.length && !firstElementInView; i++) { |
| 303 |
if (isElementInView(container, childrenArr[i] as HTMLElement, false)) { |
| 304 |
firstElementInView = childrenArr[i]; |
| 305 |
lastElementOutOfView = childrenArr[i - 1]; |
| 306 |
} |
| 307 |
} |
| 308 |
if (lastElementOutOfView) { |
| 309 |
if (this.direction === 'ltr') { |
| 310 |
// LTR scrolls left to go back |
| 311 |
container.scrollLeft -= lastElementOutOfView.scrollWidth; |
| 312 |
} else { |
| 313 |
// RTL scrolls right to go back |
| 314 |
container.scrollLeft += lastElementOutOfView.scrollWidth; |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
}; |
| 319 |
|
| 320 |
scrollForward = () => { |
| 321 |
// find last Element that is fully in view on the right, then scroll to the element after it |
| 322 |
if (this.tabList.current) { |
| 323 |
const container = this.tabList.current as any; |
| 324 |
const childrenArr = Array.from(container.children); |
| 325 |
let lastElementInView: any; |
| 326 |
let firstElementOutOfView: any; |
| 327 |
for (let i = childrenArr.length - 1; i >= 0 && !lastElementInView; i--) { |
| 328 |
if (isElementInView(container, childrenArr[i] as HTMLElement, false)) { |
| 329 |
lastElementInView = childrenArr[i]; |
| 330 |
firstElementOutOfView = childrenArr[i + 1]; |
| 331 |
} |
| 332 |
} |
| 333 |
if (firstElementOutOfView) { |
| 334 |
if (this.direction === 'ltr') { |
| 335 |
// LTR scrolls right to go forward |
| 336 |
container.scrollLeft += firstElementOutOfView.scrollWidth; |
| 337 |
} else { |
| 338 |
// RTL scrolls left to go forward |
| 339 |
container.scrollLeft -= firstElementOutOfView.scrollWidth; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
}; |
| 344 |
|
| 345 |
hideScrollButtons = () => { |
| 346 |
const { enableScrollButtons, renderScrollButtons, showScrollButtons } = this.state; |
| 347 |
if (!enableScrollButtons && !showScrollButtons && renderScrollButtons) { |
| 348 |
this.setState({ renderScrollButtons: false }); |
| 349 |
} |
| 350 |
}; |
| 351 |
|
| 352 |
setAccentStyles = (shouldInitializeStyle?: boolean) => { |
| 353 |
const currentItem = this.tabList.current.querySelector('li.pf-m-current') as HTMLElement; |
| 354 |
if (!currentItem) { |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
const { isVertical } = this.props; |
| 359 |
const { offsetWidth, offsetHeight, offsetTop } = currentItem; |
| 360 |
const lengthValue = isVertical ? offsetHeight : offsetWidth; |
| 361 |
const startValue = isVertical ? offsetTop : getInlineStartProperty(currentItem, this.tabList.current); |
| 362 |
this.setState({ |
| 363 |
currentLinkAccentLength: `${lengthValue}px`, |
| 364 |
currentLinkAccentStart: `${startValue}px`, |
| 365 |
...(shouldInitializeStyle && { isInitializingAccent: true }) |
| 366 |
}); |
| 367 |
|
| 368 |
setTimeout(() => { |
| 369 |
this.setState({ isInitializingAccent: false }); |
| 370 |
}, 0); |
| 371 |
}; |
| 372 |
|
| 373 |
handleResize = () => { |
| 374 |
this.handleScrollButtons(); |
| 375 |
this.setAccentStyles(); |
| 376 |
}; |
| 377 |
|
| 378 |
componentDidMount() { |
| 379 |
if (!this.props.isVertical) { |
| 380 |
if (canUseDOM) { |
| 381 |
window.addEventListener('resize', this.handleResize, false); |
| 382 |
} |
| 383 |
this.direction = getLanguageDirection(this.tabList.current); |
| 384 |
// call the handle resize function to check if scroll buttons should be shown |
| 385 |
this.handleScrollButtons(); |
| 386 |
} |
| 387 |
|
| 388 |
this.setAccentStyles(true); |
| 389 |
} |
| 390 |
|
| 391 |
componentWillUnmount() { |
| 392 |
if (!this.props.isVertical) { |
| 393 |
if (canUseDOM) { |
| 394 |
window.removeEventListener('resize', this.handleResize, false); |
| 395 |
} |
| 396 |
} |
| 397 |
clearTimeout(this.scrollTimeout); |
| 398 |
this.leftScrollButtonRef.current?.removeEventListener('transitionend', this.hideScrollButtons); |
| 399 |
} |
| 400 |
|
| 401 |
componentDidUpdate(prevProps: TabsProps, prevState: TabsState) { |
| 402 |
this.direction = getLanguageDirection(this.tabList.current); |
| 403 |
const { activeKey, mountOnEnter, isOverflowHorizontal, children, defaultActiveKey } = this.props; |
| 404 |
const { shownKeys, overflowingTabCount, enableScrollButtons, uncontrolledActiveKey } = this.state; |
| 405 |
const isOnCloseUpdate = !!prevProps.onClose !== !!this.props.onClose; |
| 406 |
if ( |
| 407 |
(defaultActiveKey !== undefined && prevState.uncontrolledActiveKey !== uncontrolledActiveKey) || |
| 408 |
(defaultActiveKey === undefined && prevProps.activeKey !== activeKey) || |
| 409 |
isOnCloseUpdate |
| 410 |
) { |
| 411 |
this.setAccentStyles(isOnCloseUpdate); |
| 412 |
} |
| 413 |
|
| 414 |
if (prevProps.activeKey !== activeKey && mountOnEnter && shownKeys.indexOf(activeKey) < 0) { |
| 415 |
this.setState({ |
| 416 |
shownKeys: shownKeys.concat(activeKey) |
| 417 |
}); |
| 418 |
} |
| 419 |
|
| 420 |
if ( |
| 421 |
prevProps.children && |
| 422 |
children && |
| 423 |
Children.toArray(prevProps.children).length !== Children.toArray(children).length |
| 424 |
) { |
| 425 |
this.handleScrollButtons(); |
| 426 |
this.setAccentStyles(true); |
| 427 |
} |
| 428 |
|
| 429 |
const currentOverflowingTabCount = this.countOverflowingElements(this.tabList.current); |
| 430 |
if (isOverflowHorizontal && currentOverflowingTabCount) { |
| 431 |
this.setState({ overflowingTabCount: currentOverflowingTabCount + overflowingTabCount }); |
| 432 |
} |
| 433 |
|
| 434 |
if (!prevState.enableScrollButtons && enableScrollButtons) { |
| 435 |
this.setState({ renderScrollButtons: true }); |
| 436 |
setTimeout(() => { |
| 437 |
// Remove any existing listener before adding a new one to prevent accumulation |
| 438 |
this.leftScrollButtonRef.current?.removeEventListener('transitionend', this.hideScrollButtons); |
| 439 |
this.leftScrollButtonRef.current?.addEventListener('transitionend', this.hideScrollButtons); |
| 440 |
this.setState({ showScrollButtons: true }); |
| 441 |
}, 100); |
| 442 |
} else if (prevState.enableScrollButtons && !enableScrollButtons) { |
| 443 |
this.setState({ showScrollButtons: false }); |
| 444 |
} |
| 445 |
|
| 446 |
if (prevState.uncontrolledIsExpandedLocal !== this.state.uncontrolledIsExpandedLocal) { |
| 447 |
this.setAccentStyles(true); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
static getDerivedStateFromProps(nextProps: TabsProps, prevState: TabsState) { |
| 452 |
if (prevState.uncontrolledActiveKey === undefined) { |
| 453 |
return null; |
| 454 |
} |
| 455 |
|
| 456 |
const childrenHasTabWithActiveEventKey = Children.toArray(nextProps.children) |
| 457 |
.filter((child): child is TabElement => isValidElement(child)) |
| 458 |
.some(({ props }) => props.eventKey === prevState.uncontrolledActiveKey); |
| 459 |
|
| 460 |
// if uncontrolledActiveKey is an existing eventKey of any Tab of nextProps.children --> don't update uncontrolledActiveKey |
| 461 |
if (childrenHasTabWithActiveEventKey) { |
| 462 |
return null; |
| 463 |
} |
| 464 |
|
| 465 |
// otherwise update state derived from nextProps.defaultActiveKey |
| 466 |
return { |
| 467 |
uncontrolledActiveKey: nextProps.defaultActiveKey, |
| 468 |
shownKeys: nextProps.defaultActiveKey !== undefined ? [nextProps.defaultActiveKey] : [nextProps.activeKey] // only for mountOnEnter case |
| 469 |
}; |
| 470 |
} |
| 471 |
|
| 472 |
render() { |
| 473 |
const { |
| 474 |
className, |
| 475 |
children, |
| 476 |
activeKey, |
| 477 |
defaultActiveKey, |
| 478 |
id, |
| 479 |
isAddButtonDisabled, |
| 480 |
isFilled, |
| 481 |
isSubtab, |
| 482 |
isNav, |
| 483 |
isVertical, |
| 484 |
isBox, |
| 485 |
hasNoBorderBottom, |
| 486 |
leftScrollAriaLabel, |
| 487 |
rightScrollAriaLabel, |
| 488 |
backScrollAriaLabel, |
| 489 |
forwardScrollAriaLabel, |
| 490 |
'aria-label': ariaLabel, |
| 491 |
component, |
| 492 |
ouiaId, |
| 493 |
ouiaSafe, |
| 494 |
mountOnEnter, |
| 495 |
unmountOnExit, |
| 496 |
usePageInsets, |
| 497 |
inset, |
| 498 |
variant, |
| 499 |
expandable, |
| 500 |
isExpanded, |
| 501 |
defaultIsExpanded, |
| 502 |
toggleText, |
| 503 |
toggleAriaLabel, |
| 504 |
addButtonAriaLabel, |
| 505 |
tabListAriaLabel, |
| 506 |
tabListAriaLabelledBy, |
| 507 |
onToggle, |
| 508 |
onClose, |
| 509 |
onAdd, |
| 510 |
isOverflowHorizontal: isOverflowHorizontal, |
| 511 |
...props |
| 512 |
} = this.props; |
| 513 |
const { |
| 514 |
showScrollButtons, |
| 515 |
renderScrollButtons, |
| 516 |
disableBackScrollButton, |
| 517 |
disableForwardScrollButton, |
| 518 |
shownKeys, |
| 519 |
uncontrolledActiveKey, |
| 520 |
uncontrolledIsExpandedLocal, |
| 521 |
overflowingTabCount, |
| 522 |
isInitializingAccent, |
| 523 |
currentLinkAccentLength, |
| 524 |
currentLinkAccentStart |
| 525 |
} = this.state; |
| 526 |
const filteredChildren = Children.toArray(children) |
| 527 |
.filter((child): child is TabElement => isValidElement(child)) |
| 528 |
.filter(({ props }) => !props.isHidden); |
| 529 |
|
| 530 |
const filteredChildrenWithoutOverflow = filteredChildren.slice(0, filteredChildren.length - overflowingTabCount); |
| 531 |
const filteredChildrenOverflowing = filteredChildren.slice(filteredChildren.length - overflowingTabCount); |
| 532 |
const overflowingTabProps = filteredChildrenOverflowing.map((child: React.ReactElement<TabProps>) => child.props); |
| 533 |
|
| 534 |
const defaultComponent = isNav && !component ? 'nav' : 'div'; |
| 535 |
const Component: any = component !== undefined ? component : defaultComponent; |
| 536 |
const localActiveKey = defaultActiveKey !== undefined ? uncontrolledActiveKey : activeKey; |
| 537 |
|
| 538 |
const isExpandedLocal = defaultIsExpanded !== undefined ? uncontrolledIsExpandedLocal : isExpanded; |
| 539 |
/* Uncontrolled expandable tabs */ |
| 540 |
const toggleTabs = (event: React.MouseEvent, newValue: boolean) => { |
| 541 |
if (isExpanded === undefined) { |
| 542 |
this.setState({ uncontrolledIsExpandedLocal: newValue }); |
| 543 |
} else { |
| 544 |
onToggle(event, newValue); |
| 545 |
} |
| 546 |
}; |
| 547 |
|
| 548 |
const hasOverflowTab = isOverflowHorizontal && overflowingTabCount > 0; |
| 549 |
const overflowObjectProps = typeof isOverflowHorizontal === 'object' ? { ...isOverflowHorizontal } : {}; |
| 550 |
|
| 551 |
return ( |
| 552 |
<SSRSafeIds prefix="pf-random-id-" ouiaComponentType={Tabs.displayName}> |
| 553 |
{(generatedId, generatedOuiaId) => { |
| 554 |
const uniqueId = id || generatedId; |
| 555 |
return ( |
| 556 |
<TabsContextProvider |
| 557 |
value={{ |
| 558 |
variant, |
| 559 |
mountOnEnter, |
| 560 |
unmountOnExit, |
| 561 |
localActiveKey, |
| 562 |
uniqueId, |
| 563 |
setAccentStyles: this.setAccentStyles, |
| 564 |
handleTabClick: (...args) => this.handleTabClick(...args), |
| 565 |
handleTabClose: onClose |
| 566 |
}} |
| 567 |
> |
| 568 |
<Component |
| 569 |
aria-label={ariaLabel} |
| 570 |
className={css( |
| 571 |
styles.tabs, |
| 572 |
styles.modifiers.animateCurrent, |
| 573 |
isFilled && styles.modifiers.fill, |
| 574 |
isSubtab && styles.modifiers.subtab, |
| 575 |
isNav && styles.modifiers.nav, |
| 576 |
isVertical && styles.modifiers.vertical, |
| 577 |
isVertical && expandable && formatBreakpointMods(expandable, styles), |
| 578 |
isVertical && expandable && isExpandedLocal && styles.modifiers.expanded, |
| 579 |
isBox && styles.modifiers.box, |
| 580 |
showScrollButtons && styles.modifiers.scrollable, |
| 581 |
usePageInsets && styles.modifiers.pageInsets, |
| 582 |
hasNoBorderBottom && styles.modifiers.noBorderBottom, |
| 583 |
formatBreakpointMods(inset, styles), |
| 584 |
variantStyle[variant], |
| 585 |
hasOverflowTab && styles.modifiers.overflow, |
| 586 |
isInitializingAccent && styles.modifiers.initializingAccent, |
| 587 |
className |
| 588 |
)} |
| 589 |
{...getOUIAProps(Tabs.displayName, ouiaId !== undefined ? ouiaId : generatedOuiaId, ouiaSafe)} |
| 590 |
id={id && id} |
| 591 |
{...props} |
| 592 |
style={{ |
| 593 |
[linkAccentLength.name]: currentLinkAccentLength, |
| 594 |
[linkAccentStart.name]: currentLinkAccentStart, |
| 595 |
...props.style |
| 596 |
}} |
| 597 |
> |
| 598 |
{expandable && isVertical && ( |
| 599 |
<GenerateId> |
| 600 |
{(randomId) => ( |
| 601 |
<div className={css(styles.tabsToggle)}> |
| 602 |
<div className={'pf-v6-c-tabs__toggle-button'}> |
| 603 |
<Button |
| 604 |
onClick={(event) => toggleTabs(event, !isExpandedLocal)} |
| 605 |
variant="plain" |
| 606 |
aria-label={toggleAriaLabel} |
| 607 |
aria-expanded={isExpandedLocal} |
| 608 |
id={`${randomId}-button`} |
| 609 |
aria-labelledby={`${randomId}-text ${randomId}-button`} |
| 610 |
icon={ |
| 611 |
<span className={css(styles.tabsToggleIcon)}> |
| 612 |
<RhMicronsCaretDownIcon /> |
| 613 |
</span> |
| 614 |
} |
| 615 |
> |
| 616 |
{toggleText && <span id={`${randomId}-text`}>{toggleText}</span>} |
| 617 |
</Button> |
| 618 |
</div> |
| 619 |
</div> |
| 620 |
)} |
| 621 |
</GenerateId> |
| 622 |
)} |
| 623 |
{renderScrollButtons && ( |
| 624 |
<div className={css(styles.tabsScrollButton)}> |
| 625 |
<Button |
| 626 |
aria-label={backScrollAriaLabel || leftScrollAriaLabel} |
| 627 |
onClick={this.scrollBack} |
| 628 |
isDisabled={disableBackScrollButton} |
| 629 |
aria-hidden={disableBackScrollButton} |
| 630 |
ref={this.leftScrollButtonRef} |
| 631 |
variant="plain" |
| 632 |
icon={<RhMicronsCaretLeftIcon />} |
| 633 |
/> |
| 634 |
</div> |
| 635 |
)} |
| 636 |
<ul |
| 637 |
aria-label={tabListAriaLabel} |
| 638 |
aria-labelledby={tabListAriaLabelledBy} |
| 639 |
className={css(styles.tabsList)} |
| 640 |
ref={this.tabList} |
| 641 |
onScroll={this.handleScrollButtons} |
| 642 |
role="tablist" |
| 643 |
> |
| 644 |
{isOverflowHorizontal ? filteredChildrenWithoutOverflow : filteredChildren} |
| 645 |
{hasOverflowTab && <OverflowTab overflowingTabs={overflowingTabProps} {...overflowObjectProps} />} |
| 646 |
</ul> |
| 647 |
{renderScrollButtons && ( |
| 648 |
<div className={css(styles.tabsScrollButton)}> |
| 649 |
<Button |
| 650 |
aria-label={forwardScrollAriaLabel || rightScrollAriaLabel} |
| 651 |
onClick={this.scrollForward} |
| 652 |
isDisabled={disableForwardScrollButton} |
| 653 |
aria-hidden={disableForwardScrollButton} |
| 654 |
variant="plain" |
| 655 |
icon={<RhMicronsCaretRightIcon />} |
| 656 |
/> |
| 657 |
</div> |
| 658 |
)} |
| 659 |
{onAdd !== undefined && ( |
| 660 |
<span className={css(styles.tabsAdd)}> |
| 661 |
<Button |
| 662 |
variant="plain" |
| 663 |
aria-label={addButtonAriaLabel || 'Add tab'} |
| 664 |
onClick={onAdd} |
| 665 |
icon={<PlusIcon />} |
| 666 |
isDisabled={isAddButtonDisabled} |
| 667 |
/> |
| 668 |
</span> |
| 669 |
)} |
| 670 |
</Component> |
| 671 |
{filteredChildren |
| 672 |
.filter( |
| 673 |
(child) => |
| 674 |
child.props.children && |
| 675 |
!(unmountOnExit && child.props.eventKey !== localActiveKey) && |
| 676 |
!(mountOnEnter && shownKeys.indexOf(child.props.eventKey) === -1) |
| 677 |
) |
| 678 |
.map((child) => ( |
| 679 |
<TabContent |
| 680 |
key={child.props.eventKey} |
| 681 |
activeKey={localActiveKey} |
| 682 |
child={child} |
| 683 |
id={child.props.id || uniqueId} |
| 684 |
ouiaId={child.props.ouiaId} |
| 685 |
/> |
| 686 |
))} |
| 687 |
</TabsContextProvider> |
| 688 |
); |
| 689 |
}} |
| 690 |
</SSRSafeIds> |
| 691 |
); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
export { Tabs }; |