-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathNotificationsWrapperExample.js
52 lines (41 loc) · 1.65 KB
/
NotificationsWrapperExample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { Component } from 'react';
import Button from '../../src/components/button';
import PrimaryButton from '../../src/components/primary-button';
import { Notification, NotificationsWrapper } from '../../src/components/notification';
class NotificationsWrapperExample extends Component {
state = {
id: 0,
notifications: new Map(),
};
closeNotification = id => {
const { notifications } = this.state;
notifications.delete(id);
this.setState({ notifications });
};
addNotification = duration => {
const { id, notifications } = this.state;
const type = [undefined, 'info', 'warn', 'error'][id % 4];
const notification = (
<Notification key={id} duration={duration} onClose={() => this.closeNotification(id)} type={type}>
<span>Hello world! I was made at {new Date().toTimeString()}</span>
<Button>Okay</Button>
</Notification>
);
this.setState({
notifications: notifications.set(id, notification),
id: id + 1,
});
};
render() {
const { notifications } = this.state;
return (
<div>
<NotificationsWrapper>{[...notifications.values()]}</NotificationsWrapper>
<Button onClick={() => this.addNotification('short')}>Display timed notification</Button>
<PrimaryButton onClick={() => this.addNotification()}>Display persistent notification</PrimaryButton>
</div>
);
}
}
NotificationsWrapperExample.displayName = 'NotificationsWrapperExample';
export default NotificationsWrapperExample;