Skip to content

Commit 2cdedf4

Browse files
author
Kubit
committed
Add onCloseButton to snackbar component
1 parent b0adf3c commit 2cdedf4

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

src/components/snackbar/snackbarStandAlone.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const SnackbarStandAloneComponent = (
9797
variant={props.styles?.popoverVariants?.[align]}
9898
>
9999
<SnackbarStyled
100+
data-testid={props.dataTestId}
100101
ref={ref}
101102
styles={props.styles}
102103
onBlur={props.onBlur}

src/components/snackbar/snackbarUnControlled.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ const SnackbarUnControlledComponent = <V extends string | unknown>(
6060
const handleCloseButton: (_open: boolean) => React.MouseEventHandler<HTMLButtonElement> =
6161
_open => event => {
6262
props.onOpenClose?.(_open, event);
63+
if (props.onCloseButton) {
64+
props.onCloseButton(event);
65+
return;
66+
}
6367
// After manually closing the popover, focus the last focused element
6468
// If the last focused element is not available, focus the first focusable element
6569
if (

src/components/snackbar/stories/argtypes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ export const argtypes = (variants: IThemeObjectVariants, themeSelected: string):
168168
category: CATEGORY_CONTROL.FUNCTIONS,
169169
},
170170
},
171+
onCloseButton: {
172+
description: 'Funtion to handle the entire close button function If it is needed',
173+
type: { name: 'function', required: true },
174+
control: false,
175+
table: {
176+
type: { summary: 'React.MouseEventHandler<HTMLButtonElement>' },
177+
category: CATEGORY_CONTROL.FUNCTIONS,
178+
},
179+
},
171180
onOpenClose: {
172181
description: 'Callback to be be called when the snackbar is opened or closed',
173182
type: { name: 'function' },

src/components/snackbar/stories/snackbar.stories.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@ const StoryWithHooks = args => {
3939
called when snackbar should close, either because the close icon has been clicked, or
4040
because the &quot;closeTimeout&quot; time for displaying the snackbar has expired.
4141
</p>
42+
<p style={{ marginTop: '10px' }}>
43+
<strong>Note: </strong>Snackbar does not have an internal state. In order to open or close
44+
it, &quot;isOpen&quot; prop must be used. Moreover &quot;onHandleOpen&quot; function will be
45+
called when snackbar should close, either because the close icon has been clicked, or
46+
because the &quot;closeTimeout&quot; time for displaying the snackbar has expired.
47+
</p>
48+
<p style={{ marginTop: '10px' }}>
49+
<strong>Note: </strong>Snackbar does not have an internal state. In order to open or close
50+
it, &quot;isOpen&quot; prop must be used. Moreover &quot;onHandleOpen&quot; function will be
51+
called when snackbar should close, either because the close icon has been clicked, or
52+
because the &quot;closeTimeout&quot; time for displaying the snackbar has expired.
53+
</p>
54+
<p style={{ marginTop: '10px' }}>
55+
<strong>Note: </strong>Snackbar does not have an internal state. In order to open or close
56+
it, &quot;isOpen&quot; prop must be used. Moreover &quot;onHandleOpen&quot; function will be
57+
called when snackbar should close, either because the close icon has been clicked, or
58+
because the &quot;closeTimeout&quot; time for displaying the snackbar has expired.
59+
</p>
60+
<p style={{ marginTop: '10px' }}>
61+
<strong>Note: </strong>Snackbar does not have an internal state. In order to open or close
62+
it, &quot;isOpen&quot; prop must be used. Moreover &quot;onHandleOpen&quot; function will be
63+
called when snackbar should close, either because the close icon has been clicked, or
64+
because the &quot;closeTimeout&quot; time for displaying the snackbar has expired.
65+
</p>
66+
<p style={{ marginTop: '10px' }}>
67+
<strong>Note: </strong>Snackbar does not have an internal state. In order to open or close
68+
it, &quot;isOpen&quot; prop must be used. Moreover &quot;onHandleOpen&quot; function will be
69+
called when snackbar should close, either because the close icon has been clicked, or
70+
because the &quot;closeTimeout&quot; time for displaying the snackbar has expired.
71+
</p>
72+
<p style={{ marginTop: '10px' }}>
73+
<strong>Note: </strong>Snackbar does not have an internal state. In order to open or close
74+
it, &quot;isOpen&quot; prop must be used. Moreover &quot;onHandleOpen&quot; function will be
75+
called when snackbar should close, either because the close icon has been clicked, or
76+
because the &quot;closeTimeout&quot; time for displaying the snackbar has expired.
77+
</p>
4278
</>
4379
);
4480
};
@@ -58,6 +94,7 @@ export const Snackbar: Story = {
5894
description: { content: 'This is the description' },
5995
closeIcon: { icon: ICONS.ICON_PLACEHOLDER },
6096
secondaryActionContent: 'Link',
97+
dataTestId: 'Snackbar',
6198
themeArgs: themesObject[themeSelected][STYLES_NAME.SNACKBAR],
6299
},
63100
parameters: {

src/components/snackbar/types/snackbar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface ISnackbarControlled<V = undefined extends string ? unknown : st
5959
export interface ISnackbarUnControlled<V = undefined extends string ? unknown : string>
6060
extends Omit<ISnackbarControlled<V>, 'onCloseButtonClick'> {
6161
closeTimeout?: number;
62+
onCloseButton?: React.MouseEventHandler<HTMLButtonElement>;
6263
onOpenClose?: (open: boolean, event?: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
6364
}
6465

0 commit comments

Comments
 (0)