Skip to content

Commit d143a43

Browse files
committed
fix(DrawerCloseButton): Allow props spread to button
Props were previously only spread to parent div. This allows for props spread to button. Enables patternfly/chatbot#834
1 parent 4d61988 commit d143a43

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import styles from '@patternfly/react-styles/css/components/Drawer/drawer';
22
import { css } from '@patternfly/react-styles';
3-
import { Button } from '../Button';
3+
import { Button, ButtonProps } from '../Button';
44
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon';
55

66
export interface DrawerCloseButtonProps extends React.HTMLProps<HTMLDivElement> {
@@ -10,16 +10,19 @@ export interface DrawerCloseButtonProps extends React.HTMLProps<HTMLDivElement>
1010
onClose?: () => void;
1111
/** Accessible label for the drawer close button */
1212
'aria-label'?: string;
13+
/** Additional properties spread to the close button */
14+
buttonProps?: Omit<ButtonProps, 'onClick'>;
1315
}
1416

1517
export const DrawerCloseButton: React.FunctionComponent<DrawerCloseButtonProps> = ({
1618
className = '',
1719
onClose = () => undefined as any,
1820
'aria-label': ariaLabel = 'Close drawer panel',
21+
buttonProps,
1922
...props
2023
}: DrawerCloseButtonProps) => (
2124
<div className={css(styles.drawerClose, className)} {...props}>
22-
<Button variant="plain" onClick={onClose} aria-label={ariaLabel} icon={<RhMicronsCloseIcon />} />
25+
<Button variant="plain" onClick={onClose} aria-label={ariaLabel} icon={<RhMicronsCloseIcon />} {...buttonProps} />
2326
</div>
2427
);
2528
DrawerCloseButton.displayName = 'DrawerCloseButton';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { render, screen } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import { ButtonProps } from '../../Button';
4+
import { DrawerCloseButton } from '../DrawerCloseButton';
5+
6+
test('Renders with spread buttonProps', () => {
7+
render(<DrawerCloseButton buttonProps={{ isDisabled: true }} />);
8+
expect(screen.getByRole('button')).toHaveAttribute('disabled');
9+
});
10+
11+
test('Calls onClose when clicked', async () => {
12+
const onClose = jest.fn();
13+
const user = userEvent.setup();
14+
15+
render(<DrawerCloseButton onClose={onClose} buttonProps={{ isDisabled: false }} />);
16+
await user.click(screen.getByRole('button'));
17+
expect(onClose).toHaveBeenCalledTimes(1);
18+
});
19+
20+
test('Does not spread onClick from buttonProps but spreads other props', async () => {
21+
const onClose = jest.fn();
22+
const buttonOnClick = jest.fn();
23+
const user = userEvent.setup();
24+
25+
render(
26+
<DrawerCloseButton
27+
onClose={onClose}
28+
buttonProps={{ id: 'drawer-close-button', onClick: buttonOnClick } as ButtonProps}
29+
/>
30+
);
31+
32+
const button = screen.getByRole('button');
33+
expect(button).toHaveAttribute('id', 'drawer-close-button');
34+
35+
await user.click(button);
36+
37+
expect(onClose).toHaveBeenCalledTimes(1);
38+
expect(buttonOnClick).not.toHaveBeenCalled();
39+
});

0 commit comments

Comments
 (0)