Skip to content
This repository was archived by the owner on Dec 24, 2019. It is now read-only.

Commit 5b6ccd5

Browse files
elrumordelaluzPatrick Burtchaell
authored andcommitted
Implement action and onClick for stacked notifications
* Allow Action button on Stacked Notifications - Leave the `action` and `onClick` props to be defines by the Notification component - Add references to timer to `clearTimeout` when will unmount * Apply changes for Stacked Notifications in the example - Use an Object to control the Notifications State - Add a method to handle Remove Notification, invoked from `onDismiss` and `onClick`
1 parent afd40df commit 5b6ccd5

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

examples/notification-tree/index.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,44 @@ class Example extends Component {
88
super(props);
99

1010
this.state = {
11-
notifications: OrderedSet(),
12-
13-
// This is just used for the sake of an example to make sure
14-
// notifications have unique keys. In production, you should have
15-
// a different system for UIDs.
16-
count: 0
11+
notifications: {},
1712
};
13+
14+
this.removeNotification = this.removeNotification.bind(this);
15+
this.renderNotifications = this.renderNotifications.bind(this);
1816
}
1917

18+
2019
addNotification() {
21-
const { notifications, count } = this.state;
22-
const id = notifications.size + 1;
23-
const newCount = count + 1;
20+
// This is just used for the sake of an example to make sure
21+
// notifications have unique keys. In production, you should have
22+
// a different system for UIDs.
23+
const id = Date.now();
2424

25-
return this.setState({
26-
count: newCount,
27-
notifications: notifications.add({
25+
const notifications = Object.assign({}, this.state.notifications, {
26+
[id]: {
27+
action: 'Dismiss',
28+
dismissAfter: 3400,
29+
onClick: () => this.removeNotification(id),
2830
message: `Notification ${id}`,
29-
key: newCount,
30-
barStyle: {
31-
background: 'grey'
32-
}
33-
})
31+
key: id
32+
}
3433
});
34+
35+
return this.setState({
36+
notifications: notifications
37+
});
38+
}
39+
40+
removeNotification (notif) {
41+
const notifications = Object.assign({}, this.state.notifications);
42+
delete notifications[notif];
43+
44+
this.setState({ notifications })
45+
}
46+
47+
renderNotifications () {
48+
return Object.keys(this.state.notifications).map(notif => this.state.notifications[notif])
3549
}
3650

3751
render() {
@@ -41,10 +55,8 @@ class Example extends Component {
4155
Add notification
4256
</button>
4357
<NotificationStack
44-
notifications={this.state.notifications.toArray()}
45-
onDismiss={notification => this.setState({
46-
notifications: this.state.notifications.delete(notification)
47-
})}
58+
notifications={this.renderNotifications()}
59+
onDismiss={notif => this.removeNotification(notif.key)}
4860
/>
4961
</div>
5062
);

src/notificationStack.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const NotificationStack = props => {
2121
isLast={isLast}
2222
action={notification.action || props.action}
2323
dismissAfter={isLast ? dismissAfter : dismissAfter + (index * 1000)}
24-
onClick={() => props.onDismiss(notification)}
2524
onDismiss={() => {
2625
setTimeout(() => {
2726
setTimeout(props.onDismiss.bind(this, notification), 300);

src/stackedNotification.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ class StackedNotification extends Component {
1111
}
1212

1313
componentDidMount() {
14-
setTimeout(this.setState.bind(this, {
14+
this.activeTimeout = setTimeout(this.setState.bind(this, {
1515
isActive: true
1616
}), 1);
1717

18-
setTimeout(this.setState.bind(this, {
18+
this.dismissTimeout = setTimeout(this.setState.bind(this, {
1919
isActive: false
2020
}), this.props.dismissAfter);
2121
}
2222

23+
componentWillUnmount() {
24+
clearTimeout(this.dismissTimeout);
25+
}
26+
2327
render() {
2428
const bottomPosition = `${2 + this.props.index * 4}rem`;
2529

2630
return (
2731
<Notification
2832
{...this.props}
29-
action={false}
3033
isActive={this.state.isActive}
3134
barStyle={Object.assign({}, {
3235
bottom: bottomPosition

0 commit comments

Comments
 (0)