Notice.js
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import * as React from 'react';
import { Component } from 'react';
import ReactDOM from 'react-dom';
import classNames from 'classnames';
var Notice = /*#__PURE__*/function (_Component) {
_inherits(Notice, _Component);
var _super = _createSuper(Notice);
function Notice() {
var _this;
_classCallCheck(this, Notice);
_this = _super.apply(this, arguments);
_this.closeTimer = null;
_this.close = function (e) {
if (e) {
e.stopPropagation();
}
_this.clearCloseTimer();
var _this$props = _this.props,
onClose = _this$props.onClose,
noticeKey = _this$props.noticeKey;
if (onClose) {
onClose(noticeKey);
}
};
_this.startCloseTimer = function () {
if (_this.props.duration) {
_this.closeTimer = window.setTimeout(function () {
_this.close();
}, _this.props.duration * 1000);
}
};
_this.clearCloseTimer = function () {
if (_this.closeTimer) {
clearTimeout(_this.closeTimer);
_this.closeTimer = null;
}
};
return _this;
}
_createClass(Notice, [{
key: "componentDidMount",
value: function componentDidMount() {
this.startCloseTimer();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.props.duration !== prevProps.duration || this.props.updateMark !== prevProps.updateMark) {
this.restartCloseTimer();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.clearCloseTimer();
}
}, {
key: "restartCloseTimer",
value: function restartCloseTimer() {
this.clearCloseTimer();
this.startCloseTimer();
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
prefixCls = _this$props2.prefixCls,
className = _this$props2.className,
closable = _this$props2.closable,
closeIcon = _this$props2.closeIcon,
style = _this$props2.style,
onClick = _this$props2.onClick,
children = _this$props2.children,
holder = _this$props2.holder;
var componentClass = "".concat(prefixCls, "-notice");
var dataOrAriaAttributeProps = Object.keys(this.props).reduce(function (acc, key) {
if (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-' || key === 'role') {
acc[key] = _this2.props[key];
}
return acc;
}, {});
var node = React.createElement("div", Object.assign({
className: classNames(componentClass, className, _defineProperty({}, "".concat(componentClass, "-closable"), closable)),
style: style,
onMouseEnter: this.clearCloseTimer,
onMouseLeave: this.startCloseTimer,
onClick: onClick
}, dataOrAriaAttributeProps), React.createElement("div", {
className: "".concat(componentClass, "-content")
}, children), closable ? React.createElement("a", {
tabIndex: 0,
onClick: this.close,
className: "".concat(componentClass, "-close")
}, closeIcon || React.createElement("span", {
className: "".concat(componentClass, "-close-x")
})) : null);
if (holder) {
return ReactDOM.createPortal(node, holder);
}
return node;
}
}]);
return Notice;
}(Component);
export { Notice as default };
Notice.defaultProps = {
onClose: function onClose() {},
duration: 1.5
};