shineout
Version:
Shein 前端组件库
165 lines (138 loc) • 5 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread";
import React, { PureComponent } from 'react';
import icons from '../icons';
import { isRTL } from '../config';
import { alertClass } from './styles';
import { capitalize } from '../utils/strings';
import { defaultProps } from '../utils/defaultProps';
var DefaultProps = _objectSpread({}, defaultProps, {
iconSize: 16,
duration: 200,
type: 'warning'
});
var Alert =
/*#__PURE__*/
function (_PureComponent) {
_inheritsLoose(Alert, _PureComponent);
function Alert(props) {
var _this;
_this = _PureComponent.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "element", void 0);
_this.state = {
dismiss: 0
};
_this.bindRef = _this.bindRef.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.dismiss = _this.dismiss.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleClose = _this.handleClose.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.renderClose = _this.renderClose.bind(_assertThisInitialized(_assertThisInitialized(_this)));
return _this;
}
var _proto = Alert.prototype;
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
if (this.props.dismiss !== prevProps.dismiss && this.props.dismiss) {
this.handleClose();
}
};
_proto.bindRef = function bindRef(el) {
this.element = el;
};
_proto.dismiss = function dismiss() {
var onClose = this.props.onClose;
this.setState({
dismiss: 2
});
if (typeof onClose === 'function') {
onClose();
}
};
_proto.handleClose = function handleClose() {
var _this2 = this;
if (this.state.dismiss > 0) return;
var _this$props = this.props,
_this$props$duration = _this$props.duration,
duration = _this$props$duration === void 0 ? DefaultProps.duration : _this$props$duration,
outAnimation = _this$props.outAnimation,
onClose = _this$props.onClose; // outer animation
if (outAnimation) {
if (typeof onClose === 'function') {
onClose(duration, this.element.offsetHeight);
}
return;
}
if (duration > 0) {
this.setState({
dismiss: 1
}, function () {
setTimeout(_this2.dismiss, duration);
});
} else {
this.dismiss();
}
};
_proto.renderIcon = function renderIcon() {
var icon = this.props.icon;
var _this$props2 = this.props,
_this$props2$type = _this$props2.type,
type = _this$props2$type === void 0 ? DefaultProps.type : _this$props2$type,
_this$props2$iconSize = _this$props2.iconSize,
iconSize = _this$props2$iconSize === void 0 ? DefaultProps.iconSize : _this$props2$iconSize;
if (typeof icon === 'boolean' && icon) {
icon = icons[capitalize(type)];
}
if (!icon) return null;
var style = {
width: iconSize,
height: iconSize,
marginRight: iconSize / 2
};
if (isRTL()) {
style.marginLeft = style.marginRight;
delete style.marginRight;
}
return React.createElement("div", {
className: alertClass('icon'),
style: style
}, icon);
};
_proto.renderClose = function renderClose() {
var closeItem = this.props.closeItem;
if (React.isValidElement(closeItem)) return React.cloneElement(closeItem, {
onClick: this.handleClose
});
return React.createElement("a", {
className: alertClass('close'),
onClick: this.handleClose
}, closeItem || icons.Close);
};
_proto.render = function render() {
var dismiss = this.state.dismiss;
if (dismiss === 2) return null;
var _this$props3 = this.props,
children = _this$props3.children,
className = _this$props3.className,
type = _this$props3.type,
onClose = _this$props3.onClose,
outAnimation = _this$props3.outAnimation,
hideClose = _this$props3.hideClose;
var icon = this.renderIcon();
var style = this.props.style;
var showClose = onClose && !hideClose;
var wrapClassName = alertClass('_', type, !outAnimation && dismiss === 1 && 'dismissed', showClose && 'with-close', icon && 'with-icon', isRTL() && 'rtl');
if (className) wrapClassName += " " + className;
return React.createElement("div", {
ref: this.bindRef,
className: wrapClassName,
style: style
}, showClose && this.renderClose(), icon, React.createElement("div", {
className: alertClass('content')
}, children));
};
return Alert;
}(PureComponent);
_defineProperty(Alert, "defaultProps", DefaultProps);
_defineProperty(Alert, "displayName", void 0);
Alert.displayName = 'ShineoutAlert';
export default Alert;