-
Notifications
You must be signed in to change notification settings - Fork 78
/
expectedComponents.js
189 lines (175 loc) · 6.24 KB
/
expectedComponents.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, {Component} from 'react';
import _ from 'lodash';
import TransitionGroup from 'react/lib/ReactCSSTransitionGroup';
import {POSITIONS} from '../../src/constants';
import Notification from '../../src/components/Notification';
import NotificationsContainer from '../../src/components/NotificationsContainer';
import {defaultValues} from '../../src/components/NotificationsSystem';
// We use these "expected" component to test HTML structure of each components.
// It's easier and faster than testing manually each nodes.
// With this technique, we are 100% sure of the HTML structure of each component.
export class ExpectedNotification extends Component {
_setHTML(content) {
return {
__html: content
};
}
_renderButtons() {
const {
className,
notification: {buttons}
} = this.props;
return buttons.map((button) => {
return (
<button key={button.name} className={className.button} onClick={button.onClick}>
<span className={className.buttonText}>
{button.primary ?
<b>{button.name}</b> :
button.name
}
</span>
</button>
);
});
}
render() {
const {
className,
notification: {title, message, status, dismissible, closeButton, buttons, image, allowHTML}
} = this.props;
const isDismissible = (dismissible && buttons.length === 0);
return (
<div className={className.wrapper}>
<div className={`${className.main} ${className.status(status)} ` +
`${(isDismissible && !closeButton ? className.dismissible : '')} ` +
`${className.buttons(buttons.length)}`}>
{image ?
<div className={className.imageContainer}>
<span className={className.image} style={{backgroundImage: `url(${image})`}}/>
</div> :
<span className={className.icon}/>
}
<div className={className.meta}>
{title ?
allowHTML ?
<h4 className={className.title} dangerouslySetInnerHTML={this._setHTML(title)}/> :
<h4 className={className.title}>{title}</h4> :
''}
{message ?
allowHTML ?
<p className={className.message}
dangerouslySetInnerHTML={this._setHTML(message)}/> :
<p className={className.message}>{message}</p> :
''}
</div>
{isDismissible && closeButton ?
<div className={className.closeButtonContainer}>
<span className={className.closeButton}/>
</div> :
''}
{buttons.length ?
<div className={className.buttons()}>
{this._renderButtons()}
</div> :
''}
</div>
</div>
);
}
}
export class ExpectedNotificationsContainer extends Component {
_renderNotifications() {
// get all notifications and default values for notifications
const {
position, theme: {notification: {className}},
defaultValues: {status, dismissible, dismissAfter, closeButton, allowHTML}
} = this.props;
let {notifications} = this.props;
// when notifications are displayed at the bottom,
// we display notifications from bottom to top
if ([POSITIONS.bottomLeft, POSITIONS.bottomRight].indexOf(position) >= 0) {
notifications = notifications.reverse();
}
return notifications.map((notification) => {
// Define default values for notification if it's needed
if (!notification.status) {
notification.status = status;
}
if (typeof notification.dismissible !== 'boolean') {
notification.dismissible = dismissible;
}
if (typeof notification.dismissAfter !== 'number') {
notification.dismissAfter = dismissAfter;
}
if (typeof notification.closeButton !== 'boolean') {
notification.closeButton = closeButton;
}
if (typeof notification.allowHTML !== 'boolean') {
notification.allowHTML = allowHTML;
}
return (
<Notification key={notification.id} notification={notification} className={className}/>
);
});
}
render() {
const {
className, transition: {name, enterTimeout, leaveTimeout}
} = this.props.theme.notificationsContainer;
const {position} = this.props;
return (
<div className={`${className.main} ${className.position(position)}`}>
<TransitionGroup transitionName={name} transitionEnterTimeout={enterTimeout}
transitionLeaveTimeout={leaveTimeout}>
{this._renderNotifications()}
</TransitionGroup>
</div>
);
}
}
export class ExpectedNotificationsSystem extends Component {
static defaultProps = {
notifications: [],
defaultValues
};
_renderNotificationsContainers() {
const {notifications, defaultValues: {position}, theme} = this.props;
// render all notifications in the same container at the top for small screens
if (window.innerWidth < theme.smallScreenMin) {
return (
<NotificationsContainer key='t' position='t' defaultValues={defaultValues}
theme={theme} notifications={notifications}/>
);
}
let positions = _.values(POSITIONS);
// extract the default position of all positions
positions.splice(positions.indexOf(position), 1);
let notifs = notifications.filter((notif) => {
return (!notif.position || notif.position === position);
});
// init array with all notifications with default position
let JSX = [
<NotificationsContainer key={position} position={position} defaultValues={defaultValues}
theme={theme} notifications={notifs}/>
];
// fill array with others containers
JSX = JSX.concat(positions.map((position) => {
let notifs = notifications.filter((notif) => {
return position === notif.position;
});
return (
<NotificationsContainer key={position} position={position} defaultValues={defaultValues}
theme={theme} notifications={notifs}/>
);
}));
return JSX;
}
render() {
const {className} = this.props.theme.notificationsSystem;
return (
<div className={className}>
{this._renderNotificationsContainers()}
</div>
);
}
}