Skip to content

Commit bf5996a

Browse files
authored
Merge pull request #2310 from dxc-technology/PelayoFelgueroso/navTabs-anchor
Implement `as` functionalty to TabLink to avoid using an anchor while the `href` isn't passed
2 parents f913eaa + d0ad184 commit bf5996a

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

packages/lib/src/card/Card.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import styled from "@emotion/styled";
33
import { spaces } from "../common/variables";
44
import CardPropsType from "./types";
55

6-
const Card = styled.div<{
7-
hasAction: boolean;
8-
margin: CardPropsType["margin"];
9-
shadowDepth: 0 | 1 | 2;
10-
href?: string;
11-
}>`
6+
const Card = styled.div<
7+
{
8+
hasAction: boolean;
9+
margin: CardPropsType["margin"];
10+
shadowDepth: 0 | 1 | 2;
11+
} & React.AnchorHTMLAttributes<HTMLAnchorElement>
12+
>`
1213
display: flex;
1314
cursor: ${({ hasAction }) => (hasAction ? "pointer" : "unset")};
1415
outline: ${({ hasAction }) => !hasAction && "none"};

packages/lib/src/nav-tabs/Tab.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ const TabContainer = styled.div`
1313
flex-direction: column;
1414
`;
1515

16-
const TabLink = styled.a<{
17-
disabled: TabProps["disabled"];
18-
iconPosition: NavTabsPropsType["iconPosition"];
19-
}>`
16+
const TabLink = styled.div<
17+
{
18+
disabled: TabProps["disabled"];
19+
iconPosition: NavTabsPropsType["iconPosition"];
20+
} & React.AnchorHTMLAttributes<HTMLAnchorElement>
21+
>`
2022
box-sizing: border-box;
2123
display: flex;
2224
flex-direction: ${({ iconPosition }) => (iconPosition === "top" ? "column" : "row")};
@@ -96,11 +98,11 @@ const Tab = forwardRef(
9698
notificationNumber = false,
9799
...otherProps
98100
}: TabProps,
99-
ref: Ref<HTMLAnchorElement>
101+
ref: Ref<HTMLAnchorElement | HTMLDivElement>
100102
) => {
101103
const { iconPosition, tabIndex, focusedLabel } = useContext(NavTabsContext) ?? {};
102-
const tabRef = useRef<HTMLAnchorElement>();
103-
const innerRef = useRef<HTMLAnchorElement | null>(null);
104+
const tabRef = useRef<HTMLAnchorElement | HTMLDivElement | null>();
105+
const innerRef = useRef<HTMLAnchorElement | HTMLDivElement | null>(null);
104106
useImperativeHandle(ref, () => innerRef.current!, []);
105107

106108
useEffect(() => {
@@ -109,7 +111,7 @@ const Tab = forwardRef(
109111
}
110112
}, [children, focusedLabel]);
111113

112-
const handleOnKeyDown = (event: KeyboardEvent<HTMLAnchorElement>) => {
114+
const handleOnKeyDown = (event: KeyboardEvent<HTMLDivElement | HTMLAnchorElement>) => {
113115
switch (event.key) {
114116
case " ":
115117
case "Enter":
@@ -128,11 +130,12 @@ const Tab = forwardRef(
128130
aria-disabled={disabled}
129131
aria-selected={active}
130132
disabled={disabled}
133+
as={href ? "a" : onClick ? "button" : "div"}
131134
href={!disabled ? href : undefined}
135+
onClick={!disabled ? onClick : undefined}
132136
iconPosition={iconPosition}
133-
onClick={onClick}
134137
onKeyDown={handleOnKeyDown}
135-
ref={(anchorRef: HTMLAnchorElement) => {
138+
ref={(anchorRef: HTMLAnchorElement | HTMLDivElement | null) => {
136139
tabRef.current = anchorRef;
137140
if (ref) {
138141
if (typeof ref === "function") ref(anchorRef);

0 commit comments

Comments
 (0)