yzsd
Version:
a niubi UI component
61 lines • 2.36 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { forwardRef, useEffect } from 'react';
import classNames from 'classnames';
var notice = /*#__PURE__*/forwardRef(function (props, ref) {
var prefixCls = props.prefixCls,
style = props.style,
className = props.className,
_props$duration = props.duration,
duration = _props$duration === void 0 ? 4.5 : _props$duration,
eventKey = props.eventKey,
content = props.content,
closable = props.closable,
_props$closeIcon = props.closeIcon,
closeIcon = _props$closeIcon === void 0 ? 'x' : _props$closeIcon,
divProps = props.props,
onClick = props.onClick,
onNoticeClose = props.onNoticeClose,
times = props.times;
var _React$useState = React.useState(false),
_React$useState2 = _slicedToArray(_React$useState, 2),
hovering = _React$useState2[0],
setHovering = _React$useState2[1];
var onInternalClose = function onInternalClose() {
onNoticeClose === null || onNoticeClose === void 0 ? void 0 : onNoticeClose(eventKey);
};
useEffect(function () {
if (duration && duration > 0 && !hovering) {
var timeout = setTimeout(function () {
onInternalClose();
}, duration * 1000);
return function () {
return clearInterval(timeout);
};
}
}, [hovering, duration, times]);
return /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
ref: ref,
className: classNames("".concat(prefixCls, "-notice"), _defineProperty({}, "".concat(prefixCls, "-notice-closable"), closable), className),
style: style,
onMouseEnter: function onMouseEnter() {
return setHovering(true);
},
onMouseLeave: function onMouseLeave() {
return setHovering(false);
},
onClick: onClick
}), /*#__PURE__*/React.createElement("div", {
className: classNames("".concat(prefixCls, "-notice-content"))
}, content), closable && /*#__PURE__*/React.createElement("a", {
tabIndex: 0,
className: classNames("".concat(prefixCls, "-notice-close")),
onClick: function onClick(e) {
e.preventDefault();
e.stopPropagation();
onInternalClose();
}
}, closeIcon));
});
export default notice;