diff --git a/README.md b/README.md index 201380f..d61e6c6 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ Example [here](https://codepen.io/minhtranite/pen/RgoaLL) |------|------|---------|----------| | enterTimeout | number | 400 | false | | leaveTimeout | number | 400 | false | +| limit | number | 0 | false | ## NotificationManager API diff --git a/src/NotificationContainer.js b/src/NotificationContainer.js index 431d11b..c83d9e7 100644 --- a/src/NotificationContainer.js +++ b/src/NotificationContainer.js @@ -6,12 +6,14 @@ import Notifications from './Notifications'; class NotificationContainer extends React.Component { static propTypes = { enterTimeout: PropTypes.number, - leaveTimeout: PropTypes.number + leaveTimeout: PropTypes.number, + limit: PropTypes.number }; static defaultProps = { enterTimeout: 400, - leaveTimeout: 400 + leaveTimeout: 400, + limit: 0 }; state = { @@ -19,6 +21,7 @@ class NotificationContainer extends React.Component { }; componentWillMount = () => { + NotificationManager.limit = this.props.limit; NotificationManager.addChangeListener(this.handleStoreChange); }; diff --git a/src/NotificationManager.js b/src/NotificationManager.js index 7206496..0537db5 100644 --- a/src/NotificationManager.js +++ b/src/NotificationManager.js @@ -20,6 +20,7 @@ const Constants = { class NotificationManager extends EventEmitter { constructor() { super(); + this.limit = 0; this.listNotify = []; } @@ -33,8 +34,16 @@ class NotificationManager extends EventEmitter { }; if (notify.priority) { this.listNotify.unshift(Object.assign(defaultNotify, notify)); + + if (this.limit && this.listNotify.length > this.limit) { + this.listNotify.pop(); + } } else { this.listNotify.push(Object.assign(defaultNotify, notify)); + + if (this.limit && this.listNotify.length > this.limit) { + this.listNotify.shift(); + } } this.emitChange(); }