Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
"peerDependencies": {
"@cfpb/cfpb-design-system": "5.3.2",
"react": "^19.2.4",
"react-dom": "^19.2.4",
"react-router": "^7.14.0"
"react-dom": "^19.2.4"
},
"devDependencies": {
"@cfpb/browserslist-config": "^0.0.6",
Expand Down
10 changes: 5 additions & 5 deletions src/components/Breadcrumb/breadcrumb.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('<Breadcrumb />', () => {
<Breadcrumb
crumbs={[
{
href: '/home',
to: '/home',
label: 'Home',
},
]}
Expand All @@ -32,8 +32,8 @@ describe('<Breadcrumb />', () => {
render(
<Breadcrumb
crumbs={[
{ href: '/home', label: 'Home' },
{ href: '/section', label: 'Section' },
{ to: '/home', label: 'Home' },
{ to: '/section', label: 'Section' },
]}
/>,
);
Expand All @@ -49,8 +49,8 @@ describe('<Breadcrumb />', () => {
render(
<Breadcrumb
crumbs={[
{ href: '/home', label: 'Home' },
{ href: '/current', label: 'Current', isCurrent: true },
{ to: '/home', label: 'Home' },
{ to: '/current', label: 'Current', isCurrent: true },
]}
/>,
);
Expand Down
9 changes: 5 additions & 4 deletions src/components/Breadcrumb/breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type { HTMLAttributes } from 'react';
import { Fragment } from 'react';
import './breadcrumb.scss';
import { JSXElement } from '../../types/jsx-element';
import Link from '../Link/link';

export interface BreadcrumbCrumb {
href: string;
to: string;
label: string;
isCurrent?: boolean;
}
Expand Down Expand Up @@ -34,16 +35,16 @@ export const Breadcrumb = ({
>
<nav className='m-breadcrumbs' aria-label={ariaLabel}>
{crumbs.map((crumb) => (
<Fragment key={`${crumb.href}-${crumb.label}`}>
<Fragment key={`${crumb.to}-${crumb.label}`}>
{` / `}
{crumb.isCurrent ? (
<span className='m-breadcrumbs__crumb' aria-current='page'>
{` ${crumb.label} `}
</span>
) : (
<a className='m-breadcrumbs__crumb' href={crumb.href}>
<Link className='m-breadcrumbs__crumb' to={crumb.to}>
{` ${crumb.label} `}
</a>
</Link>
)}
</Fragment>
))}
Expand Down
10 changes: 5 additions & 5 deletions src/components/Header/responsive-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function CfpbLogo({
return (
<Link
data-testid='CfpbLogoLink'
href={href}
to={href}
title='Home'
aria-label='Home'
className='o-header__logo'
Expand Down Expand Up @@ -140,18 +140,18 @@ export default function ResponsiveMenu({
);
}

export const ExampleLinks: ReactNode[] = [
<Link key='home' href='/' label='Home' />,
export const ExampleLinks: React.ReactNode[] = [
<Link key='home' to='/' label='Home' />,
<Link
key='filing'
className='nav-item active'
href='/filing'
to='/filing'
label='Filing'
/>,
<Link
key='profile'
className='nav-item profile'
href='/profile'
to='/profile'
label='John Sample'
/>,
<Button
Expand Down
20 changes: 20 additions & 0 deletions src/components/Link/base-link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ReactNode } from "react";
import type { JSXElement } from "../../types/jsx-element";

export interface BaseLinkProperties {
to: string | undefined;
children: ReactNode;
[key: string]: unknown;
}

export const BaseLink = ({
to,
children,
...others
}: BaseLinkProperties): JSXElement | null => {
return (
<a href={to} {...others}>
{children}
</a>
);
};
66 changes: 35 additions & 31 deletions src/components/Link/link.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import Link, { LinkText, ListLink } from './link';
import { DSRContext } from '../../context/dsr-context';
import { ReactNode } from 'react';
import type { JSXElement } from "../../types/jsx-element";

describe('<Link />', () => {
const linkBaseProperties = {
href: '/foo/bar',
to: '/foo/bar',
'data-testid': 'link-test-id',
label: 'some link',
};
const testId = linkBaseProperties['data-testid'];

interface CustomLinkProperties {
to: string | undefined;
children: ReactNode;
}

const CustomLinkComponent = ({
to,
children,
...others
}: CustomLinkProperties): JSXElement => {
return (
<a href={to} {...others} className='link-component-from-context'>
{children}
</a>
);
};

it('Type: "default"', () => {
render(<Link {...linkBaseProperties} />);
const link = screen.getByTestId(testId);
Expand Down Expand Up @@ -79,43 +98,28 @@ describe('<Link />', () => {
expect(link).toHaveAttribute('target', '_blank');
});

it('Option: isRouterLink - it renders a router link', () => {
it('Context: uses link component configured in context', () => {
render(
<MemoryRouter initialEntries={['/foo/bar']}>
<Link {...linkBaseProperties} isRouterLink />
</MemoryRouter>,
<DSRContext value={{LinkComponent:CustomLinkComponent}}>
<Link {...linkBaseProperties}>
<span data-testid='link-child'>Child</span>
</Link>
</DSRContext>,
);

const link = screen.getByRole('link', { name: /some link/i });
expect(link).toHaveAttribute('href', '/foo/bar');
expect(screen.getByTestId('link-test-id')).toBeInTheDocument();
expect(screen.getByTestId('link-test-id')).toHaveClass('link-component-from-context');
});

it('Option: isRouterLink - it renders children and icons', async () => {
it('Context: uses base link component by default', () => {
render(
<MemoryRouter initialEntries={['/foo/bar']}>
<Link {...linkBaseProperties} isRouterLink iconLeft='left'>
<Link {...linkBaseProperties}>
<span data-testid='link-child'>Child</span>
</Link>
</MemoryRouter>,
</Link>,
);

const link = screen.getByRole('link', { name: /some link/i });
expect(link).toHaveClass('a-link');
expect(screen.getByTestId('link-child')).toBeInTheDocument();
expect(screen.getByText('some link')).toHaveClass('a-link__text');
expect(await screen.findByTestId('link-icon-left')).toBeInTheDocument();
expect(screen.getByTestId('link-test-id')).toBeInTheDocument();
expect(screen.getByTestId('link-test-id')).not.toHaveClass('link-component-from-context');
});

it('Option: isRouterLink - it requires href', () => {
const brokenProperties = {
...linkBaseProperties,
href: undefined as unknown as string,
};

expect(() => render(<Link {...brokenProperties} isRouterLink />)).toThrow(
'Link component: href is a required attribute when isRouterLink is true',
);
});
});

describe('<LinkText>', () => {
Expand All @@ -131,7 +135,7 @@ describe('<ListLink>', () => {
const testId = 'list-link';

it('includes all expected elements', () => {
render(<ListLink data-testid={testId} href='/foo/bar' label='Test text' />);
render(<ListLink data-testid={testId} to='/foo/bar' label='Test text' />);
// ListItem
const listItem = screen.getByRole('listitem');
expect(listItem).toBeInTheDocument();
Expand Down
43 changes: 8 additions & 35 deletions src/components/Link/link.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { JSX } from 'react';
import { Link as RouterLink } from 'react-router';
import type { JSXElement } from '../../types/jsx-element';

import classnames from 'classnames';
import type { HTMLProps, ReactNode, Ref } from 'react';
import { useDSRContext } from '../../context/dsr-context';
import { Icon } from '../Icon/icon';
import ListItem from '../List/list-item';
import './link.scss';
Expand All @@ -20,7 +19,7 @@ export interface LinkProperties extends HTMLProps<HTMLAnchorElement> {
/**
* The link's destination URL.
*/
href: string;
to: string | undefined;
/**
* Name of icon to display left of link text
*/
Expand All @@ -33,10 +32,6 @@ export interface LinkProperties extends HTMLProps<HTMLAnchorElement> {
* Whether the link is a standalone link
*/
isJump?: boolean;
/**
* Whether the link is a react router link
*/
isRouterLink?: boolean;
/**
* The link's text content, not required if children are provided
*/
Expand All @@ -57,11 +52,10 @@ export interface LinkProperties extends HTMLProps<HTMLAnchorElement> {
export default function Link({
isButton = false,
children,
href,
to,
iconLeft,
iconRight,
isJump = false,
isRouterLink = false,
label,
type = 'default',
...others
Expand All @@ -80,47 +74,26 @@ export default function Link({
'a-link': shouldUseLinkStyles,
});

const { LinkComponent } = useDSRContext();

if (hasLeftIcon && hasRightIcon) {
throw new Error(
'Link component: only one of iconLeft or iconRight can be provided',
);
}

if (isRouterLink) {
if (!href) {
throw new Error(
'Link component: href is a required attribute when isRouterLink is true',
);
}
return (
<RouterLink to={href} {...others} className={cname}>
{children}
{!!iconLeft && (
<Icon name={iconLeft} isPresentational data-testid='link-icon-left' />
)}
{labelNode}
{!!iconRight && (
<Icon
name={iconRight}
isPresentational
data-testid='link-icon-right'
/>
)}
</RouterLink>
);
}

return (
<a {...others} className={cname} href={href}>
<LinkComponent {...others} className={cname} to={to}>
{children}

{!!iconLeft && (
<Icon name={iconLeft} isPresentational data-testid='link-icon-left' />
)}
{labelNode}
{!!iconRight && (
<Icon name={iconRight} isPresentational data-testid='link-icon-right' />
)}
</a>
</LinkComponent>
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/components/SecondaryNav/secondary-nav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type { SecondaryNavItem } from './secondary-nav';

describe('<SecondaryNav />', () => {
const defaultItems: SecondaryNavItem[] = [
{ href: '/a', label: 'Link A' },
{ href: '/b', label: 'Link B', isActive: true },
{ href: '/c', label: 'Link C' },
{ to: '/a', label: 'Link A' },
{ to: '/b', label: 'Link B', isActive: true },
{ to: '/c', label: 'Link C' },
];

it('renders a nav with the default aria-label', () => {
Expand Down Expand Up @@ -62,8 +62,8 @@ describe('<SecondaryNav />', () => {
label: 'Parent',
isActive: true,
children: [
{ href: '/child-a', label: 'Child A', isActive: true },
{ href: '/child-b', label: 'Child B' },
{ to: '/child-a', label: 'Child A', isActive: true },
{ to: '/child-b', label: 'Child B' },
],
},
];
Expand Down
14 changes: 7 additions & 7 deletions src/components/SecondaryNav/secondary-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import Link from '../Link/link';
import './secondary-nav.scss';

export interface SecondaryNavChildItem {
href: string;
to: string;
label: string;
isActive?: boolean;
}

export interface SecondaryNavItem {
href?: string;
to?: string;
label: string;
/**
* Whether this item is the current page. Ignored when the item has children;
Expand Down Expand Up @@ -61,13 +61,13 @@ export const SecondaryNav = ({

return (
<li
key={item.href ?? item.label}
key={item.to ?? item.label}
className='o-secondary-nav__item'
data-nav-is-active={sectionHasActive ? 'true' : undefined}
>
{item.href ? (
{item.to ? (
<Link
href={item.href}
to={item.to}
className={classnames(
'o-secondary-nav__link o-secondary-nav__link--parent',
parentIsActive && 'o-secondary-nav__link--current',
Expand All @@ -88,9 +88,9 @@ export const SecondaryNav = ({
{item.children && item.children.length > 0 ? (
<ul className='o-secondary-nav__list o-secondary-nav__list--children'>
{item.children.map((child) => (
<li key={child.href} className='o-secondary-nav__item'>
<li key={child.to} className='o-secondary-nav__item'>
<Link
href={child.href}
to={child.to}
className={classnames(
'o-secondary-nav__link',
child.isActive && 'o-secondary-nav__link--current',
Expand Down
Loading
Loading