react-ions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
130 lines (98 loc) • 4.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _StyledDiv = require('../StyledDiv');
var _StyledDiv2 = _interopRequireDefault(_StyledDiv);
var _Alert = require('./Alert');
var _Alert2 = _interopRequireDefault(_Alert);
var _styles = require('./styles.css');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _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; }
function _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; }
var AlertSystem = function (_Component) {
_inherits(AlertSystem, _Component);
function AlertSystem(props) {
_classCallCheck(this, AlertSystem);
var _this = _possibleConstructorReturn(this, (AlertSystem.__proto__ || Object.getPrototypeOf(AlertSystem)).call(this, props));
_this.state = {
alerts: _this.props.alerts
};
_this.getAlerts = function () {
return _this.state.alerts.map(function (alert) {
return !alert.hidden && _react2.default.createElement(
_Alert2.default,
{
key: alert.key,
type: alert.type,
className: alert.class || '',
closable: alert.closable,
timeout: alert.timeout,
onClose: _this.removeAlert.bind(_this, alert),
slideIn: _this.props.slideIn
},
alert.content
);
});
};
_this.removeAlert = function (alert) {
var alerts = _this.state.alerts;
alerts.map(function (a) {
if (alert.key === a.key) {
// Hide the alert
a.hidden = true;
// Call its onCose callback if provided
if (typeof a.onClose === 'function') {
a.onClose(alert);
}
}
});
_this.setState({ alerts: alerts });
};
_this.render = function () {
return _react2.default.createElement(
_StyledDiv2.default,
{ className: _this.props.className, css: (0, _styles.alertSystemWrapper)(_this.props.slideIn) },
_this.getAlerts()
);
};
if (props.optClass && process.env.NODE_ENV !== 'production') {
console.warn('AlertSystem: Use of optClass will be deprecated as of react-ions 6.0.0, please use `className` instead');
}
return _this;
}
_createClass(AlertSystem, null, [{
key: 'getDerivedStateFromProps',
value: function getDerivedStateFromProps(nextProps) {
var alerts = nextProps.alerts;
alerts.map(function (alert) {
// Add a unique key to the alert if one is not provided
if (!alert.key) {
alert.key = (alert.type || 'success') + '-' + new Date().getTime();
}
});
return { alerts: alerts };
}
}]);
return AlertSystem;
}(_react.Component);
AlertSystem.propTypes = {
/**
* The alerts to display.
*/
alerts: _propTypes.array.isRequired,
/**
* A class to add to the alert system component.
*/
className: _propTypes.string,
/**
* Whether or not to slide the alerts in from the right
*/
slideIn: _propTypes.bool
};
exports.default = AlertSystem;