@enact/ui 5.4.3 → 5.4.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/Scroller/Scroller.d.ts +2 -2
- package/Scroller/ScrollerBasic.js +27 -14
- package/Scroller/tests/ScrollerBasic-specs.js +187 -14
- package/package.json +7 -7
- package/useScroll/tests/useScroll-specs.js +180 -0
- package/useScroll/useScroll.js +3 -1
package/CHANGELOG.md
CHANGED
package/Scroller/Scroller.d.ts
CHANGED
|
@@ -26,8 +26,8 @@ export class ScrollerBasic extends React.Component<
|
|
|
26
26
|
/**
|
|
27
27
|
* Programmatically animates the native scroll position of `node` toward the target `left` / `top`
|
|
28
28
|
offsets using `requestAnimationFrame` . It computes the scroll direction on each axis, then repeatedly
|
|
29
|
-
calls `scrollBy`
|
|
30
|
-
|
|
29
|
+
calls `scrollBy` with a dynamic step (10% of remaining distance, minimum 8px) until the target is reached
|
|
30
|
+
or 1s has elapsed. Used as a Chrome fallback when repeating a smooth scroll.
|
|
31
31
|
*/
|
|
32
32
|
animateScroll(any, any, any): void;
|
|
33
33
|
}
|
|
@@ -151,6 +151,10 @@ var ScrollerBasic = exports.ScrollerBasic = /*#__PURE__*/function (_Component) {
|
|
|
151
151
|
if (_platform.platform.chrome && smoothBehavior && repeat) {
|
|
152
152
|
this.animateScroll(this.getRtlPositionX(left), top, node);
|
|
153
153
|
} else {
|
|
154
|
+
if (this.scrollAnimationId) {
|
|
155
|
+
window.cancelAnimationFrame(this.scrollAnimationId);
|
|
156
|
+
this.scrollAnimationId = null;
|
|
157
|
+
}
|
|
154
158
|
node.scrollTo({
|
|
155
159
|
left: this.getRtlPositionX(left),
|
|
156
160
|
top: top,
|
|
@@ -162,8 +166,8 @@ var ScrollerBasic = exports.ScrollerBasic = /*#__PURE__*/function (_Component) {
|
|
|
162
166
|
/**
|
|
163
167
|
* Programmatically animates the native scroll position of `node` toward the target `left`/`top`
|
|
164
168
|
* offsets using `requestAnimationFrame`. It computes the scroll direction on each axis, then repeatedly
|
|
165
|
-
* calls `scrollBy`
|
|
166
|
-
*
|
|
169
|
+
* calls `scrollBy` with a dynamic step (10% of remaining distance, minimum 8px) until the target is reached
|
|
170
|
+
* or 1s has elapsed. Used as a Chrome fallback when repeating a smooth scroll.
|
|
167
171
|
*/
|
|
168
172
|
}, {
|
|
169
173
|
key: "animateScroll",
|
|
@@ -171,23 +175,32 @@ var ScrollerBasic = exports.ScrollerBasic = /*#__PURE__*/function (_Component) {
|
|
|
171
175
|
var _this3 = this;
|
|
172
176
|
var directionX = Math.sign(left - node.scrollLeft);
|
|
173
177
|
var directionY = Math.sign(top - node.scrollTop);
|
|
174
|
-
var
|
|
178
|
+
var startTime = performance.now();
|
|
179
|
+
var _animateScroll = function animateScroll(currentTime) {
|
|
180
|
+
var elapsed = (currentTime - startTime) / 1000;
|
|
175
181
|
var scrollLeft = directionX > 0 ? node.scrollLeft < left : node.scrollLeft > left;
|
|
176
182
|
var scrollTop = directionY > 0 ? node.scrollTop < top : node.scrollTop > top;
|
|
177
183
|
|
|
178
|
-
//
|
|
179
|
-
if (
|
|
180
|
-
|
|
184
|
+
// Stop animating if the target is reached or 1s has elapsed since animation started
|
|
185
|
+
if (!scrollTop && !scrollLeft || elapsed > 1) {
|
|
186
|
+
// Fallback: if timed out before reaching target, jump there with smooth scroll
|
|
187
|
+
if (elapsed > 1 && (node.scrollLeft !== left || node.scrollTop !== top)) {
|
|
188
|
+
node.scrollTo({
|
|
189
|
+
top: top,
|
|
190
|
+
left: left,
|
|
191
|
+
behavior: 'smooth'
|
|
192
|
+
});
|
|
193
|
+
}
|
|
181
194
|
return;
|
|
182
195
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
196
|
+
var horizontalScrollFactor = Math.max(Math.abs(left - node.scrollLeft) * 0.1, 8);
|
|
197
|
+
var verticalScrollFactor = Math.max(Math.abs(top - node.scrollTop) * 0.1, 8);
|
|
198
|
+
node.scrollBy({
|
|
199
|
+
top: directionY * verticalScrollFactor,
|
|
200
|
+
left: directionX * horizontalScrollFactor,
|
|
201
|
+
behavior: 'instant'
|
|
202
|
+
});
|
|
203
|
+
_this3.scrollAnimationId = window.requestAnimationFrame(_animateScroll);
|
|
191
204
|
};
|
|
192
205
|
this.scrollAnimationId = window.requestAnimationFrame(_animateScroll);
|
|
193
206
|
}
|
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
require("@testing-library/jest-dom");
|
|
4
4
|
var _Scroller = require("../Scroller");
|
|
5
|
+
var mockPlatform = {};
|
|
6
|
+
jest.mock('@enact/core/platform', function () {
|
|
7
|
+
return {
|
|
8
|
+
get platform() {
|
|
9
|
+
return mockPlatform;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
});
|
|
5
13
|
describe('ScrollBasic', function () {
|
|
6
14
|
var scrollContentRef;
|
|
7
15
|
beforeEach(function () {
|
|
@@ -18,7 +26,7 @@ describe('ScrollBasic', function () {
|
|
|
18
26
|
afterEach(function () {
|
|
19
27
|
jest.clearAllMocks();
|
|
20
28
|
});
|
|
21
|
-
test('should call
|
|
29
|
+
test('should call scrollTo on scrollToPosition', function () {
|
|
22
30
|
var instance = new _Scroller.ScrollerBasic({
|
|
23
31
|
scrollContentRef: scrollContentRef,
|
|
24
32
|
direction: 'both'
|
|
@@ -36,24 +44,189 @@ describe('ScrollBasic', function () {
|
|
|
36
44
|
behavior: 'instant'
|
|
37
45
|
});
|
|
38
46
|
});
|
|
39
|
-
test('should
|
|
40
|
-
var rafCallback;
|
|
47
|
+
test('should cancel pending animation before calling scrollTo on scrollToPosition', function () {
|
|
41
48
|
var instance = new _Scroller.ScrollerBasic({
|
|
42
49
|
scrollContentRef: scrollContentRef,
|
|
43
50
|
direction: 'both'
|
|
44
51
|
});
|
|
45
|
-
|
|
46
|
-
instance.
|
|
47
|
-
window.
|
|
48
|
-
|
|
52
|
+
var mockAnimationId = 42;
|
|
53
|
+
instance.scrollAnimationId = mockAnimationId;
|
|
54
|
+
window.cancelAnimationFrame = jest.fn();
|
|
55
|
+
instance.scrollToPosition(100, 200, 'instant');
|
|
56
|
+
expect(window.cancelAnimationFrame).toHaveBeenCalledWith(mockAnimationId);
|
|
57
|
+
expect(instance.scrollAnimationId).toBeNull();
|
|
58
|
+
expect(scrollContentRef.current.scrollTo).toHaveBeenCalledWith({
|
|
59
|
+
left: 100,
|
|
60
|
+
top: 200,
|
|
61
|
+
behavior: 'instant'
|
|
49
62
|
});
|
|
63
|
+
});
|
|
64
|
+
test('should not call cancelAnimationFrame when scrollAnimationId is null on scrollToPosition', function () {
|
|
65
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
66
|
+
scrollContentRef: scrollContentRef,
|
|
67
|
+
direction: 'both'
|
|
68
|
+
});
|
|
69
|
+
instance.scrollAnimationId = null;
|
|
50
70
|
window.cancelAnimationFrame = jest.fn();
|
|
51
|
-
instance.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
71
|
+
instance.scrollToPosition(100, 200, 'instant');
|
|
72
|
+
expect(window.cancelAnimationFrame).not.toHaveBeenCalled();
|
|
73
|
+
});
|
|
74
|
+
test('should call animateScroll instead of scrollTo when platform is Chrome, behavior is smooth, and repeat is true', function () {
|
|
75
|
+
mockPlatform.chrome = 132;
|
|
76
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
77
|
+
scrollContentRef: scrollContentRef,
|
|
78
|
+
direction: 'both'
|
|
79
|
+
});
|
|
80
|
+
instance.animateScroll = jest.fn();
|
|
81
|
+
instance.scrollToPosition(100, 200, 'smooth', true);
|
|
82
|
+
expect(instance.animateScroll).toHaveBeenCalledWith(100, 200, scrollContentRef.current);
|
|
83
|
+
expect(scrollContentRef.current.scrollTo).not.toHaveBeenCalled();
|
|
84
|
+
delete mockPlatform.chrome;
|
|
85
|
+
});
|
|
86
|
+
describe('animateScroll', function () {
|
|
87
|
+
var rafCallbacks;
|
|
88
|
+
var rafIdCounter;
|
|
89
|
+
var nowValue;
|
|
90
|
+
beforeEach(function () {
|
|
91
|
+
rafCallbacks = [];
|
|
92
|
+
rafIdCounter = 1;
|
|
93
|
+
nowValue = 0;
|
|
94
|
+
window.requestAnimationFrame = jest.fn(function (cb) {
|
|
95
|
+
var id = rafIdCounter++;
|
|
96
|
+
rafCallbacks.push({
|
|
97
|
+
id: id,
|
|
98
|
+
cb: cb
|
|
99
|
+
});
|
|
100
|
+
return id;
|
|
101
|
+
});
|
|
102
|
+
window.cancelAnimationFrame = jest.fn(function (id) {
|
|
103
|
+
rafCallbacks = rafCallbacks.filter(function (entry) {
|
|
104
|
+
return entry.id !== id;
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
jest.spyOn(performance, 'now').mockImplementation(function () {
|
|
108
|
+
return nowValue;
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
afterEach(function () {
|
|
112
|
+
jest.restoreAllMocks();
|
|
113
|
+
});
|
|
114
|
+
function flushRaf(time) {
|
|
115
|
+
nowValue = time;
|
|
116
|
+
var callbacks = rafCallbacks.splice(0);
|
|
117
|
+
callbacks.forEach(function (_ref) {
|
|
118
|
+
var cb = _ref.cb;
|
|
119
|
+
return cb(time);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
test('should call scrollBy with a dynamic step (10% of remaining distance) during animateScroll', function () {
|
|
123
|
+
scrollContentRef.current.scrollLeft = 0;
|
|
124
|
+
scrollContentRef.current.scrollTop = 0;
|
|
125
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
126
|
+
scrollContentRef: scrollContentRef,
|
|
127
|
+
direction: 'both'
|
|
128
|
+
});
|
|
129
|
+
var node = scrollContentRef.current;
|
|
130
|
+
instance.animateScroll(200, 100, node);
|
|
131
|
+
|
|
132
|
+
// First frame at t=0
|
|
133
|
+
flushRaf(0);
|
|
134
|
+
expect(node.scrollBy).toHaveBeenCalledWith(expect.objectContaining({
|
|
135
|
+
left: expect.closeTo(20, 1),
|
|
136
|
+
// 10% of 200
|
|
137
|
+
top: expect.closeTo(10, 1),
|
|
138
|
+
// 10% of 100
|
|
139
|
+
behavior: 'instant'
|
|
140
|
+
}));
|
|
141
|
+
});
|
|
142
|
+
test('should use minimum step of 8px when remaining distance is less than 80px', function () {
|
|
143
|
+
scrollContentRef.current.scrollLeft = 0;
|
|
144
|
+
scrollContentRef.current.scrollTop = 0;
|
|
145
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
146
|
+
scrollContentRef: scrollContentRef,
|
|
147
|
+
direction: 'both'
|
|
148
|
+
});
|
|
149
|
+
var node = scrollContentRef.current;
|
|
150
|
+
|
|
151
|
+
// 50px remaining → 10% = 5px < 8px minimum
|
|
152
|
+
instance.animateScroll(50, 50, node);
|
|
153
|
+
flushRaf(0);
|
|
154
|
+
expect(node.scrollBy).toHaveBeenCalledWith(expect.objectContaining({
|
|
155
|
+
left: 8,
|
|
156
|
+
top: 8,
|
|
157
|
+
behavior: 'instant'
|
|
158
|
+
}));
|
|
159
|
+
});
|
|
160
|
+
test('should stop animating and not call scrollBy when target is already reached', function () {
|
|
161
|
+
scrollContentRef.current.scrollLeft = 100;
|
|
162
|
+
scrollContentRef.current.scrollTop = 200;
|
|
163
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
164
|
+
scrollContentRef: scrollContentRef,
|
|
165
|
+
direction: 'both'
|
|
166
|
+
});
|
|
167
|
+
var node = scrollContentRef.current;
|
|
168
|
+
|
|
169
|
+
// Target equals current position → already reached
|
|
170
|
+
instance.animateScroll(100, 200, node);
|
|
171
|
+
flushRaf(0);
|
|
172
|
+
expect(node.scrollBy).not.toHaveBeenCalled();
|
|
173
|
+
expect(node.scrollTo).not.toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
test('should stop animation and call scrollTo with smooth behavior when 1s timeout elapses before reaching target', function () {
|
|
176
|
+
scrollContentRef.current.scrollLeft = 0;
|
|
177
|
+
scrollContentRef.current.scrollTop = 0;
|
|
178
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
179
|
+
scrollContentRef: scrollContentRef,
|
|
180
|
+
direction: 'both'
|
|
181
|
+
});
|
|
182
|
+
var node = scrollContentRef.current;
|
|
183
|
+
instance.animateScroll(500, 500, node);
|
|
184
|
+
|
|
185
|
+
// Advance past 1s timeout without updating scroll position
|
|
186
|
+
flushRaf(1100);
|
|
187
|
+
expect(node.scrollTo).toHaveBeenCalledWith({
|
|
188
|
+
top: 500,
|
|
189
|
+
left: 500,
|
|
190
|
+
behavior: 'smooth'
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
test('should not call scrollTo fallback when target is reached exactly at timeout boundary', function () {
|
|
194
|
+
scrollContentRef.current.scrollLeft = 500;
|
|
195
|
+
scrollContentRef.current.scrollTop = 500;
|
|
196
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
197
|
+
scrollContentRef: scrollContentRef,
|
|
198
|
+
direction: 'both'
|
|
199
|
+
});
|
|
200
|
+
var node = scrollContentRef.current;
|
|
201
|
+
|
|
202
|
+
// Target already reached
|
|
203
|
+
instance.animateScroll(500, 500, node);
|
|
204
|
+
flushRaf(1100);
|
|
205
|
+
expect(node.scrollTo).not.toHaveBeenCalled();
|
|
206
|
+
});
|
|
207
|
+
test('should continue requesting animation frames until target is reached', function () {
|
|
208
|
+
scrollContentRef.current.scrollLeft = 0;
|
|
209
|
+
scrollContentRef.current.scrollTop = 0;
|
|
210
|
+
var instance = new _Scroller.ScrollerBasic({
|
|
211
|
+
scrollContentRef: scrollContentRef,
|
|
212
|
+
direction: 'both'
|
|
213
|
+
});
|
|
214
|
+
var node = scrollContentRef.current;
|
|
215
|
+
instance.animateScroll(100, 0, node);
|
|
216
|
+
|
|
217
|
+
// First frame: scrollBy called, new rAF registered
|
|
218
|
+
flushRaf(0);
|
|
219
|
+
expect(node.scrollBy).toHaveBeenCalledTimes(1);
|
|
220
|
+
|
|
221
|
+
// Simulate scroll progress
|
|
222
|
+
node.scrollLeft = 50;
|
|
223
|
+
flushRaf(100);
|
|
224
|
+
expect(node.scrollBy).toHaveBeenCalledTimes(2);
|
|
225
|
+
|
|
226
|
+
// Simulate reaching target
|
|
227
|
+
node.scrollLeft = 100;
|
|
228
|
+
flushRaf(200);
|
|
229
|
+
expect(node.scrollBy).toHaveBeenCalledTimes(2);
|
|
230
|
+
});
|
|
58
231
|
});
|
|
59
232
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enact/ui",
|
|
3
|
-
"version": "5.4.
|
|
3
|
+
"version": "5.4.4",
|
|
4
4
|
"description": "A collection of simplified unstyled cross-platform UI components for Enact",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -32,16 +32,16 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@enact/core": "^5.4.
|
|
36
|
-
"@enact/i18n": "^5.4.
|
|
35
|
+
"@enact/core": "^5.4.4",
|
|
36
|
+
"@enact/i18n": "^5.4.4",
|
|
37
37
|
"classnames": "^2.5.1",
|
|
38
|
-
"ilib": "^14.
|
|
38
|
+
"ilib": "^14.22.0",
|
|
39
39
|
"invariant": "^2.2.4",
|
|
40
40
|
"prop-types": "^15.8.1",
|
|
41
41
|
"ramda": "^0.32.0",
|
|
42
|
-
"react": "^19.2.
|
|
43
|
-
"react-dom": "^19.2.
|
|
44
|
-
"react-is": "^19.2.
|
|
42
|
+
"react": "^19.2.6",
|
|
43
|
+
"react-dom": "^19.2.6",
|
|
44
|
+
"react-is": "^19.2.6",
|
|
45
45
|
"warning": "^4.0.3"
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
require("@testing-library/jest-dom");
|
|
4
|
+
var _react = require("@testing-library/react");
|
|
5
|
+
var _useScroll = require("../useScroll");
|
|
6
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
7
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
8
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
9
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
10
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
11
|
+
var mockPlatform;
|
|
12
|
+
jest.mock('@enact/core/platform', function () {
|
|
13
|
+
return {
|
|
14
|
+
__esModule: true,
|
|
15
|
+
get platform() {
|
|
16
|
+
return mockPlatform;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
function createMockRefs() {
|
|
21
|
+
var el = document.createElement('div');
|
|
22
|
+
document.body.appendChild(el);
|
|
23
|
+
return {
|
|
24
|
+
scrollContainerRef: {
|
|
25
|
+
current: el
|
|
26
|
+
},
|
|
27
|
+
scrollContentHandle: {
|
|
28
|
+
current: {
|
|
29
|
+
getScrollBounds: function getScrollBounds() {
|
|
30
|
+
return {
|
|
31
|
+
maxTop: 0,
|
|
32
|
+
maxLeft: 0,
|
|
33
|
+
clientWidth: 0,
|
|
34
|
+
clientHeight: 0,
|
|
35
|
+
scrollWidth: 0,
|
|
36
|
+
scrollHeight: 0
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
scrollContentRef: {
|
|
42
|
+
current: el
|
|
43
|
+
},
|
|
44
|
+
horizontalScrollbarHandle: {
|
|
45
|
+
current: {}
|
|
46
|
+
},
|
|
47
|
+
verticalScrollbarHandle: {
|
|
48
|
+
current: {}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
4
52
|
describe('useScroll', function () {
|
|
5
53
|
describe('Rounding target scroll position', function () {
|
|
6
54
|
beforeEach(function () {
|
|
@@ -76,4 +124,136 @@ describe('useScroll', function () {
|
|
|
76
124
|
expect(roundedTargetY).toEqual(80.8);
|
|
77
125
|
});
|
|
78
126
|
});
|
|
127
|
+
describe('onKeyDown repeat handling (native scroll mode)', function () {
|
|
128
|
+
beforeEach(function () {
|
|
129
|
+
jest.useFakeTimers();
|
|
130
|
+
mockPlatform = {
|
|
131
|
+
chrome: 132
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
afterEach(function () {
|
|
135
|
+
jest.runOnlyPendingTimers();
|
|
136
|
+
jest.useRealTimers();
|
|
137
|
+
});
|
|
138
|
+
function createNativeScrollProps(mocks) {
|
|
139
|
+
var extra = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
140
|
+
return _objectSpread(_objectSpread({
|
|
141
|
+
direction: 'vertical',
|
|
142
|
+
scrollMode: 'native'
|
|
143
|
+
}, mocks), {}, {
|
|
144
|
+
assignProperties: jest.fn(),
|
|
145
|
+
horizontalScrollbar: 'auto',
|
|
146
|
+
verticalScrollbar: 'auto'
|
|
147
|
+
}, extra);
|
|
148
|
+
}
|
|
149
|
+
test('should forward onKeyDown event when key is not repeated', function () {
|
|
150
|
+
var mocks = createMockRefs();
|
|
151
|
+
var onKeyDown = jest.fn();
|
|
152
|
+
var props = createNativeScrollProps(mocks, {
|
|
153
|
+
onKeyDown: onKeyDown
|
|
154
|
+
});
|
|
155
|
+
(0, _react.renderHook)(function () {
|
|
156
|
+
return (0, _useScroll.useScrollBase)(props);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// eslint-disable-next-line testing-library/no-unnecessary-act
|
|
160
|
+
(0, _react.act)(function () {
|
|
161
|
+
_react.fireEvent.keyDown(mocks.scrollContainerRef.current, {
|
|
162
|
+
keyCode: 40,
|
|
163
|
+
repeat: false
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
expect(onKeyDown).toHaveBeenCalledTimes(1);
|
|
167
|
+
});
|
|
168
|
+
test('should not forward onKeyDown event when arrowKey is repeated and lastInputType is arrowKey', function () {
|
|
169
|
+
var mocks = createMockRefs();
|
|
170
|
+
var onKeyDown = jest.fn();
|
|
171
|
+
var scrollContainerHandle = null;
|
|
172
|
+
var props = createNativeScrollProps(mocks, {
|
|
173
|
+
onKeyDown: onKeyDown,
|
|
174
|
+
setScrollContainerHandle: function setScrollContainerHandle(handle) {
|
|
175
|
+
scrollContainerHandle = handle;
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
(0, _react.renderHook)(function () {
|
|
179
|
+
return (0, _useScroll.useScrollBase)(props);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Set lastInputType to arrowKey via the exposed themeScrollContainerHandle
|
|
183
|
+
(0, _react.act)(function () {
|
|
184
|
+
if (scrollContainerHandle) {
|
|
185
|
+
scrollContainerHandle.lastInputType = 'arrowKey';
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// eslint-disable-next-line testing-library/no-unnecessary-act
|
|
190
|
+
(0, _react.act)(function () {
|
|
191
|
+
_react.fireEvent.keyDown(mocks.scrollContainerRef.current, {
|
|
192
|
+
keyCode: 40,
|
|
193
|
+
repeat: true
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
expect(onKeyDown).not.toHaveBeenCalled();
|
|
197
|
+
});
|
|
198
|
+
test('should not forward onKeyDown event when pageKey is repeated and lastInputType is pageKey', function () {
|
|
199
|
+
var mocks = createMockRefs();
|
|
200
|
+
var onKeyDown = jest.fn();
|
|
201
|
+
var scrollContainerHandle = null;
|
|
202
|
+
var props = createNativeScrollProps(mocks, {
|
|
203
|
+
onKeyDown: onKeyDown,
|
|
204
|
+
setScrollContainerHandle: function setScrollContainerHandle(handle) {
|
|
205
|
+
scrollContainerHandle = handle;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
(0, _react.renderHook)(function () {
|
|
209
|
+
return (0, _useScroll.useScrollBase)(props);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Set lastInputType to pageKey via the exposed themeScrollContainerHandle
|
|
213
|
+
(0, _react.act)(function () {
|
|
214
|
+
if (scrollContainerHandle) {
|
|
215
|
+
scrollContainerHandle.lastInputType = 'pageKey';
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// eslint-disable-next-line testing-library/no-unnecessary-act
|
|
220
|
+
(0, _react.act)(function () {
|
|
221
|
+
_react.fireEvent.keyDown(mocks.scrollContainerRef.current, {
|
|
222
|
+
keyCode: 33,
|
|
223
|
+
repeat: true
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
expect(onKeyDown).not.toHaveBeenCalled();
|
|
227
|
+
});
|
|
228
|
+
test('should forward onKeyDown event when repeat is true but lastInputType is neither arrowKey nor pageKey', function () {
|
|
229
|
+
var mocks = createMockRefs();
|
|
230
|
+
var onKeyDown = jest.fn();
|
|
231
|
+
var scrollContainerHandle = null;
|
|
232
|
+
var props = createNativeScrollProps(mocks, {
|
|
233
|
+
onKeyDown: onKeyDown,
|
|
234
|
+
setScrollContainerHandle: function setScrollContainerHandle(handle) {
|
|
235
|
+
scrollContainerHandle = handle;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
(0, _react.renderHook)(function () {
|
|
239
|
+
return (0, _useScroll.useScrollBase)(props);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// Set lastInputType to wheel (not arrowKey or pageKey)
|
|
243
|
+
(0, _react.act)(function () {
|
|
244
|
+
if (scrollContainerHandle) {
|
|
245
|
+
scrollContainerHandle.lastInputType = 'wheel';
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// eslint-disable-next-line testing-library/no-unnecessary-act
|
|
250
|
+
(0, _react.act)(function () {
|
|
251
|
+
_react.fireEvent.keyDown(mocks.scrollContainerRef.current, {
|
|
252
|
+
keyCode: 40,
|
|
253
|
+
repeat: true
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
expect(onKeyDown).toHaveBeenCalledTimes(1);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
79
259
|
});
|
package/useScroll/useScroll.js
CHANGED
|
@@ -850,7 +850,9 @@ var useScrollBase = exports.useScrollBase = function useScrollBase(props) {
|
|
|
850
850
|
var _props$preventScroll;
|
|
851
851
|
(_props$preventScroll = props.preventScroll) === null || _props$preventScroll === void 0 || _props$preventScroll.call(props, ev);
|
|
852
852
|
mutableRef.current.repeat = ev.repeat;
|
|
853
|
-
|
|
853
|
+
var isArrowKey = mutableRef.current.lastInputType === 'arrowKey';
|
|
854
|
+
var isPageKey = mutableRef.current.lastInputType === 'pageKey';
|
|
855
|
+
if (ev.repeat && (isArrowKey || isPageKey)) return;
|
|
854
856
|
(0, _handle.forward)('onKeyDown', ev, props);
|
|
855
857
|
}
|
|
856
858
|
}
|