Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnqueueSnackbar supports snackbar with key zero #318

Merged
merged 3 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/SnackbarProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Component } from 'react';
import { createPortal } from 'react-dom';
import clsx from 'clsx';
import SnackbarContext from './SnackbarContext';
import { MESSAGES, REASONS, originKeyExtractor, omitContainerKeys, merge, DEFAULTS } from './utils/constants';
import { MESSAGES, REASONS, originKeyExtractor, omitContainerKeys, merge, DEFAULTS, isDefined } from './utils/constants';
import SnackbarItem from './SnackbarItem';
import SnackbarContainer from './SnackbarContainer';
import warning from './utils/warning';
Expand Down Expand Up @@ -49,7 +49,7 @@ class SnackbarProvider extends Component<SnackbarProviderProps, State> {
* Returns generated or user defined key referencing the new snackbar or null
*/
enqueueSnackbar = (message: SnackbarMessage, { key, preventDuplicate, ...options }: OptionsObject = {}): SnackbarKey => {
const hasSpecifiedKey = key || key === 0;
const hasSpecifiedKey = isDefined(key);
const id = hasSpecifiedKey ? (key as SnackbarKey) : new Date().getTime() + Math.random();

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const omitContainerKeys = (classes: SnackbarProviderProps['classes']): Sn
Object.keys(classes).filter(key => !allClasses.container[key]).reduce((obj, key) => ({ ...obj, [key]: classes[key] }), {})
);

export const isDefined = (value: string | null | undefined | number): boolean => (!!value || value === 0);

export const DEFAULTS = {
variant: 'default',
autoHideDuration: 5000,
Expand Down