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

feature/custom-component add custom component render and classNames #106

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store
.idea/
.yarnrc
coverage/
node_modules/
dist/
Expand Down
5 changes: 5 additions & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


lastUpdateCheck 1495138597547
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ The notification object has the following properties:
| Name | Type | Default | Description |
|------------ |--------------- |--------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| title | string | null | Title of the notification |
| message | string | null | Message of the notification |
| message | string | null | Message of the notification |
| getContentComponent | function | null | Return a React component to display in the notification (override other settings) |
| level | string | null | Level of the notification. Available: **success**, **error**, **warning** and **info** |
| position | string | tr | Position of the notification. Available: **tr (top right)**, **tl (top left)**, **tc (top center)**, **br (bottom right)**, **bl (bottom left)**, **bc (bottom center)** |
| autoDismiss | integer | 5 | Delay in seconds for the notification go away. Set this to **0** to not auto-dismiss the notification |
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"homepage": "https://github.com/igorprado/react-notification-system",
"dependencies": {
"classnames": "^2.2.5",
"create-react-class": "^15.5.1",
"object-assign": "^4.0.1",
"prop-types": "^15.5.6"
Expand Down
37 changes: 22 additions & 15 deletions src/NotificationContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var createReactClass = require('create-react-class');
var PropTypes = require('prop-types');
var NotificationItem = require('./NotificationItem');
var Constants = require('./constants');
var classnames = require('classnames');

var NotificationContainer = createReactClass({

Expand All @@ -26,28 +27,34 @@ var NotificationContainer = createReactClass({
render: function() {
var self = this;
var notifications;
var classNameContainer = this.props.classNameContainer;

var classNameSelector = classnames(
'notification-container',
'notifications-' + this.props.position, {
[classNameContainer]: !!classNameContainer
}
);

if ([Constants.positions.bl, Constants.positions.br, Constants.positions.bc].indexOf(this.props.position) > -1) {
this.props.notifications.reverse();
}

notifications = this.props.notifications.map(function(notification) {
return (
<NotificationItem
ref={ 'notification-' + notification.uid }
key={ notification.uid }
notification={ notification }
getStyles={ self.props.getStyles }
onRemove={ self.props.onRemove }
noAnimation={ self.props.noAnimation }
allowHTML={ self.props.allowHTML }
children={ self.props.children }
/>
);
});
notifications = this.props.notifications.map(notification => (
<NotificationItem
ref={ 'notification-' + notification.uid }
key={ notification.uid }
notification={ notification }
getStyles={ self.props.getStyles }
onRemove={ self.props.onRemove }
noAnimation={ self.props.noAnimation }
allowHTML={ self.props.allowHTML }
children={ self.props.children }
/>
));

return (
<div className={ 'notifications-' + this.props.position } style={ this._style }>
<div className={ classNameSelector } style={ this._style }>
{ notifications }
</div>
);
Expand Down
42 changes: 27 additions & 15 deletions src/NotificationItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var ReactDOM = require('react-dom');
var Constants = require('./constants');
var Helpers = require('./helpers');
var merge = require('object-assign');
var classnames = require('classnames');

/* From Modernizr */
var whichTransitionEvent = function() {
Expand Down Expand Up @@ -242,23 +243,20 @@ var NotificationItem = createReactClass({

render: function() {
var notification = this.props.notification;
var className = 'notification notification-' + notification.level;
var notificationStyle = merge({}, this._styles.notification);
var cssByPos = this._getCssPropertyByPosition();
var dismiss = null;
var actionButton = null;
var title = null;
var message = null;
var content = null;
var getContentComponent = notification.getContentComponent;

if (this.state.visible) {
className += ' notification-visible';
} else if (this.state.visible === false) {
className += ' notification-hidden';
}

if (!notification.dismissible) {
className += ' notification-not-dismissible';
}
var className = classnames('notification', 'notification-' + notification.level, {
'notification-visible': this.state.visible,
'notification-hidden': !this.state.visible,
'notification-not-dismissible': !notification.dismissible
});

if (this.props.getStyles.overrideStyle) {
if (!this.state.visible && !this.state.removed) {
Expand Down Expand Up @@ -316,12 +314,26 @@ var NotificationItem = createReactClass({
actionButton = notification.children;
}

if (getContentComponent) {
content = getContentComponent();
} else {
content = [
title,
message,
dismiss,
actionButton
];
}

return (
<div className={ className } onClick={ this._dismiss } onMouseEnter={ this._handleMouseEnter } onMouseLeave={ this._handleMouseLeave } style={ notificationStyle }>
{ title }
{ message }
{ dismiss }
{ actionButton }
<div
className={ className }
onClick={ this._dismiss }
onMouseEnter={ this._handleMouseEnter }
onMouseLeave={ this._handleMouseLeave }
style={ notificationStyle }
>
{content}
</div>
);
}
Expand Down
10 changes: 9 additions & 1 deletion src/NotificationSystem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var merge = require('object-assign');
var NotificationContainer = require('./NotificationContainer');
var Constants = require('./constants');
var Styles = require('./styles');
var classnames = require('classnames');

var NotificationSystem = createReactClass({

Expand Down Expand Up @@ -238,6 +239,13 @@ var NotificationSystem = createReactClass({
var self = this;
var containers = null;
var notifications = this.state.notifications;
var className = this.props.className;

var classNameSelector = classnames(
'notifications-wrapper', {
[className]: !!className
}
);

if (notifications.length) {
containers = Object.keys(Constants.positions).map(function(position) {
Expand Down Expand Up @@ -266,7 +274,7 @@ var NotificationSystem = createReactClass({


return (
<div className="notifications-wrapper" style={ this._getStyles.wrapper() }>
<div className={ classNameSelector } style={ this._getStyles.wrapper() }>
{ containers }
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var CONSTANTS = {
position: 'tr',
autoDismiss: 5,
dismissible: true,
action: null
action: null,
getContentComponent: null
}
};

Expand Down