@ucloud-fe/react-components 1.5.3 → 1.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/main.min.js +1 -1
- package/lib/hooks/useOverflow.js +35 -16
- package/package.json +1 -1
package/lib/hooks/useOverflow.js
CHANGED
|
@@ -21,6 +21,8 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
|
21
21
|
|
|
22
22
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
23
|
|
|
24
|
+
// https://github.com/que-etc/resize-observer-polyfill/issues/80
|
|
25
|
+
// 由于 ts 官方增加了 contentRect 的类型定义,导致和 resize-observer-polyfill 内部的定义冲突,目前先以 "skipLibCheck" 解决
|
|
24
26
|
var DEFAULT_DEP = [];
|
|
25
27
|
/**
|
|
26
28
|
* 计算容器内的元素最大展示数量
|
|
@@ -38,34 +40,47 @@ var useOverflow = function useOverflow(_ref) {
|
|
|
38
40
|
minCount = _ref$minCount === void 0 ? 0 : _ref$minCount;
|
|
39
41
|
var deps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_DEP;
|
|
40
42
|
|
|
43
|
+
// the count of trying to show
|
|
41
44
|
var _useState = (0, _react.useState)(function () {
|
|
42
45
|
(0, _newArrowCheck2.default)(this, _this);
|
|
43
46
|
return Math.min(defaultCount, maxCount);
|
|
44
47
|
}.bind(this)),
|
|
45
48
|
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
|
46
49
|
count = _useState2[0],
|
|
47
|
-
setCount = _useState2[1];
|
|
50
|
+
setCount = _useState2[1]; // the count valid to show
|
|
51
|
+
|
|
48
52
|
|
|
49
53
|
var _useState3 = (0, _react.useState)(count),
|
|
50
54
|
_useState4 = (0, _slicedToArray2.default)(_useState3, 2),
|
|
51
55
|
measureCount = _useState4[0],
|
|
52
|
-
setMeasureCount = _useState4[1];
|
|
56
|
+
setMeasureCount = _useState4[1]; // latest valid count
|
|
57
|
+
|
|
53
58
|
|
|
54
59
|
var _useState5 = (0, _react.useState)(null),
|
|
55
60
|
_useState6 = (0, _slicedToArray2.default)(_useState5, 2),
|
|
56
61
|
latestValidCount = _useState6[0],
|
|
57
|
-
setLatestValidCount = _useState6[1];
|
|
62
|
+
setLatestValidCount = _useState6[1]; // the container element
|
|
63
|
+
|
|
58
64
|
|
|
59
65
|
var _useState7 = (0, _react.useState)(true),
|
|
60
66
|
_useState8 = (0, _slicedToArray2.default)(_useState7, 2),
|
|
61
67
|
measuring = _useState8[0],
|
|
62
|
-
setMeasuring = _useState8[1];
|
|
68
|
+
setMeasuring = _useState8[1]; // save to ref for use in resize observer to avoid frequently create new observer
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
var measuringRef = (0, _react.useRef)(measuring);
|
|
72
|
+
(0, _react.useEffect)(function () {
|
|
73
|
+
(0, _newArrowCheck2.default)(this, _this);
|
|
74
|
+
measuringRef.current = measuring;
|
|
75
|
+
}.bind(this), [measuring]); // start to measure
|
|
63
76
|
|
|
64
77
|
var startMeasuring = (0, _react.useCallback)(function () {
|
|
65
78
|
(0, _newArrowCheck2.default)(this, _this);
|
|
79
|
+
if (measuringRef.current) return;
|
|
66
80
|
setMeasuring(true);
|
|
67
81
|
setLatestValidCount(null);
|
|
68
|
-
}.bind(this), []);
|
|
82
|
+
}.bind(this), []); // stop measuring
|
|
83
|
+
|
|
69
84
|
var endMeasuring = (0, _react.useCallback)(function (measureCount) {
|
|
70
85
|
(0, _newArrowCheck2.default)(this, _this);
|
|
71
86
|
setMeasuring(false);
|
|
@@ -73,6 +88,7 @@ var useOverflow = function useOverflow(_ref) {
|
|
|
73
88
|
}.bind(this), []);
|
|
74
89
|
(0, _react.useEffect)(function () {
|
|
75
90
|
(0, _newArrowCheck2.default)(this, _this);
|
|
91
|
+
// console.log('reset when deps changed');
|
|
76
92
|
startMeasuring(); // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
93
|
}.bind(this), deps);
|
|
78
94
|
(0, _react.useEffect)(function () {
|
|
@@ -80,12 +96,7 @@ var useOverflow = function useOverflow(_ref) {
|
|
|
80
96
|
// reset when config changed
|
|
81
97
|
startMeasuring();
|
|
82
98
|
}.bind(this), [minCount, maxCount, startMeasuring]);
|
|
83
|
-
var measuringRef = (0, _react.useRef)(measuring);
|
|
84
99
|
(0, _react.useEffect)(function () {
|
|
85
|
-
(0, _newArrowCheck2.default)(this, _this);
|
|
86
|
-
measuringRef.current = measuring;
|
|
87
|
-
}.bind(this), [measuring]);
|
|
88
|
-
(0, _react.useLayoutEffect)(function () {
|
|
89
100
|
var _this2 = this;
|
|
90
101
|
|
|
91
102
|
(0, _newArrowCheck2.default)(this, _this);
|
|
@@ -97,8 +108,11 @@ var useOverflow = function useOverflow(_ref) {
|
|
|
97
108
|
(0, _newArrowCheck2.default)(this, _this2);
|
|
98
109
|
|
|
99
110
|
if (!measuringRef.current) {
|
|
100
|
-
startMeasuring();
|
|
101
|
-
}
|
|
111
|
+
startMeasuring(); // console.log('reset when width changed');
|
|
112
|
+
} // else {
|
|
113
|
+
// console.log('lock when width changed');
|
|
114
|
+
// }
|
|
115
|
+
|
|
102
116
|
}.bind(this));
|
|
103
117
|
resizeObserver.observe(containerDOM);
|
|
104
118
|
}
|
|
@@ -110,11 +124,13 @@ var useOverflow = function useOverflow(_ref) {
|
|
|
110
124
|
resizeObserver.disconnect();
|
|
111
125
|
}
|
|
112
126
|
}.bind(this);
|
|
113
|
-
}.bind(this), [containerRef, startMeasuring]);
|
|
127
|
+
}.bind(this), [containerRef, startMeasuring]); // console.log({ count, latestValidCount, maxCount, minCount, measuring });
|
|
128
|
+
// use layout effect to measure can avoid most shaking
|
|
129
|
+
|
|
114
130
|
(0, _react.useLayoutEffect)(function () {
|
|
115
131
|
(0, _newArrowCheck2.default)(this, _this);
|
|
116
132
|
var containerDOM = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current;
|
|
117
|
-
if (!containerDOM) return;
|
|
133
|
+
if (!containerDOM) return; // measure end
|
|
118
134
|
|
|
119
135
|
if (count === latestValidCount) {
|
|
120
136
|
endMeasuring(latestValidCount);
|
|
@@ -124,10 +140,13 @@ var useOverflow = function useOverflow(_ref) {
|
|
|
124
140
|
var newCount = count;
|
|
125
141
|
|
|
126
142
|
if (containerDOM.offsetWidth >= containerDOM.scrollWidth) {
|
|
127
|
-
setLatestValidCount(count);
|
|
143
|
+
setLatestValidCount(count); // try to show more
|
|
144
|
+
|
|
128
145
|
newCount = Math.min(count + 1, maxCount);
|
|
129
146
|
} else if (containerDOM.offsetWidth < containerDOM.scrollWidth) {
|
|
130
|
-
if
|
|
147
|
+
// if container with not enough space event when count is 0, it will be set to 0
|
|
148
|
+
if (count === 0) setLatestValidCount(count); // try to show less
|
|
149
|
+
|
|
131
150
|
newCount = Math.max(0, minCount, count - 1);
|
|
132
151
|
}
|
|
133
152
|
|