fix: Remove all links legacyBehavior (#25585)

* Remove all links legacyBehavior

* fix: resolve type errors in Button and Dropdown when using Link without legacyBehavior

- Button.tsx: Only pass ref to button element, not to Link (Link manages its own anchor)
- Dropdown.tsx: Strip ref from props when using Link to avoid type incompatibility

This fixes the type errors that were causing API V1 and V2 builds to fail.

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: only pass disabled and type props to button element, not to Link

Link component doesn't accept disabled or type props, so these should only be passed when rendering a button element.

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: strip ref from passThroughProps when rendering Link

The passThroughProps spread was including a ref property that's incompatible with Link's expected ref type. This strips the ref when rendering a Link component.

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: use type assertion for React.createElement props to handle union types

The Button component uses a union type for props that can be either Link or button props. TypeScript can't narrow the union type properly when using React.createElement with a dynamic element type, so we use a type assertion to cast the props to the correct type.

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: render Link and button separately to avoid type conflicts

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: preserve data-testid when rendering Button as Link

Co-Authored-By: Volnei Munhoz <[email protected]>

---------

Co-authored-by: Anik Dhabal Babu <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-12-21 12:40:27 +00:00
1 parent 067fa7e137
commit 19ac0139c1
8 files changed
+103 -100

No files matched your search

+34 -24
View File
@@ -239,25 +239,17 @@ export const Button = forwardRef<HTMLAnchorElement | HTMLButtonElement, ButtonPr
...passThroughProps
} = props;
// Buttons are **always** disabled if we're in a `loading` state
const disabled = props.disabled || loading;
// If pass an `href`-attr is passed it's `<a>`, otherwise it's a `<button />`
const disabled = props.disabled || loading || false;
// If pass an `href`-attr is passed it's Link, otherwise it's a `<button />`
const isLink = typeof props.href !== "undefined";
const elementType = isLink ? "a" : "button";
const element = React.createElement(
elementType,
{
...passThroughProps,
disabled,
type: !isLink ? type : undefined,
ref: forwardedRef,
className: classNames(buttonClasses({ color, size, loading, variant }), props.className),
// if we click a disabled button, we prevent going through the click handler
onClick: disabled
? (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
e.preventDefault();
}
: props.onClick,
},
const buttonClassName = classNames(buttonClasses({ color, size, loading, variant }), props.className);
const handleClick = disabled
? (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
e.preventDefault();
}
: props.onClick;
const buttonContent = (
<>
{CustomStartIcon ||
(StartIcon && (
@@ -334,18 +326,36 @@ export const Button = forwardRef<HTMLAnchorElement | HTMLButtonElement, ButtonPr
</>
);
return props.href ? (
<Link data-testid="link-component" passHref href={props.href} shallow={shallow && shallow} legacyBehavior>
{element}
</Link>
) : (
// Render Link or button separately to avoid type conflicts
// Link manages its own anchor element, so we don't pass ref to it
if (isLink) {
return (
<Link
{...(passThroughProps as Omit<JSX.IntrinsicElements["a"], "href" | "onClick" | "ref"> & LinkProps)}
shallow={shallow && shallow}
className={buttonClassName}
onClick={handleClick}>
{buttonContent}
</Link>
);
}
return (
<Wrapper
data-testid="wrapper"
tooltip={props.tooltip}
tooltipSide={tooltipSide}
tooltipOffset={tooltipOffset}
tooltipClassName={tooltipClassName}>
{element}
<button
{...(passThroughProps as Omit<JSX.IntrinsicElements["button"], "onClick" | "ref">)}
ref={forwardedRef as React.Ref<HTMLButtonElement>}
disabled={disabled}
type={type as "button" | "submit" | "reset"}
className={buttonClassName}
onClick={handleClick}>
{buttonContent}
</button>
</Wrapper>
);
});