Skip to content

Commit deec405

Browse files
fix(Card): Add ref forwarding (#11879)
We need to add ref forwarding to card in order to fix patternfly/chatbot#417 in the ChatBot extension. Sometimes the feedback cards need to be focused, and they don't always have an inner focusable element.
1 parent 2e26d99 commit deec405

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/react-core/src/components/Card/Card.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext } from 'react';
1+
import { createContext, forwardRef } from 'react';
22
import styles from '@patternfly/react-styles/css/components/Card/card';
33
import { css } from '@patternfly/react-styles';
44
import { useOUIAProps, OUIAProps } from '../../helpers';
@@ -42,6 +42,8 @@ export interface CardProps extends React.HTMLProps<HTMLElement>, OUIAProps {
4242
ouiaId?: number | string;
4343
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
4444
ouiaSafe?: boolean;
45+
/** @hide Forwarded ref */
46+
innerRef?: React.Ref<any>;
4547
}
4648

4749
interface CardContextProps {
@@ -64,7 +66,7 @@ export const CardContext = createContext<Partial<CardContextProps>>({
6466
isDisabled: false
6567
});
6668

67-
export const Card: React.FunctionComponent<CardProps> = ({
69+
const CardBase: React.FunctionComponent<CardProps> = ({
6870
children,
6971
id = '',
7072
className,
@@ -82,7 +84,7 @@ export const Card: React.FunctionComponent<CardProps> = ({
8284
variant = 'default',
8385
ouiaId,
8486
ouiaSafe = true,
85-
87+
innerRef,
8688
...props
8789
}: CardProps) => {
8890
const Component = component as any;
@@ -127,6 +129,7 @@ export const Card: React.FunctionComponent<CardProps> = ({
127129
}}
128130
>
129131
<Component
132+
ref={innerRef}
130133
id={id}
131134
className={css(
132135
styles.card,
@@ -148,4 +151,7 @@ export const Card: React.FunctionComponent<CardProps> = ({
148151
</CardContext.Provider>
149152
);
150153
};
154+
155+
export const Card = forwardRef((props: CardProps, ref: React.Ref<any>) => <CardBase {...props} innerRef={ref} />);
156+
151157
Card.displayName = 'Card';

packages/react-core/src/components/Card/__tests__/Card.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { render, screen } from '@testing-library/react';
22
import '@testing-library/jest-dom';
33

44
import { Card } from '../Card';
5+
import { createRef } from 'react';
56

67
describe('Card', () => {
78
test('renders with PatternFly Core styles', () => {
@@ -91,4 +92,14 @@ describe('Card', () => {
9192
const card = screen.getByText('secondary card');
9293
expect(card).toHaveClass('pf-m-secondary');
9394
});
95+
96+
test('ref is added to the root element', () => {
97+
const ref = createRef<HTMLTextAreaElement>();
98+
render(
99+
<Card ref={ref} tabIndex={-1}>
100+
card
101+
</Card>
102+
);
103+
expect(ref.current).toBe(screen.getByText('card'));
104+
});
94105
});

0 commit comments

Comments
 (0)