Skip to content

Commit

Permalink
Fixes #180 - Add support to globally customize snackbarContent
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhosseindhv committed Oct 11, 2019
1 parent 8e9d0af commit caad8d8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/SnackbarItem/SnackbarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Collapse from '@material-ui/core/Collapse';
import SnackbarContent from '@material-ui/core/SnackbarContent';
import { getTransitionDirection, getSnackbarClasses, getCollapseClasses } from './SnackbarItem.util';
import styles from './SnackbarItem.styles';
import { capitalise } from '../utils/constants';
import { capitalise, MESSAGES } from '../utils/constants';
import warning from '../utils/warning';


class SnackbarItem extends Component {
Expand Down Expand Up @@ -43,6 +44,7 @@ class SnackbarItem extends Component {
const {
classes,
action,
content,
ContentProps = {},
hideIconVariant,
preventDuplicate,
Expand All @@ -57,6 +59,8 @@ class SnackbarItem extends Component {
const {
key,
persist,
children,
content: singleContent,
variant = 'default',
action: singleAction,
ContentProps: singleContentProps = {},
Expand All @@ -79,9 +83,17 @@ class SnackbarItem extends Component {
finalAction = contentProps.action(key);
}

let snackChildren = snack.children;
if (snackChildren && typeof snackChildren === 'function') {
snackChildren = snackChildren(key);
let snackContent;
if (snack.children) {
snackContent = snack.children;
warning(MESSAGES.NO_CHILDREN_OPTION);
}
if (singleContent) {
snackContent = singleContent;
}
snackContent = snackContent || content;
if (snackContent && typeof snackContent === 'function') {
snackContent = snackContent(key);
}

return (
Expand All @@ -104,7 +116,7 @@ class SnackbarItem extends Component {
classes={getSnackbarClasses(classes)}
onClose={this.handleClose(key)}
>
{snackChildren || (
{snackContent || (
<SnackbarContent
className={classNames(
classes.base,
Expand Down
9 changes: 7 additions & 2 deletions src/SnackbarProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,15 @@ SnackbarProvider.propTypes = {
info: PropTypes.any,
}),
/**
* Callback to get action(s). actions are mostly buttons displayed in Snackbar.
* Callback used for getting action(s). actions are mostly buttons displayed in Snackbar.
* @param {string|number} key key of a snackbar
*/
action: PropTypes.func,
action: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* Replace the snackbar. Callback used for displaying entirely customized snackbar.
* @param {string|number} key key of a snackbar
*/
content: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* The anchor of the `Snackbar`.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface OptionsObject extends Omit<SnackbarProps, 'open' | 'message' |
persist?: boolean;
preventDuplicate?: boolean;
children?: React.ReactNode | ((key: OptionsObject['key']) => React.ReactNode);
content?: React.ReactNode | ((key: OptionsObject['key']) => React.ReactNode);
action?: SnackbarContentProps['action'] | ((key: OptionsObject['key']) => React.ReactNode);
}

Expand Down Expand Up @@ -47,6 +48,7 @@ export interface SnackbarProviderProps extends Omit<SnackbarProps, 'open' | 'mes
preventDuplicate?: boolean;
dense?: boolean;
action?: SnackbarContentProps['action'] | ((key: OptionsObject['key']) => React.ReactNode);
content?: React.ReactNode | ((key: OptionsObject['key']) => React.ReactNode);
}

export const SnackbarProvider: React.ComponentType<SnackbarProviderProps>;
1 change: 1 addition & 0 deletions src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const defaultIconVariant = {

export const MESSAGES = {
NO_PERSIST_ALL: 'WARNING - notistack: Reached maxSnack while all enqueued snackbars have \'persist\' flag. Notistack will dismiss the oldest snackbar anyway to allow other ones in the queue to be presented.',
NO_CHILDREN_OPTION: 'WARNING - notistack: \'children\' option in enqueueSnackbar has been deprecated and renamed to \'content\', and it will be removed in the next major release.',
};

export const SNACKBAR_INDENTS = {
Expand Down

0 comments on commit caad8d8

Please sign in to comment.