Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): Conditionally render deprecation warning #3269

Merged
merged 6 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/plenty-drinks-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui-react": patch
---

fix(react): Conditionally render deprecation warning if Link component's `to` prop is used without using `as` prop
6 changes: 3 additions & 3 deletions packages/react/src/primitives/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { View } from '../View';
import { useDeprecationWarning } from '../../hooks/useDeprecationWarning';

const LinkPrimitive: Primitive<LinkProps, 'a'> = (
{ as = 'a', children, className, isExternal, to, ...rest },
{ as, children, className, isExternal, to, ...rest },
ref
) => {
useDeprecationWarning({
shouldWarn: to != null,
shouldWarn: to != null && !as,
message:
"The Link component's to prop will soon be deprecated. " +
'Please see the Amplify UI documentation for using the Link component with routing libraries: ' +
'https://ui.docs.amplify.aws/react/components/link#routing-libraries',
});
return (
<View
as={as}
as={as ?? 'a'}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a ternary here to be more explicit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

className={classNames(ComponentClassNames.Link, className)}
ref={ref}
rel={isExternal ? 'noopener noreferrer' : undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,17 @@ describe('Link: ', () => {
expect(screen.getByText(/you are on the about page/i)).toBeInTheDocument();
});

it('should call console.warn if "to" prop is used', async () => {
it('should call console.warn if "to" prop is used without "as" prop', async () => {
const spyWarn = jest.spyOn(console, 'warn');
render(<Link to="/test">Test</Link>);
expect(spyWarn).toHaveBeenCalled();
spyWarn.mockRestore();
});

it('should not call console.warn if "to" prop is used with "as" prop', async () => {
const spyWarn = jest.spyOn(console, 'warn');
render(<SampleRoutingApp />);
expect(spyWarn).not.toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

spyWarn.mockRestore();
});
});