Skip to content
Merged
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
12 changes: 9 additions & 3 deletions packages/react-core/src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createContext } from 'react';
import { createContext, forwardRef } from 'react';
import styles from '@patternfly/react-styles/css/components/Card/card';
import { css } from '@patternfly/react-styles';
import { useOUIAProps, OUIAProps } from '../../helpers';
Expand Down Expand Up @@ -42,6 +42,8 @@ export interface CardProps extends React.HTMLProps<HTMLElement>, OUIAProps {
ouiaId?: number | string;
/** 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. */
ouiaSafe?: boolean;
/** @hide Forwarded ref */
innerRef?: React.Ref<any>;
}

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

export const Card: React.FunctionComponent<CardProps> = ({
const CardBase: React.FunctionComponent<CardProps> = ({
children,
id = '',
className,
Expand All @@ -82,7 +84,7 @@ export const Card: React.FunctionComponent<CardProps> = ({
variant = 'default',
ouiaId,
ouiaSafe = true,

innerRef,
...props
}: CardProps) => {
const Component = component as any;
Expand Down Expand Up @@ -127,6 +129,7 @@ export const Card: React.FunctionComponent<CardProps> = ({
}}
>
<Component
ref={innerRef}
id={id}
className={css(
styles.card,
Expand All @@ -148,4 +151,7 @@ export const Card: React.FunctionComponent<CardProps> = ({
</CardContext.Provider>
);
};

export const Card = forwardRef((props: CardProps, ref: React.Ref<any>) => <CardBase {...props} innerRef={ref} />);

Card.displayName = 'Card';
11 changes: 11 additions & 0 deletions packages/react-core/src/components/Card/__tests__/Card.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import { Card } from '../Card';
import { createRef } from 'react';

describe('Card', () => {
test('renders with PatternFly Core styles', () => {
Expand Down Expand Up @@ -91,4 +92,14 @@ describe('Card', () => {
const card = screen.getByText('secondary card');
expect(card).toHaveClass('pf-m-secondary');
});

test('ref is added to the root element', () => {
const ref = createRef<HTMLTextAreaElement>();
render(
<Card ref={ref} tabIndex={-1}>
card
</Card>
);
expect(ref.current).toBe(screen.getByText('card'));
});
});
Loading