-
Notifications
You must be signed in to change notification settings - Fork 392
fix(tabs): honor hash-linked nav tabs #12303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tarunvashishth
wants to merge
3
commits into
patternfly:main
Choose a base branch
from
tarunvashishth:tabs-should-work-with-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+285
−25
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,25 @@ const variantStyle = { | |
| secondary: styles.modifiers.secondary | ||
| }; | ||
|
|
||
| const getHashFromHref = (href?: string) => { | ||
| const hashIndex = href?.indexOf('#') ?? -1; | ||
|
|
||
| return hashIndex >= 0 ? href.slice(hashIndex) : undefined; | ||
| }; | ||
|
|
||
| const getTabHashActiveKey = ({ children, component, isNav }: Pick<TabsProps, 'children' | 'component' | 'isNav'>) => { | ||
| if (!canUseDOM || !(component === TabsComponent.nav || isNav) || !window.location.hash) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return Children.toArray(children) | ||
| .filter((child): child is TabElement => isValidElement(child)) | ||
| .filter(({ props }) => !props.isHidden) | ||
| .find( | ||
| ({ props }) => !props.isDisabled && !props.isAriaDisabled && getHashFromHref(props.href) === window.location.hash | ||
| )?.props.eventKey; | ||
| }; | ||
|
|
||
| interface TabsState { | ||
| /** Used to signal if the scroll buttons should be used */ | ||
| enableScrollButtons: boolean; | ||
|
|
@@ -152,7 +171,8 @@ interface TabsState { | |
| disableBackScrollButton: boolean; | ||
| disableForwardScrollButton: boolean; | ||
| shownKeys: (string | number)[]; | ||
| uncontrolledActiveKey: number | string; | ||
| uncontrolledActiveKey: number | string | undefined; | ||
| initialActiveKey: number | string | undefined; | ||
| uncontrolledIsExpandedLocal: boolean; | ||
| overflowingTabCount: number; | ||
| isInitializingAccent: boolean; | ||
|
|
@@ -167,14 +187,20 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| private direction = 'ltr'; | ||
| constructor(props: TabsProps) { | ||
| super(props); | ||
| const hashActiveKey = getTabHashActiveKey(props); | ||
|
|
||
| this.state = { | ||
| enableScrollButtons: false, | ||
| showScrollButtons: false, | ||
| renderScrollButtons: false, | ||
| disableBackScrollButton: true, | ||
| disableForwardScrollButton: true, | ||
| shownKeys: this.props.defaultActiveKey !== undefined ? [this.props.defaultActiveKey] : [this.props.activeKey], // only for mountOnEnter case | ||
| uncontrolledActiveKey: this.props.defaultActiveKey, | ||
| shownKeys: | ||
| this.props.defaultActiveKey !== undefined | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is a little nested, Not a blocker overall. |
||
| ? [hashActiveKey ?? this.props.defaultActiveKey] | ||
| : [hashActiveKey ?? this.props.activeKey], // only for mountOnEnter case | ||
| uncontrolledActiveKey: hashActiveKey ?? this.props.defaultActiveKey, | ||
| initialActiveKey: this.props.defaultActiveKey === undefined ? hashActiveKey : undefined, | ||
| uncontrolledIsExpandedLocal: this.props.defaultIsExpanded, | ||
| overflowingTabCount: 0, | ||
| isInitializingAccent: true, | ||
|
|
@@ -221,14 +247,26 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| eventKey: number | string, | ||
| tabContentRef: React.RefObject<any> | ||
| ) { | ||
| const { shownKeys } = this.state; | ||
| const { onSelect, defaultActiveKey } = this.props; | ||
| const { shownKeys, initialActiveKey } = this.state; | ||
| const { onSelect, defaultActiveKey, component, isNav } = this.props; | ||
|
|
||
| if (event.currentTarget instanceof HTMLAnchorElement && event.currentTarget.hasAttribute('href')) { | ||
| event.preventDefault(); | ||
|
|
||
| if (canUseDOM && (component === TabsComponent.nav || isNav)) { | ||
| window.history.pushState(null, '', event.currentTarget.href); | ||
| } | ||
| } | ||
|
|
||
| // if defaultActiveKey Tabs are uncontrolled, set new active key internally | ||
| if (defaultActiveKey !== undefined) { | ||
| this.setState({ | ||
| uncontrolledActiveKey: eventKey | ||
| }); | ||
| } else { | ||
| if (initialActiveKey !== undefined) { | ||
| this.setState({ initialActiveKey: undefined }); | ||
| } | ||
| onSelect(event, eventKey); | ||
| } | ||
|
|
||
|
|
@@ -401,8 +439,9 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| componentDidUpdate(prevProps: TabsProps, prevState: TabsState) { | ||
| this.direction = getLanguageDirection(this.tabList.current); | ||
| const { activeKey, mountOnEnter, isOverflowHorizontal, children, defaultActiveKey } = this.props; | ||
| const { shownKeys, overflowingTabCount, enableScrollButtons, uncontrolledActiveKey } = this.state; | ||
| const { shownKeys, overflowingTabCount, enableScrollButtons, uncontrolledActiveKey, initialActiveKey } = this.state; | ||
| const isOnCloseUpdate = !!prevProps.onClose !== !!this.props.onClose; | ||
|
|
||
| if ( | ||
| (defaultActiveKey !== undefined && prevState.uncontrolledActiveKey !== uncontrolledActiveKey) || | ||
| (defaultActiveKey === undefined && prevProps.activeKey !== activeKey) || | ||
|
|
@@ -417,6 +456,10 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| }); | ||
| } | ||
|
|
||
| if (defaultActiveKey === undefined && prevProps.activeKey !== activeKey && initialActiveKey !== undefined) { | ||
| this.setState({ initialActiveKey: undefined }); | ||
| } | ||
|
|
||
| if ( | ||
| prevProps.children && | ||
| children && | ||
|
|
@@ -517,6 +560,7 @@ class Tabs extends Component<TabsProps, TabsState> { | |
| disableForwardScrollButton, | ||
| shownKeys, | ||
| uncontrolledActiveKey, | ||
| initialActiveKey, | ||
| uncontrolledIsExpandedLocal, | ||
| overflowingTabCount, | ||
| isInitializingAccent, | ||
|
|
@@ -533,7 +577,7 @@ class Tabs extends Component<TabsProps, TabsState> { | |
|
|
||
| const defaultComponent = isNav && !component ? 'nav' : 'div'; | ||
| const Component: any = component !== undefined ? component : defaultComponent; | ||
| const localActiveKey = defaultActiveKey !== undefined ? uncontrolledActiveKey : activeKey; | ||
| const localActiveKey = defaultActiveKey !== undefined ? uncontrolledActiveKey : (initialActiveKey ?? activeKey); | ||
|
|
||
| const isExpandedLocal = defaultIsExpanded !== undefined ? uncontrolledIsExpandedLocal : isExpanded; | ||
| /* Uncontrolled expandable tabs */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
🏁 Script executed:
Repository: patternfly/patternfly-react
Length of output: 53
Add test coverage for the
getTabHashActiveKeyfunction with hash-based navigation.The new
getTabHashActiveKeyfunction accesseswindow.location.hashand should be tested to ensure the hash-based tab selection works correctly and prevent regressions. Currently, no hash-related test setup exists in the Tabs tests, while the Nav component tests demonstrate the pattern withwindow.location.hashsetup in abeforeEachblock.🤖 Prompt for AI Agents