Skip to content

Commit d44e1df

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 Assisted-by: Cursor
1 parent 4d61988 commit d44e1df

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

Lines changed: 20 additions & 6 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,30 @@ 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
20-
}: DrawerCloseButtonProps) => (
21-
<div className={css(styles.drawerClose, className)} {...props}>
22-
<Button variant="plain" onClick={onClose} aria-label={ariaLabel} icon={<RhMicronsCloseIcon />} />
23-
</div>
24-
);
23+
}: DrawerCloseButtonProps) => {
24+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
25+
const { onClick: _onClick, ...restButtonProps } = (buttonProps ?? {}) as ButtonProps;
26+
27+
return (
28+
<div className={css(styles.drawerClose, className)} {...props}>
29+
<Button
30+
variant="plain"
31+
onClick={onClose}
32+
aria-label={ariaLabel}
33+
icon={<RhMicronsCloseIcon />}
34+
{...restButtonProps}
35+
/>
36+
</div>
37+
);
38+
};
2539
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)