Skip to content

Commit 3f940da

Browse files
committed
Merge branch 'release/0.2.14' into develop
2 parents 847f843 + 3d6e955 commit 3f940da

6 files changed

Lines changed: 99 additions & 10 deletions

File tree

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
},
1919
"rules": {
20-
"react/display-name": 2,
20+
"react/display-name": 0,
2121
"react/jsx-curly-spacing": [2, "always"],
2222
"react/jsx-no-duplicate-props": 2,
2323
"react/jsx-no-undef": 2,

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 0.2.14 - May 3, 2017
4+
5+
* Ability to [edit notifications](https://github.com/igorprado/react-notification-system#removenotificationnotification). (thanks to @syndbg)
6+
* Removed deprecation warning. Now using `prop-types` and `create-react-class`packages. (thanks to @andrewBalekha)
7+
* Fix calling `onRemove` before updating the notifications state. (thanks to @szdc)
8+
39
## 0.2.13 - Mar 14, 2017
410

511
* UMD support. (thanks to @jochenberger)

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ Returns the notification object to be used to programmatically dismiss a notific
8686

8787
Remove a notification programmatically. You can pass an object returned by `addNotification()` or by `onAdd()` callback. If passing an object, you need to make sure it must contain the `uid` property. You can pass only the `uid` too: `removeNotification(uid)`.
8888

89+
90+
### `editNotification(notification)`
91+
92+
Edit a notification programmatically. You can pass an object previously returned by `addNotification()` or by `onAdd()` callback. If passing an object, you need to make sure it must contain the `uid` property. You can pass only the `uid` too: `editNotification(uid)`.
93+
94+
8995
### `clearNotifications()`
9096

9197
Removes ALL notifications programatically.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-notification-system",
3-
"version": "0.2.13",
3+
"version": "0.2.14",
44
"description": "A React Notification System fully customized",
55
"main": "dist/NotificationSystem.js",
66
"scripts": {

src/NotificationSystem.jsx

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,18 @@ var NotificationSystem = createReactClass({
6868
var notifications = this.state.notifications.filter(function(toCheck) {
6969
if (toCheck.uid === uid) {
7070
notification = toCheck;
71+
return false;
7172
}
72-
return toCheck.uid !== uid;
73+
return true;
7374
});
7475

75-
if (notification && notification.onRemove) {
76-
notification.onRemove(notification);
77-
}
78-
7976
if (this._isMounted) {
8077
this.setState({ notifications: notifications });
8178
}
79+
80+
if (notification && notification.onRemove) {
81+
notification.onRemove(notification);
82+
}
8283
},
8384

8485
getInitialState: function() {
@@ -154,18 +155,63 @@ var NotificationSystem = createReactClass({
154155
return _notification;
155156
},
156157

157-
removeNotification: function(notification) {
158+
getNotificationRef: function(notification) {
158159
var self = this;
160+
var foundNotification = null;
161+
159162
Object.keys(this.refs).forEach(function(container) {
160163
if (container.indexOf('container') > -1) {
161164
Object.keys(self.refs[container].refs).forEach(function(_notification) {
162165
var uid = notification.uid ? notification.uid : notification;
163166
if (_notification === 'notification-' + uid) {
164-
self.refs[container].refs[_notification]._hideNotification();
167+
// NOTE: Stop iterating further and return the found notification.
168+
// Since UIDs are uniques and there won't be another notification found.
169+
foundNotification = self.refs[container].refs[_notification];
170+
return;
165171
}
166172
});
167173
}
168174
});
175+
176+
return foundNotification;
177+
},
178+
179+
removeNotification: function(notification) {
180+
var foundNotification = this.getNotificationRef(notification);
181+
return foundNotification && foundNotification._hideNotification();
182+
},
183+
184+
editNotification: function(notification, newNotification) {
185+
var foundNotification = null;
186+
// NOTE: Find state notification to update by using
187+
// `setState` and forcing React to re-render the component.
188+
var uid = notification.uid ? notification.uid : notification;
189+
190+
var newNotifications = this.state.notifications.filter(function(stateNotification) {
191+
if (uid === stateNotification.uid) {
192+
foundNotification = stateNotification;
193+
return false;
194+
}
195+
196+
return true;
197+
});
198+
199+
200+
if (!foundNotification) {
201+
return;
202+
}
203+
204+
newNotifications.push(
205+
merge(
206+
{},
207+
foundNotification,
208+
newNotification
209+
)
210+
);
211+
212+
this.setState({
213+
notifications: newNotifications
214+
});
169215
},
170216

171217
clearNotifications: function() {
@@ -223,7 +269,6 @@ var NotificationSystem = createReactClass({
223269
<div className="notifications-wrapper" style={ this._getStyles.wrapper() }>
224270
{ containers }
225271
</div>
226-
227272
);
228273
}
229274
});

test/notification-system.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,38 @@ describe('Notification Component', function() {
172172
done();
173173
});
174174

175+
it('should edit an existing notification using returned object', (done) => {
176+
const notificationCreated = component.addNotification(defaultNotification);
177+
const notification = TestUtils.scryRenderedDOMComponentsWithClass(instance, 'notification');
178+
expect(notification.length).toEqual(1);
179+
180+
const newTitle = 'foo';
181+
const newContent = 'foobar';
182+
183+
component.editNotification(notificationCreated, { title: newTitle, message: newContent });
184+
clock.tick(1000);
185+
const notificationEdited = TestUtils.findRenderedDOMComponentWithClass(instance, 'notification');
186+
expect(notificationEdited.getElementsByClassName('notification-title')[0].textContent).toEqual(newTitle);
187+
expect(notificationEdited.getElementsByClassName('notification-message')[0].textContent).toEqual(newContent);
188+
done();
189+
});
190+
191+
it('should edit an existing notification using uid', (done) => {
192+
const notificationCreated = component.addNotification(defaultNotification);
193+
const notification = TestUtils.scryRenderedDOMComponentsWithClass(instance, 'notification');
194+
expect(notification.length).toEqual(1);
195+
196+
const newTitle = 'foo';
197+
const newContent = 'foobar';
198+
199+
component.editNotification(notificationCreated.uid, { title: newTitle, message: newContent });
200+
clock.tick(1000);
201+
const notificationEdited = TestUtils.findRenderedDOMComponentWithClass(instance, 'notification');
202+
expect(notificationEdited.getElementsByClassName('notification-title')[0].textContent).toEqual(newTitle);
203+
expect(notificationEdited.getElementsByClassName('notification-message')[0].textContent).toEqual(newContent);
204+
done();
205+
});
206+
175207
it('should remove all notifications', done => {
176208
component.addNotification(defaultNotification);
177209
component.addNotification(defaultNotification);

0 commit comments

Comments
 (0)