UNPKG

4.49 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _style = require('./style.scss');
18
19var _style2 = _interopRequireDefault(_style);
20
21var _bind = require('classnames/bind');
22
23var _bind2 = _interopRequireDefault(_bind);
24
25var _Alert = require('./Alert');
26
27var _Alert2 = _interopRequireDefault(_Alert);
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
34
35function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
36
37var AlertSystem = function (_React$Component) {
38 _inherits(AlertSystem, _React$Component);
39
40 function AlertSystem(props) {
41 _classCallCheck(this, AlertSystem);
42
43 var _this = _possibleConstructorReturn(this, (AlertSystem.__proto__ || Object.getPrototypeOf(AlertSystem)).call(this, props));
44
45 _this.getAlerts = function () {
46 return _this.state.alerts.map(function (alert, index) {
47 return !alert.hidden ? _react2.default.createElement(
48 _Alert2.default,
49 { key: alert.key, type: alert.type || 'success', optClass: alert.class || '', closable: typeof alert.closable !== 'undefined' ? alert.closable : true, timeout: alert.timeout, onClose: _this.removeAlert.bind(_this, alert) },
50 alert.content
51 ) : null;
52 });
53 };
54
55 _this.removeAlert = function (alert) {
56 var alerts = _this.state.alerts;
57
58 alerts.map(function (a, index) {
59 if (alert.key === a.key) {
60 a.hidden = true;
61
62 if (typeof a.onClose === 'function') {
63 a.onClose(alert);
64 }
65 }
66 });
67
68 _this.setState({ alerts: alerts });
69 };
70
71 _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
72 var alerts = nextProps.alerts;
73
74 alerts.map(function (alert, index) {
75 if (!alert.key) {
76 alert.key = (alert.type || 'success') + '-' + new Date().getTime();
77 }
78 });
79
80 _this.setState({ alerts: alerts });
81 };
82
83 _this.state = {
84 alerts: props.alerts
85 };
86 return _this;
87 }
88
89 _createClass(AlertSystem, [{
90 key: 'render',
91 value: function render() {
92 var cx = _bind2.default.bind(_style2.default);
93 var slideInClass = this.props.slideIn ? _style2.default['slide-in-right'] : null;
94 var alertSystemClasses = cx(_style2.default['alert-system'], slideInClass, this.props.optClass);
95
96 return _react2.default.createElement(
97 'div',
98 { className: alertSystemClasses },
99 this.getAlerts()
100 );
101 }
102 }]);
103
104 return AlertSystem;
105}(_react2.default.Component);
106
107AlertSystem.propTypes = {
108 /**
109 * The alerts to display.
110 */
111 alerts: _propTypes2.default.array.isRequired,
112 /**
113 * Optional styles to add to the alert system component.
114 */
115 optClass: _propTypes2.default.string,
116 /**
117 * Whether or not to slide the alerts in from the right
118 */
119 slideIn: _propTypes2.default.bool
120};
121exports.default = AlertSystem;
\No newline at end of file