scinote-web/app/javascript/src/components/Alert/index.jsx
2017-09-27 17:34:45 +02:00

83 lines
No EOL
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { Component } from "react";
import PropTypes from "prop-types";
class Alert extends Component {
static alertClass (type) {
const classes = {
error: "alert-danger",
alert: "alert-warning",
notice: "alert-info",
success: "alert-success"
};
return classes[type] || classes.success;
}
static glyphiconClass (type) {
const classes = {
error: "glyphicon-exclamation-sign",
alert: "glyphicon-exclamation-sign",
notice: "glyphicon-info-sign",
success: "glyphicon-ok-sign"
};
return classes[type] || classes.success;
}
componentDidMount() {
this.timer = setTimeout(
this.props.onClose,
this.props.timeout
);
}
componentWillUnmount() {
clearTimeout(this.timer);
}
render() {
const message = this.props.message;
const alertClassName =
`alert
${this.alertClass(message.type)}
alert-dismissable
alert-floating
fade in`;
const glyphiconClassName =
`glyphicon
${this.glyphiconClass(message.type)}`;
return(
<div className={alertClassName}>
<div className="container">
<button type="button"
className="close"
data-dismiss="alert"
aria-label="Close"
onClick={this.props.onClose}
>
<span aria-hidden="true">×</span>
</button>
<span className={glyphiconClassName} />
<span>{message.text}</span>
</div>
</div>
);
}
}
Alert.propTypes = {
onClose: PropTypes.func,
timeout: PropTypes.number,
message: PropTypes.shape({
id: PropTypes.number,
type: PropTypes.string.isRequired,
text: PropTypes.string.isRequired
}).isRequired
};
Alert.defaultProps = {
onClose: null,
timeout: 3000
};
export default Alert;