Skip to content

Commit 3a6401f

Browse files
authored
Merge pull request #981 from logonoff/onexpand
feat(multicontentcard): add callback passing to onExpand
2 parents 4e055ac + 12801f7 commit 3a6401f

3 files changed

Lines changed: 68 additions & 41 deletions

File tree

packages/module/src/MultiContentCard/MultiContentCard.test.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Ref } from 'react';
2-
import { screen, render } from '@testing-library/react';
2+
import { screen, render, act } from '@testing-library/react';
33
import { Button, Card, CardHeader, CardBody, Content, ContentVariants, Icon, List, ListItem, CardFooter, Dropdown, MenuToggle, DropdownList, DropdownItem, MenuToggleElement } from '@patternfly/react-core';
44
import { ArrowRightIcon, BellIcon, CogIcon, EllipsisVIcon, LockIcon } from '@patternfly/react-icons';
55
import MultiContentCard, { MultiContentCardDividerVariant } from './MultiContentCard';
@@ -126,6 +126,29 @@ describe('MultiContentCard component', () => {
126126
expect(container.firstChild).toMatchSnapshot();
127127
});
128128

129+
it('should call onExpand when expanding the multi-content card', () => {
130+
const onExpand = jest.fn();
131+
132+
render(
133+
<MultiContentCard
134+
isExpandable
135+
defaultExpanded={false}
136+
toggleText='Expandable card toggle text'
137+
cards={cards}
138+
onExpand={onExpand}
139+
/>
140+
);
141+
expect(screen.queryByText('Getting Started')).toBe(null);
142+
143+
act(() => {
144+
screen.getByRole('button', { name: 'Details' }).click();
145+
});
146+
147+
expect(onExpand).toHaveBeenCalled();
148+
149+
expect(screen.getByText('Getting Started')).toBeInTheDocument();
150+
});
151+
129152
it('should render expandable multi-content card - with actions', () => {
130153
const { container } = render(
131154
<MultiContentCard

packages/module/src/MultiContentCard/MultiContentCard.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface MultiContentCardProps extends Omit<CardProps, 'children' | 'tit
4040
withDividers?: boolean;
4141
/** Indicates whether the card is expandable */
4242
isExpandable?: boolean;
43+
/** Callback expandable card */
44+
onExpand?: () => void;
4345
/** Indicates whether the card is expanded by default */
4446
defaultExpanded?: boolean;
4547
/** Indicates whether the actions toggle is right aligned */
@@ -67,13 +69,15 @@ const MultiContentCard: FunctionComponent<MultiContentCardProps> = ({
6769
withDividers = false,
6870
isExpandable = false,
6971
defaultExpanded = true,
72+
onExpand,
7073
ouiaId = 'MultiContentCard',
7174
...props
7275
}: MultiContentCardProps) => {
7376
const [ isExpanded, setIsExpanded ] = useState(defaultExpanded);
7477
const classes = useStyles();
75-
const onExpand = () => {
78+
const onExpandChange = () => {
7679
setIsExpanded(!isExpanded);
80+
onExpand && onExpand();
7781
};
7882

7983
const renderCards = (cards: (ReactElement | MutliContentCardProps)[], withDividers?: boolean) => (
@@ -105,7 +109,7 @@ const MultiContentCard: FunctionComponent<MultiContentCardProps> = ({
105109
{isExpandable && (
106110
<CardHeader
107111
data-ouia-component-id={`${ouiaId}-header`}
108-
onExpand={onExpand}
112+
onExpand={onExpandChange}
109113
isToggleRightAligned={isToggleRightAligned}
110114
toggleButtonProps={{
111115
'aria-label': 'Details',

0 commit comments

Comments
 (0)