UI: Fix tab href race condition (#78458)

prevent tab clicking from activating the anchor's href

This prevents a very rare race condition when switching tabs very fast or when switching between tabs that update shortly after initial render.
This commit is contained in:
Gilles De Mey
2023-11-21 13:01:29 +01:00
committed by GitHub
parent 670291e8a8
commit 020b1adf79
2 changed files with 17 additions and 1 deletions

View File

@@ -39,6 +39,14 @@ export const Tab = React.forwardRef<HTMLAnchorElement, TabProps>(
const linkClass = cx(tabsStyles.link, active ? tabsStyles.activeStyle : tabsStyles.notActive);
// wraps the onClick handler with "event.preventDefault" prevents a rare race condition between navigating to the "href" and "onClick" handler
const onClickHandler = onChangeTab
? (event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
event.preventDefault();
onChangeTab(event);
}
: () => {};
return (
<div className={tabsStyles.item}>
<a
@@ -46,7 +54,7 @@ export const Tab = React.forwardRef<HTMLAnchorElement, TabProps>(
href={href ? href : '#'}
className={linkClass}
{...otherProps}
onClick={onChangeTab}
onClick={onClickHandler}
aria-label={otherProps['aria-label'] || selectors.components.Tab.title(label)}
role="tab"
aria-selected={active}

View File

@@ -51,6 +51,14 @@ export const Simple: Story = () => {
);
};
export const withHref: Story = () => (
<DashboardStoryCanvas>
<TabsBar>
<Tab label={'to Google'} active={false} href="https://google.com/" />
</TabsBar>
</DashboardStoryCanvas>
);
export const Counter: Story<CounterProps> = (args) => {
return <TabCounter {...args} />;
};