-
Notifications
You must be signed in to change notification settings - Fork 235
feat: Add href to action card #4682
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
Open
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1a9b062
feat: Add href to action card
SpyZzey 55fca43
refactor: Dedupe WithNativeAttributes in ActionCard
SpyZzey 241c85a
chore: Update snapshots
SpyZzey 7526acd
test: Add click parameters test utils and improve tests
SpyZzey 0ea3505
chore: Update snapshots
SpyZzey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import * as React from 'react'; | ||
|
|
||
| import { ActionCard, Icon, SpaceBetween } from '~components'; | ||
|
|
||
| import { SimplePage } from '../app/templates'; | ||
|
|
||
| export default function ActionCardLinkPage() { | ||
| const [lastFollowed, setLastFollowed] = React.useState<string | null>(null); | ||
|
|
||
| return ( | ||
| <SimplePage title="Action Card link page" screenshotArea={{}}> | ||
| <SpaceBetween size="l"> | ||
| <div>Last followed: {lastFollowed ?? 'None'}</div> | ||
|
|
||
| <div style={{ maxInlineSize: '400px' }}> | ||
| <SpaceBetween size="m"> | ||
| <ActionCard | ||
| header={<b>Navigates with href</b>} | ||
| description="Renders as an anchor element" | ||
| href="#in-page" | ||
| icon={<Icon name="angle-right" />} | ||
| onFollow={event => { | ||
| event.preventDefault(); | ||
| setLastFollowed('Header card'); | ||
| }} | ||
| /> | ||
|
|
||
| <ActionCard | ||
| ariaLabel="Standalone link card" | ||
| href="#standalone" | ||
| icon={<Icon name="angle-right" />} | ||
| iconVerticalAlignment="center" | ||
| onFollow={event => { | ||
| event.preventDefault(); | ||
| setLastFollowed('Standalone card'); | ||
| }} | ||
| > | ||
| Standalone link card | ||
| </ActionCard> | ||
|
|
||
| <ActionCard | ||
| header={<b>External link (new tab)</b>} | ||
| description="Opens in a new tab" | ||
| href="https://cloudscape.design/" | ||
| target="_blank" | ||
| icon={<Icon name="external" />} | ||
| /> | ||
|
|
||
| <ActionCard | ||
| header={<b>Disabled link</b>} | ||
| description="href is removed when disabled" | ||
| href="#disabled" | ||
| disabled={true} | ||
| /> | ||
| </SpaceBetween> | ||
| </div> | ||
| </SpaceBetween> | ||
| </SimplePage> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,132 @@ describe('ActionCard Component', () => { | |
| }); | ||
| }); | ||
|
|
||
| describe('href', () => { | ||
| test('renders a button by default', () => { | ||
| const wrapper = renderActionCard({ header: 'Header' }); | ||
| expect(wrapper.getElement().querySelector('button')).toBeTruthy(); | ||
| expect(wrapper.getElement().querySelector('a')).toBeNull(); | ||
| }); | ||
|
|
||
| test('renders an anchor with href when href is provided', () => { | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test' }); | ||
| const anchor = wrapper.getElement().querySelector('a')!; | ||
| expect(anchor).toBeTruthy(); | ||
| expect(anchor).toHaveAttribute('href', '#test'); | ||
| expect(wrapper.getElement().querySelector('button')).toBeNull(); | ||
| }); | ||
|
|
||
| test('renders an anchor for the standalone (no header) variant', () => { | ||
| const wrapper = renderActionCard({ ariaLabel: 'Card', href: '#test' }); | ||
| const anchor = wrapper.getElement().querySelector('a')!; | ||
| expect(anchor).toHaveAttribute('href', '#test'); | ||
| expect(anchor).toHaveAttribute('aria-label', 'Card'); | ||
| }); | ||
|
|
||
| test('applies target and default rel for _blank', () => { | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', target: '_blank' }); | ||
| const anchor = wrapper.getElement().querySelector('a')!; | ||
| expect(anchor).toHaveAttribute('target', '_blank'); | ||
| expect(anchor).toHaveAttribute('rel', 'noopener noreferrer'); | ||
| }); | ||
|
|
||
| test('does not set rel by default when target is not _blank', () => { | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test' }); | ||
| expect(wrapper.getElement().querySelector('a')!).not.toHaveAttribute('rel'); | ||
| }); | ||
|
|
||
| test('custom rel overrides the default', () => { | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', target: '_blank', rel: 'nofollow' }); | ||
| expect(wrapper.getElement().querySelector('a')!).toHaveAttribute('rel', 'nofollow'); | ||
| }); | ||
|
|
||
| test('applies download attribute', () => { | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', download: 'file.txt' }); | ||
| expect(wrapper.getElement().querySelector('a')!).toHaveAttribute('download', 'file.txt'); | ||
| }); | ||
|
|
||
| test('removes href when disabled', () => { | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', disabled: true }); | ||
| const anchor = wrapper.getElement().querySelector('a')!; | ||
| expect(anchor).not.toHaveAttribute('href'); | ||
| expect(anchor).toHaveAttribute('aria-disabled', 'true'); | ||
| expect(wrapper.getElement().querySelector('button')).toBeNull(); | ||
| }); | ||
|
|
||
| test('disabled link stays focusable and announced as a link', () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it focusable when disabled?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It matches the current behavior in Action card with button tag. |
||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', disabled: true }); | ||
| const anchor = wrapper.getElement().querySelector('a')!; | ||
| expect(anchor).toHaveAttribute('role', 'link'); | ||
| anchor.focus(); | ||
| expect(document.activeElement).toBe(anchor); | ||
| }); | ||
| }); | ||
|
|
||
| describe('onFollow', () => { | ||
| test('fires onFollow with href and target when href is set', () => { | ||
| const onFollowSpy = jest.fn(); | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', target: '_blank', onFollow: onFollowSpy }); | ||
| wrapper.click(); | ||
| expect(onFollowSpy).toHaveBeenCalledTimes(1); | ||
| expect(onFollowSpy).toHaveBeenCalledWith( | ||
| expect.objectContaining({ detail: { href: '#test', target: '_blank' } }) | ||
| ); | ||
| }); | ||
|
|
||
| test('does not fire onFollow when no href is set', () => { | ||
| const onFollowSpy = jest.fn(); | ||
| const wrapper = renderActionCard({ header: 'Header', onFollow: onFollowSpy }); | ||
| wrapper.click(); | ||
| expect(onFollowSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('does not fire onFollow when disabled', () => { | ||
| const onFollowSpy = jest.fn(); | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', disabled: true, onFollow: onFollowSpy }); | ||
| wrapper.click(); | ||
| expect(onFollowSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('does not fire onFollow when a modifier key is pressed while clicking', () => { | ||
| const onClickSpy = jest.fn(); | ||
| const onFollowSpy = jest.fn(); | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', onClick: onClickSpy, onFollow: onFollowSpy }); | ||
| wrapper.click({ button: 0, ctrlKey: true }); | ||
| expect(onFollowSpy).not.toHaveBeenCalled(); | ||
| expect(onClickSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('fires onFollow for the standalone (no header) anchor variant', () => { | ||
| const onFollowSpy = jest.fn(); | ||
| const wrapper = renderActionCard({ ariaLabel: 'Card', href: '#test', target: '_blank', onFollow: onFollowSpy }); | ||
| wrapper.click(); | ||
| expect(onFollowSpy).toHaveBeenCalledTimes(1); | ||
| expect(onFollowSpy).toHaveBeenCalledWith( | ||
| expect.objectContaining({ detail: { href: '#test', target: '_blank' } }) | ||
| ); | ||
| }); | ||
|
|
||
| test('still fires onClick alongside onFollow', () => { | ||
| const onClickSpy = jest.fn(); | ||
| const onFollowSpy = jest.fn(); | ||
| const wrapper = renderActionCard({ header: 'Header', href: '#test', onClick: onClickSpy, onFollow: onFollowSpy }); | ||
| wrapper.click(); | ||
| expect(onClickSpy).toHaveBeenCalledTimes(1); | ||
| expect(onFollowSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('nativeAnchorAttributes', () => { | ||
| test('passes custom attributes to the anchor element', () => { | ||
| const wrapper = renderActionCard({ | ||
| header: 'Header', | ||
| href: '#test', | ||
| nativeAnchorAttributes: { 'data-testid': 'test-anchor' }, | ||
| }); | ||
| expect(wrapper.getElement().querySelector('a')!).toHaveAttribute('data-testid', 'test-anchor'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ariaLabel', () => { | ||
| test('root always has role=group', () => { | ||
| const withHeader = renderActionCard({ header: 'Header' }); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.