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

feat(thumbnail-card-details): add keydown callback and default tabindex #3782

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d1d2e97
feat(thumbnail-card-details): add keydown callback
jmcbgaston Dec 10, 2024
7db5350
feat(thumbnail-card-details): optionalize onKeyDownCallback
jmcbgaston Dec 10, 2024
bf259ab
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 16, 2024
bbbfcd7
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 17, 2024
75ac51e
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 17, 2024
3442c8c
feat(thumbnail-card-details): spec update
jmcbgaston Dec 17, 2024
1f98580
feat(thumbnail-card-details): convert spec to RTL
jmcbgaston Dec 17, 2024
d3ac776
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 17, 2024
42ac602
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 18, 2024
99a2fd0
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 19, 2024
7aa7e5e
feat(thumbnail-card-details): update specs
jmcbgaston Dec 19, 2024
08a5a96
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 20, 2024
1f1cdfc
feat(thumbnail-card-details): rename prop
jmcbgaston Dec 20, 2024
2e31b23
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 20, 2024
6d044f7
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 23, 2024
b3eed64
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Dec 27, 2024
bba98c1
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Jan 2, 2025
725fa52
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Jan 6, 2025
7d02199
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Jan 6, 2025
05206a1
Merge branch 'master' into update-thumbnail-card-details-with-callback
jmcbgaston Jan 6, 2025
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
14 changes: 11 additions & 3 deletions src/components/thumbnail-card/ThumbnailCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Props = {
className?: string,
highlightOnHover?: boolean,
icon?: React.Node,
onKeyDownCallback?: () => void,
subtitle?: React.Node,
thumbnail: React.Node,
title: React.Node,
Expand All @@ -20,11 +21,12 @@ type Props = {
const ThumbnailCard = ({
actionItem,
className = '',
icon,
highlightOnHover = false,
icon,
onKeyDownCallback,
subtitle,
title,
thumbnail,
title,
...rest
}: Props) => (
<div
Expand All @@ -34,7 +36,13 @@ const ThumbnailCard = ({
{...rest}
>
<ThumbnailCardThumbnail thumbnail={thumbnail} />
<ThumbnailCardDetails actionItem={actionItem} icon={icon} subtitle={subtitle} title={title} />
<ThumbnailCardDetails
greg-in-a-box marked this conversation as resolved.
Show resolved Hide resolved
actionItem={actionItem}
icon={icon}
onKeyDownCallback={onKeyDownCallback}
subtitle={subtitle}
title={title}
/>
</div>
);

Expand Down
12 changes: 7 additions & 5 deletions src/components/thumbnail-card/ThumbnailCardDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,36 @@ import { useIsContentOverflowed } from '../../utils/dom';
type Props = {
actionItem?: React.Element<any>,
icon?: React.Node,
onKeyDownCallback?: () => void,
subtitle?: React.Node,
title: React.Node,
};

type TitleProps = {
onKeyDownCallback?: () => void,
title: React.Node,
};

const Title = ({ title }: TitleProps) => {
// $FlowFixMe
const Title = ({ title, onKeyDownCallback }: TitleProps) => {
const textRef: { current: null | HTMLElement } = React.useRef(null);
const isTextOverflowed = useIsContentOverflowed(textRef);

return (
<Tooltip isDisabled={!isTextOverflowed} text={title}>
<div ref={textRef} className="thumbnail-card-title">
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex, jsx-a11y/no-static-element-interactions */}
<div ref={textRef} role="link" className="thumbnail-card-title" tabIndex={0} onKeyDown={onKeyDownCallback}>
jmcbgaston marked this conversation as resolved.
Show resolved Hide resolved
{title}
</div>
</Tooltip>
);
};

const ThumbnailCardDetails = ({ actionItem, icon, subtitle, title }: Props) => (
const ThumbnailCardDetails = ({ actionItem, icon, subtitle, title, onKeyDownCallback }: Props) => (
<div className="thumbnail-card-details">
{icon}
<div className="thumbnail-card-details-content">
<div className="ThumbnailCardDetails-bodyText">
<Title title={title} />
<Title title={title} onKeyDownCallback={onKeyDownCallback} />
{subtitle && <div className="thumbnail-card-subtitle">{subtitle}</div>}
</div>
{actionItem}
Expand Down
jmcbgaston marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// @flow
import * as React from 'react';

import { mount, shallow } from 'enzyme';
import { fireEvent, render, screen } from '@testing-library/react';
jmcbgaston marked this conversation as resolved.
Show resolved Hide resolved

import * as libDom from '../../../utils/dom';

import ThumbnailCardDetails from '../ThumbnailCardDetails';

const getWrapper = (props = {}) => shallow(<ThumbnailCardDetails title={<div>Foo Bar!</div>} {...props} />);
const getWrapper = (props = {}) => render(<ThumbnailCardDetails title={<div>Foo Bar!</div>} {...props} />);
jmcbgaston marked this conversation as resolved.
Show resolved Hide resolved

jest.mock('../../../utils/dom', () => ({ useIsContentOverflowed: jest.fn() }));

Expand Down Expand Up @@ -45,9 +44,22 @@ describe('components/thumbnail-card/ThumbnailCardDetails', () => {

test('should render a Tooltip if text is overflowed', () => {
libDom.useIsContentOverflowed.mockReturnValue(true);
const { container } = getWrapper();
const title = container.querySelector('.thumbnail-card-title');

fireEvent.focus(title);
jmcbgaston marked this conversation as resolved.
Show resolved Hide resolved
const tooltip = screen.getByRole('tooltip');

expect(tooltip).toBeInTheDocument();
});

test('should accept a keydown callback', () => {
const someFunction = jest.fn();
const { container } = getWrapper({ onKeyDownCallback: someFunction });
const title = container.querySelector('.thumbnail-card-title');

const wrapper = mount(<ThumbnailCardDetails title={<div>Foo Bar!</div>} />);
fireEvent.keyDown(title, { key: 'Enter' });
jmcbgaston marked this conversation as resolved.
Show resolved Hide resolved

expect(wrapper.find('Tooltip').length).toBe(1);
expect(someFunction).toHaveBeenCalled();
});
});
Loading
Loading