Skip to content

Commit

Permalink
Fixes #267 - Allow autoHideDuration of null
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhosseindhv committed May 17, 2020
1 parent caf513d commit c84c3ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
10 changes: 6 additions & 4 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 } from './utils/constants';
import { MESSAGES, REASONS, originKeyExtractor, omitContainerKeys, merge, DEFAULTS } from './utils/constants';
import SnackbarItem from './SnackbarItem';
import SnackbarContainer from './SnackbarContainer';
import warning from './utils/warning';
Expand Down Expand Up @@ -51,16 +51,18 @@ class SnackbarProvider extends Component<SnackbarProviderProps, State> {
enqueueSnackbar = (message: SnackbarMessage, { key, preventDuplicate, ...options }: OptionsObject = {}): SnackbarKey => {
const hasSpecifiedKey = key || key === 0;
const id = hasSpecifiedKey ? (key as SnackbarKey) : new Date().getTime() + Math.random();

const merger = merge(options, this.props, DEFAULTS);
const snack: Snack = {
key: id,
...options,
message,
open: true,
entered: false,
requestClose: false,
variant: options.variant || this.props.variant || 'default',
autoHideDuration: options.autoHideDuration || this.props.autoHideDuration || 5000,
anchorOrigin: options.anchorOrigin || this.props.anchorOrigin || { vertical: 'bottom', horizontal: 'left' },
variant: merger('variant'),
anchorOrigin: merger('anchorOrigin'),
autoHideDuration: merger('autoHideDuration'),
};

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

export const DEFAULTS = {
variant: 'default',
autoHideDuration: 5000,
anchorOrigin: {
vertical: 'bottom',
horizontal: 'left',
},
};

const numberOrNull = (numberish?: number | null) => (
typeof numberish === 'number' || numberish === null
);

// @ts-ignore
export const merge = (options, props, defaults) => (name: keyof Snack): any => {
if (name === 'autoHideDuration') {
if (numberOrNull(options.autoHideDuration)) return options.autoHideDuration;
if (numberOrNull(props.autoHideDuration)) return props.autoHideDuration;
return DEFAULTS.autoHideDuration;
}

return options[name] || props[name] || defaults[name];
};

export const REASONS: { [key: string]: CloseReason } = {
CLICKAWAY: 'clickaway',
MAXSNACK: 'maxsnack',
Expand Down

0 comments on commit c84c3ea

Please sign in to comment.