Skip to content

Commit

Permalink
Add support to for onClose and onExited props.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhosseindhv committed Nov 19, 2018
1 parent e7eea87 commit 728e004
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"error",
4
],
"react/destructuring-assignment": 0,
"react/forbid-prop-types": 0,
"react/sort-comp": 0,
"react/jsx-filename-extension": [
Expand Down
16 changes: 11 additions & 5 deletions src/SnackbarItem/SnackbarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ import {
capitalise,
defaultAnchorOrigin,
getTransitionDirection,
muiClasses,
getMuiClasses,
TransitionComponent,
variantIcon,
} from './SnackbarItem.util';


class SnackbarItem extends Component {
handleClose = key => (event, reason) => {
const { onClose } = this.props;
const { onClose, snack: { onClose: singleOnClose } } = this.props;
if (reason === 'clickaway') return;
if (singleOnClose) singleOnClose(key);
onClose(key);
};

handleExited = key => () => {
const { onExited, snack: { onExited: singleOnExited } } = this.props;
if (singleOnExited) singleOnExited(key);
onExited(key);
};

render() {
const {
classes,
Expand All @@ -33,7 +40,6 @@ class SnackbarItem extends Component {
level,
snack,
style,
onExited,
onClickAction,
...other
} = this.props;
Expand Down Expand Up @@ -68,9 +74,9 @@ class SnackbarItem extends Component {
{...other}
{...singleSnackProps}
open={snack.open}
classes={muiClasses(classes)}
classes={getMuiClasses(classes)}
onClose={this.handleClose(key)}
onExited={() => onExited(key)}
onExited={this.handleExited(key)}
>
<SnackbarContent
className={classNames(
Expand Down
11 changes: 1 addition & 10 deletions src/SnackbarItem/SnackbarItem.styles.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import green from '@material-ui/core/colors/green';
import amber from '@material-ui/core/colors/amber';
import { muiClasses } from './SnackbarItem.util';
import {
TRANSITION_DELAY,
TRANSITION_DOWN_DURATION,
} from '../utils/constants';

const muiClasses = {
root: {},
anchorOriginTopCenter: {},
anchorOriginBottomCenter: {},
anchorOriginTopRight: {},
anchorOriginBottomRight: {},
anchorOriginTopLeft: {},
anchorOriginBottomLeft: {},
};

const styles = theme => ({
...muiClasses,
base: {
Expand Down
23 changes: 12 additions & 11 deletions src/SnackbarItem/SnackbarItem.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ const defaultAnchorOrigin = {
horizontal: 'left',
};

const notistackClasses = [
'base',
'variantSuccess',
'variantError',
'variantInfo',
'variantWarning',
'message',
'icon',
];
const muiClasses = {
root: {},
anchorOriginTopCenter: {},
anchorOriginBottomCenter: {},
anchorOriginTopRight: {},
anchorOriginBottomRight: {},
anchorOriginTopLeft: {},
anchorOriginBottomLeft: {},
};

/**
* returns transition direction according the the given anchor origin
Expand All @@ -89,9 +89,9 @@ const capitalise = text => text.charAt(0).toUpperCase() + text.slice(1);
* in material-ui snackbar classes prop
* @param {object} classes
*/
const muiClasses = classes => (
const getMuiClasses = classes => (
Object.keys(classes)
.filter(key => !notistackClasses.includes(key))
.filter(key => muiClasses[key] !== undefined)
.reduce((obj, key) => ({
...obj,
[key]: classes[key],
Expand All @@ -102,6 +102,7 @@ export {
capitalise,
defaultAnchorOrigin,
getTransitionDirection,
getMuiClasses,
muiClasses,
TransitionComponent,
variantIcon,
Expand Down
8 changes: 8 additions & 0 deletions src/SnackbarProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class SnackbarProvider extends Component {
...snacks.map(item => (item.key === key ? { ...item, open: false } : { ...item })),
],
}));

if (this.props.onClose) this.props.onClose(key);
};

/**
Expand All @@ -118,6 +120,8 @@ class SnackbarProvider extends Component {
}),
() => setTimeout(this.handleDisplaySnack, enterDelay),
);

if (this.props.onExited) this.props.onExited(key);
};

render() {
Expand Down Expand Up @@ -153,10 +157,14 @@ SnackbarProvider.propTypes = {
* on top of one another
*/
maxSnack: PropTypes.number,
onClose: PropTypes.func,
onExited: PropTypes.func,
};

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

export default SnackbarProvider;

0 comments on commit 728e004

Please sign in to comment.