Notice.js
4.33 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var React = _interopRequireWildcard(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _classnames = _interopRequireDefault(require("classnames"));
var Notice = /*#__PURE__*/function (_Component) {
(0, _inherits2.default)(Notice, _Component);
var _super = (0, _createSuper2.default)(Notice);
function Notice() {
var _this;
(0, _classCallCheck2.default)(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;
}
(0, _createClass2.default)(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: (0, _classnames.default)(componentClass, className, (0, _defineProperty2.default)({}, "".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.default.createPortal(node, holder);
}
return node;
}
}]);
return Notice;
}(React.Component);
exports.default = Notice;
Notice.defaultProps = {
onClose: function onClose() {},
duration: 1.5
};