Skip to content

Commit ad36b59

Browse files
iamhosseindhvThierry Santos
and
Thierry Santos
authored
EnqueueSnackbar supports snackbar with key zero (#318)
* feat: added a function to check if a value is defined * refact: encapsulate key with isDefined to check if the value is valid Co-authored-by: Thierry Santos <[email protected]>
1 parent cdd35a9 commit ad36b59

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/SnackbarProvider.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Component } from 'react';
22
import { createPortal } from 'react-dom';
33
import clsx from 'clsx';
44
import SnackbarContext from './SnackbarContext';
5-
import { MESSAGES, REASONS, originKeyExtractor, omitContainerKeys, merge, DEFAULTS } from './utils/constants';
5+
import { MESSAGES, REASONS, originKeyExtractor, omitContainerKeys, merge, DEFAULTS, isDefined } from './utils/constants';
66
import SnackbarItem from './SnackbarItem';
77
import SnackbarContainer from './SnackbarContainer';
88
import warning from './utils/warning';
@@ -49,7 +49,7 @@ class SnackbarProvider extends Component<SnackbarProviderProps, State> {
4949
* Returns generated or user defined key referencing the new snackbar or null
5050
*/
5151
enqueueSnackbar = (message: SnackbarMessage, { key, preventDuplicate, ...options }: OptionsObject = {}): SnackbarKey => {
52-
const hasSpecifiedKey = key || key === 0;
52+
const hasSpecifiedKey = isDefined(key);
5353
const id = hasSpecifiedKey ? (key as SnackbarKey) : new Date().getTime() + Math.random();
5454

5555
const merger = merge(options, this.props, DEFAULTS);
@@ -173,7 +173,7 @@ class SnackbarProvider extends Component<SnackbarProviderProps, State> {
173173
* Set the entered state of the snackbar with the given key.
174174
*/
175175
handleEnteredSnack: TransitionHandlerProps['onEntered'] = (node, isAppearing, key) => {
176-
if (typeof key === 'undefined') {
176+
if (!isDefined(key)) {
177177
throw new Error('handleEnteredSnack Cannot be called with undefined key');
178178
}
179179

@@ -215,7 +215,7 @@ class SnackbarProvider extends Component<SnackbarProviderProps, State> {
215215
closeSnackbar: ProviderContext['closeSnackbar'] = (key) => {
216216
// call individual snackbar onClose callback passed through options parameter
217217
const toBeClosed = this.state.snacks.find(item => item.key === key);
218-
if (key && toBeClosed && toBeClosed.onClose) {
218+
if (isDefined(key) && toBeClosed && toBeClosed.onClose) {
219219
toBeClosed.onClose(null, REASONS.INSTRUCTED, key);
220220
}
221221

@@ -232,7 +232,7 @@ class SnackbarProvider extends Component<SnackbarProviderProps, State> {
232232
// @ts-ignore
233233
handleExitedSnack: TransitionHandlerProps['onExited'] = (event, key1, key2) => {
234234
const key = key1 || key2;
235-
if (typeof key === 'undefined') {
235+
if (!isDefined(key)) {
236236
throw new Error('handleExitedSnack Cannot be called with undefined key');
237237
}
238238

src/utils/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export const omitContainerKeys = (classes: SnackbarProviderProps['classes']): Sn
5050
Object.keys(classes).filter(key => !allClasses.container[key]).reduce((obj, key) => ({ ...obj, [key]: classes[key] }), {})
5151
);
5252

53+
export const isDefined = (value: string | null | undefined | number): boolean => (!!value || value === 0);
54+
5355
export const DEFAULTS = {
5456
variant: 'default',
5557
autoHideDuration: 5000,

0 commit comments

Comments
 (0)