react-errors
Version:
Show errors on the top-right
122 lines (104 loc) • 4.47 kB
JavaScript
;
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; }; }();
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
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; } /* eslint react/jsx-no-bind:0 */
var styles = {
errorsContainer: {
textAlign: 'left',
fontSize: '14px',
lineHeight: '21px',
position: 'fixed',
top: 0,
right: 0,
zIndex: 999999999,
fontFamily: '微软雅黑, "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif'
},
errorContainer: {
margin: 15,
padding: 15,
width: 200,
// http://v4-alpha.getbootstrap.com/components/utilities/#contextual-colors-and-backgrounds
backgroundColor: '#d9534f',
color: 'white',
// borderRadius: 4,
// Like bootstrap .popover class
// http://getbootstrap.com/javascript/#live-demo-1
boxShadow: '0 5px 10px rgba(0, 0, 0, .2)',
boxSizing: 'border-box'
},
errorP: {
marginTop: 0,
marginBottom: 0,
wordBreak: 'break-word'
},
errorClose: {
float: 'right',
fontSize: '18px',
fontWeight: 'bold',
lineHeight: 1,
cursor: 'pointer',
marginLeft: 8
}
};
/**
* Errors Component
*
* Warning: This component will render exactly what messages in `errors`,
* so you should change `errors` whenever `onErrorClose` triggered.
*
* @props errors array of error, each error should be a instance of
* `Error` or an object which contains `message`
* @props onErrorClose trigger when the close button is clicked, with a param
* which is the index of errors
*/
var Errors = function (_Component) {
_inherits(Errors, _Component);
function Errors() {
_classCallCheck(this, Errors);
return _possibleConstructorReturn(this, Object.getPrototypeOf(Errors).apply(this, arguments));
}
_createClass(Errors, [{
key: 'render',
value: function render() {
var _this2 = this;
return _react2.default.createElement(
'div',
{ style: styles.errorsContainer },
this.props.errors.map(function (error, index) {
return _react2.default.createElement(
'div',
{ style: styles.errorContainer, key: index + error.message },
_react2.default.createElement(
'span',
{
style: styles.errorClose,
onClick: _this2.props.onErrorClose.bind(null, index)
},
'×'
),
_react2.default.createElement(
'p',
{ style: styles.errorP },
error.message
)
);
})
);
}
}]);
return Errors;
}(_react.Component);
Errors.propTypes = {
errors: _react.PropTypes.arrayOf(_react.PropTypes.shape({
message: _react.PropTypes.string.isRequired
})).isRequired,
onErrorClose: _react.PropTypes.func.isRequired
};
exports.default = Errors;