Skip to content

Commit

Permalink
Added "as" prop to Link (#15233)
Browse files Browse the repository at this point in the history
* feat: added as prop

* feat: added example

* fix: removed undefined from typescript

* fix: rollback change for className

* fix: added undefined

* feat: added as description

* fix: added control as false and updated snapshot
  • Loading branch information
guidari authored Nov 28, 2023
1 parent a8a5ff8 commit 9a489e4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4328,6 +4328,9 @@ Map {
"Link" => Object {
"$$typeof": Symbol(react.forward_ref),
"propTypes": Object {
"as": Object {
"type": "string",
},
"children": Object {
"type": "node",
},
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/components/Link/Link.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default {
disable: true,
},
},
as: {
control: false,
},
renderIcon: {
table: {
disable: true,
Expand Down
21 changes: 18 additions & 3 deletions packages/react/src/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import React, {
import { usePrefix } from '../../internal/usePrefix';

export interface LinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
/**
* Provide a custom element or component to render the top-level node for the
* component.
*/
as?: string | undefined;

/**
* @description Indicates the element that represents the
* current item within a container or set of related
Expand Down Expand Up @@ -70,6 +76,7 @@ export interface LinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
const Link = React.forwardRef<HTMLAnchorElement, PropsWithChildren<LinkProps>>(
function Link(
{
as: BaseComponent,
children,
className: customClassName,
href,
Expand All @@ -92,7 +99,7 @@ const Link = React.forwardRef<HTMLAnchorElement, PropsWithChildren<LinkProps>>(
});
const rel = target === '_blank' ? 'noopener' : undefined;
const linkProps: AnchorHTMLAttributes<HTMLAnchorElement> = {
className,
className: BaseComponent ? undefined : className,
rel,
target,
};
Expand All @@ -106,22 +113,30 @@ const Link = React.forwardRef<HTMLAnchorElement, PropsWithChildren<LinkProps>>(
linkProps['aria-disabled'] = true;
}

const BaseComponentAsAny = (BaseComponent ?? 'a') as any;

return (
<a ref={ref} {...linkProps} {...rest}>
<BaseComponentAsAny ref={ref} {...linkProps} {...rest}>
{children}
{!inline && Icon && (
<div className={`${prefix}--link__icon`}>
<Icon />
</div>
)}
</a>
</BaseComponentAsAny>
);
}
);

Link.displayName = 'Link';

Link.propTypes = {
/**
* Provide a custom element or component to render the top-level node for the
* component.
*/
as: PropTypes.string,

/**
* Provide the content for the Link
*/
Expand Down

0 comments on commit 9a489e4

Please sign in to comment.