amis-rpc-design/node_modules/rc-notification/es/Notice.js
2023-10-07 19:42:30 +08:00

76 lines
2.7 KiB
JavaScript

import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import classNames from 'classnames';
import KeyCode from "rc-util/es/KeyCode";
import * as React from 'react';
var Notify = /*#__PURE__*/React.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];
// ======================== Close =========================
var onInternalClose = function onInternalClose() {
onNoticeClose(eventKey);
};
var onCloseKeyDown = function onCloseKeyDown(e) {
if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === KeyCode.ENTER) {
onInternalClose();
}
};
// ======================== Effect ========================
React.useEffect(function () {
if (!hovering && duration > 0) {
var timeout = setTimeout(function () {
onInternalClose();
}, duration * 1000);
return function () {
clearTimeout(timeout);
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [duration, hovering, times]);
// ======================== Render ========================
var noticePrefixCls = "".concat(prefixCls, "-notice");
return /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
ref: ref,
className: classNames(noticePrefixCls, className, _defineProperty({}, "".concat(noticePrefixCls, "-closable"), closable)),
style: style,
onMouseEnter: function onMouseEnter() {
setHovering(true);
},
onMouseLeave: function onMouseLeave() {
setHovering(false);
},
onClick: onClick
}), /*#__PURE__*/React.createElement("div", {
className: "".concat(noticePrefixCls, "-content")
}, content), closable && /*#__PURE__*/React.createElement("a", {
tabIndex: 0,
className: "".concat(noticePrefixCls, "-close"),
onKeyDown: onCloseKeyDown,
onClick: function onClick(e) {
e.preventDefault();
e.stopPropagation();
onInternalClose();
}
}, closeIcon));
});
export default Notify;