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

Add preventDuplicate options #67

Merged
merged 5 commits into from
Feb 23, 2019
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
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ iconVariant type: any required: false default: Materia
hideIconVariant type: boolean required: false default: false

// event fired when user clicks on action button (if any)
onClickAction type: func required: false defualt: dismisses the snackbar
onClickAction type: func required: false default: dismisses the snackbar

// hide or display duplicated messages
preventDuplicate type: boolean required: false default: false

// Example of a Mui-Snackbar prop
transitionDuration={{ exit: 380, enter: 400 }}
Expand All @@ -114,13 +117,16 @@ Adds a snackbar to the queue to be displayed to the user. It takes two arguments
const key = this.props.enqueueSnackbar(message, options)

// text of the snackbar
message type:string required: true
message type:string required: true

// object containing options with the following shape
options: type:object required: false
options: type:object required: false

// type of the snackbar
options.variant type:string oneOf(['default', 'error', 'success', 'warning', 'info'])
options.variant type:string oneOf(['default', 'error', 'success', 'warning', 'info'])

// hide or display this message if it's the same of the previous one
options.preventDuplicate type:boolean required: false

// You can pass any material-ui Snackbar prop here, and they will be applied to this individual snackbar.
// for example, this particular snackbar will get dismissed after 1 second.
Expand Down
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import { TransitionActions } from 'react-transition-group/Transition';
import { SvgIconProps } from '@material-ui/core/SvgIcon';
import { SnackbarProps, SnackbarClassKey } from '@material-ui/core/Snackbar';

Expand All @@ -11,6 +10,7 @@ export interface OptionsObject extends Omit<SnackbarProps, 'open' | 'message' |
key?: string | number;
variant?: VariantType;
onClickAction?: Function;
preventDuplicate?: boolean;
}

type NotistackClassKey = 'variantSuccess'
Expand All @@ -23,7 +23,7 @@ export type CombinedClassKey = NotistackClassKey | SnackbarClassKey;

export interface InjectedNotistackProps {
onPresentSnackbar: (variant: VariantType, message: string) => void;
enqueueSnackbar: (message: string | React.ReactNode, options?: OptionsObject) => string | number;
enqueueSnackbar: (message: string | React.ReactNode, options?: OptionsObject) => string | number | null;
closeSnackbar: (key: string | number) => void
}

Expand All @@ -40,6 +40,7 @@ export interface SnackbarProviderProps
iconVariant?: React.ComponentType<SvgIconProps>;
hideIconVariant?: boolean;
onClickAction?: Function;
preventDuplicate?: boolean;
}

export const SnackbarProvider: React.ComponentType<SnackbarProviderProps>;
11 changes: 10 additions & 1 deletion src/SnackbarProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ class SnackbarProvider extends Component {
* can be: (default, success, error, warning, info)
* @returns generated or user defined key referencing the new snackbar
*/
handleEnqueueSnackbar = (message, { key, ...options } = {}) => {
handleEnqueueSnackbar = (message, { key, preventDuplicate, ...options } = {}) => {
const shouldPreventDuplicate = preventDuplicate || this.props.preventDuplicate;
const isInQueue = !!this.queue.find(item => item.message === message);
const isDisplayed = !!this.state.snacks.find(item => item.message === message);
if (shouldPreventDuplicate && (isInQueue || isDisplayed)) {
return null;
}

const id = key || new Date().getTime() + Math.random();
this.queue.push({
key: id,
Expand Down Expand Up @@ -227,12 +234,14 @@ SnackbarProvider.propTypes = {
maxSnack: PropTypes.number,
onClose: PropTypes.func,
onExited: PropTypes.func,
preventDuplicate: PropTypes.bool,
};

SnackbarProvider.defaultProps = {
maxSnack: 3,
onClose: undefined,
onExited: undefined,
preventDuplicate: false,
};

export default SnackbarProvider;