react-ions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
138 lines (109 loc) • 4.52 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _Icon = require('../Icon');
var _Icon2 = _interopRequireDefault(_Icon);
var _StyledDiv = require('../StyledDiv');
var _StyledDiv2 = _interopRequireDefault(_StyledDiv);
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 Alert = function (_Component) {
_inherits(Alert, _Component);
function Alert(props) {
_classCallCheck(this, Alert);
var _this = _possibleConstructorReturn(this, (Alert.__proto__ || Object.getPrototypeOf(Alert)).call(this, props));
_this.state = {
timerStart: 0,
timeout: _this.props.timeout,
timer: false
};
_this.componentDidMount = function () {
_this.startTimer();
};
_this.startTimer = function () {
if (!_this.props.timeout) return;
_this.setState({
timerStart: new Date(),
timer: setTimeout(_this.closeAlert, _this.state.timeout)
});
};
_this.pauseTimer = function () {
if (!_this.state.timer) return;
clearTimeout(_this.state.timer);
var timeout = _this.state.timeout;
timeout -= new Date() - _this.state.timerStart;
_this.setState({ timeout: timeout });
};
_this.closeAlert = function () {
if (_this.state.timer) clearTimeout(_this.state.timer);
if (_this.props.onClose) _this.props.onClose();
};
_this.render = function () {
return _react2.default.createElement(
_StyledDiv2.default,
{ className: _this.props.className, css: (0, _styles.alertWrapper)(_this.props.type, _this.props.closable, _this.props.slideIn), onMouseEnter: _this.pauseTimer, onMouseLeave: _this.startTimer },
_react2.default.createElement(_Icon2.default, { name: 'md-' + _this.props.type, width: '17', height: '17' }),
_react2.default.createElement(
'div',
null,
_this.props.children
),
_this.props.closable && _react2.default.createElement(
_StyledDiv2.default,
{ css: _styles.closeIcon, onClick: _this.closeAlert },
_react2.default.createElement(_Icon2.default, { name: 'md-close', width: '12', height: '12' })
),
_this.props.countdownBar && !!_this.props.timeout && _react2.default.createElement(
_StyledDiv2.default,
{ css: _styles.countdownBarWrapper },
_react2.default.createElement(_StyledDiv2.default, { className: 'countdown-bar', css: (0, _styles.countdownBar)(_this.props.type, _this.props.timeout) })
)
);
};
return _this;
}
return Alert;
}(_react.Component);
Alert.propTypes = {
/**
* A class to add to the alert component.
*/
className: _propTypes.string,
/**
* Whether the alert can be closed.
*/
closable: _propTypes.bool,
/**
* Whether to show the countdown bar.
*/
countdownBar: _propTypes.bool,
/**
* A callback to be triggered when the close icon is clicked or when the timeout expires.
*/
onClose: _propTypes.func,
/**
* Whether or not to slide the alert in from the right.
*/
slideIn: _propTypes.bool,
/**
* How long before the alert disappears.
*/
timeout: _propTypes.number,
/**
* The alert type.
*/
type: (0, _propTypes.oneOf)(['success', 'warning', 'info', 'danger'])
};
Alert.defaultProps = {
closable: true,
countdownBar: true,
type: 'success'
};
exports.default = Alert;