react-native-external-keyboard 0.6.8-rc → 0.6.8-rc1
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/ios/Delegates/RNCEKVFocusDelegate/RNCEKVFocusDelegate.mm +58 -21
- package/ios/Extensions/RCTEnhancedScrollView+RNCEKVExternalKeyboard.mm +12 -2
- package/lib/commonjs/utils/useKeyboardPress/useKeyboardPress.android.js +34 -21
- package/lib/commonjs/utils/useKeyboardPress/useKeyboardPress.android.js.map +1 -1
- package/lib/module/utils/useKeyboardPress/useKeyboardPress.android.js +34 -21
- package/lib/module/utils/useKeyboardPress/useKeyboardPress.android.js.map +1 -1
- package/lib/typescript/src/utils/useKeyboardPress/useKeyboardPress.android.d.ts +2 -2
- package/lib/typescript/src/utils/useKeyboardPress/useKeyboardPress.android.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/utils/useKeyboardPress/useKeyboardPress.android.ts +47 -23
|
@@ -13,46 +13,83 @@
|
|
|
13
13
|
#import "RNCEKVFocusEffectUtility.h"
|
|
14
14
|
|
|
15
15
|
@implementation RNCEKVFocusDelegate{
|
|
16
|
-
|
|
16
|
+
UIView<RNCEKVFocusProtocol>* _delegate;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
- (instancetype _Nonnull )initWithView:(UIView<RNCEKVFocusProtocol> *_Nonnull)delegate{
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
self = [super init];
|
|
21
|
+
if (self) {
|
|
22
|
+
_delegate = delegate;
|
|
23
|
+
}
|
|
24
|
+
return self;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
- (UIView*)getFirstFocusable:(UIView*)view {
|
|
28
|
+
if(view.subviews.count == 0) return nil;
|
|
29
|
+
UIView* child = view.subviews[0];
|
|
30
|
+
if(child.canBecomeFocused) {
|
|
31
|
+
return child;
|
|
32
|
+
} else {
|
|
33
|
+
return [self getFirstFocusable: child];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
- (UIView *)focusingViewForGestureHandler {
|
|
39
|
+
UIView *view = _delegate.subviews.firstObject;
|
|
40
|
+
// Handle the special case for RNGestureHandlerButtonComponentView
|
|
41
|
+
if (view && [NSStringFromClass([view class]) isEqualToString:@"RNGestureHandlerButtonComponentView"] &&
|
|
42
|
+
view.subviews.count > 0) {
|
|
43
|
+
return view.subviews.firstObject;
|
|
44
|
+
}
|
|
45
|
+
return nil;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
- (UIView *)firstObject {
|
|
49
|
+
UIView *firstChild = _delegate.subviews.firstObject;
|
|
50
|
+
if (firstChild && firstChild.canBecomeFocused) {
|
|
51
|
+
return firstChild;
|
|
23
52
|
}
|
|
24
|
-
return
|
|
53
|
+
return nil;
|
|
25
54
|
}
|
|
26
55
|
|
|
56
|
+
|
|
57
|
+
|
|
27
58
|
// ToDo RNCEKV-2, Double check condition, count more then 1 means that a view can be a ViewGroup, bun when we use hover component it can be considered as ViewGroup instead of touchable component, it can be improved by flag, or by removing hover component from js implementation
|
|
28
|
-
- (UIView*)getFocusingView {
|
|
29
|
-
if(_delegate.isGroup) {
|
|
59
|
+
- (UIView *)getFocusingView {
|
|
60
|
+
if (_delegate.isGroup) {
|
|
30
61
|
return _delegate;
|
|
31
62
|
}
|
|
32
63
|
|
|
33
|
-
|
|
34
|
-
|
|
64
|
+
UIView *gestureHandlerView = [self focusingViewForGestureHandler];
|
|
65
|
+
if (gestureHandlerView) {
|
|
66
|
+
return gestureHandlerView;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
UIView *firstObject = [self firstObject];
|
|
70
|
+
if (firstObject) {
|
|
71
|
+
return firstObject;
|
|
35
72
|
}
|
|
36
73
|
|
|
37
74
|
return _delegate;
|
|
38
75
|
}
|
|
39
76
|
|
|
40
77
|
- (BOOL)canBecomeFocused {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
78
|
+
if(!_delegate.canBeFocused) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
return [self getFocusingView] == _delegate;
|
|
45
82
|
}
|
|
46
83
|
|
|
47
84
|
- (NSNumber*)isFocusChanged:(UIFocusUpdateContext *)context {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
85
|
+
UIView* view = [self getFocusingView];
|
|
86
|
+
if(context.nextFocusedView == view) {
|
|
87
|
+
return @YES;
|
|
88
|
+
} else if (context.previouslyFocusedView == view) {
|
|
89
|
+
return @NO;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return nil;
|
|
56
93
|
}
|
|
57
94
|
|
|
58
95
|
|
|
@@ -10,13 +10,23 @@
|
|
|
10
10
|
|
|
11
11
|
#import "RCTEnhancedScrollView.h"
|
|
12
12
|
#import "RNCEKVSwizzleInstanceMethod.h"
|
|
13
|
+
#import "RCTScrollViewComponentView.h"
|
|
13
14
|
|
|
14
15
|
@implementation RCTEnhancedScrollView (RNCEKVExternalKeyboard)
|
|
15
16
|
|
|
16
17
|
- (NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
@try {
|
|
19
|
+
BOOL isScrollViewComponent = self.superview &&
|
|
20
|
+
[self.superview isKindOfClass:[RCTScrollViewComponentView class]];
|
|
21
|
+
|
|
22
|
+
if (isScrollViewComponent) {
|
|
23
|
+
RCTScrollViewComponentView *scrollViewComponent = (RCTScrollViewComponentView *)self.superview;
|
|
24
|
+
return @[scrollViewComponent.containerView];
|
|
25
|
+
}
|
|
19
26
|
}
|
|
27
|
+
@catch (NSException *exception) {
|
|
28
|
+
}
|
|
29
|
+
|
|
20
30
|
return [super preferredFocusEnvironments];
|
|
21
31
|
}
|
|
22
32
|
|
|
@@ -9,6 +9,15 @@ const ANDROID_SPACE_KEY_CODE = exports.ANDROID_SPACE_KEY_CODE = 62;
|
|
|
9
9
|
const ANDROID_DPAD_CENTER_CODE = exports.ANDROID_DPAD_CENTER_CODE = 23;
|
|
10
10
|
const ANDROID_ENTER_CODE = exports.ANDROID_ENTER_CODE = 66;
|
|
11
11
|
const ANDROID_TRIGGER_CODES = exports.ANDROID_TRIGGER_CODES = [ANDROID_SPACE_KEY_CODE, ANDROID_DPAD_CENTER_CODE, ANDROID_ENTER_CODE];
|
|
12
|
+
const useDebouncedCallback = (callback, delay) => {
|
|
13
|
+
const timeoutRef = (0, _react.useRef)();
|
|
14
|
+
return (0, _react.useCallback)((...args) => {
|
|
15
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
16
|
+
timeoutRef.current = setTimeout(() => {
|
|
17
|
+
callback(...args);
|
|
18
|
+
}, delay);
|
|
19
|
+
}, [callback, delay]);
|
|
20
|
+
};
|
|
12
21
|
const useKeyboardPress = ({
|
|
13
22
|
onKeyUpPress,
|
|
14
23
|
onKeyDownPress,
|
|
@@ -18,22 +27,15 @@ const useKeyboardPress = ({
|
|
|
18
27
|
onLongPress,
|
|
19
28
|
triggerCodes = ANDROID_TRIGGER_CODES
|
|
20
29
|
}) => {
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
30
|
+
const isLongPressRef = (0, _react.useRef)(false);
|
|
31
|
+
const debouncedOnPress = useDebouncedCallback(event => {
|
|
32
|
+
if (isLongPressRef.current) {
|
|
33
|
+
onLongPress === null || onLongPress === void 0 || onLongPress();
|
|
34
|
+
} else {
|
|
24
35
|
onPress === null || onPress === void 0 || onPress(event);
|
|
25
36
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (!onPressIn && !onLongPress) return onKeyDownPress;
|
|
29
|
-
return e => {
|
|
30
|
-
pressInRef.current = new Date().getTime();
|
|
31
|
-
onKeyDownPress === null || onKeyDownPress === void 0 || onKeyDownPress(e);
|
|
32
|
-
if (triggerCodes.includes(e.nativeEvent.keyCode)) {
|
|
33
|
-
onPressIn === null || onPressIn === void 0 || onPressIn(e);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
}, [onKeyDownPress, onLongPress, onPressIn, triggerCodes]);
|
|
37
|
+
isLongPressRef.current = false;
|
|
38
|
+
}, 40);
|
|
37
39
|
const onKeyUpPressHandler = (0, _react.useCallback)(e => {
|
|
38
40
|
const {
|
|
39
41
|
nativeEvent: {
|
|
@@ -45,17 +47,28 @@ const useKeyboardPress = ({
|
|
|
45
47
|
onKeyUpPress === null || onKeyUpPress === void 0 || onKeyUpPress(e);
|
|
46
48
|
if (triggerCodes.includes(keyCode)) {
|
|
47
49
|
if (isLongPress) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
onPress === null || onPress === void 0 || onPress();
|
|
50
|
+
isLongPressRef.current = true;
|
|
51
|
+
debouncedOnPress();
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
}, [onPressOut, onKeyUpPress, triggerCodes, debouncedOnPress]);
|
|
55
|
+
const onKeyDownPressHandler = (0, _react.useMemo)(() => {
|
|
56
|
+
if (!onPressIn) return onKeyDownPress;
|
|
57
|
+
return e => {
|
|
58
|
+
onKeyDownPress === null || onKeyDownPress === void 0 || onKeyDownPress(e);
|
|
59
|
+
if (triggerCodes.includes(e.nativeEvent.keyCode)) {
|
|
60
|
+
onPressIn === null || onPressIn === void 0 || onPressIn(e);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}, [onKeyDownPress, onPressIn, triggerCodes]);
|
|
64
|
+
const onPressHandler = (0, _react.useCallback)(event => {
|
|
65
|
+
debouncedOnPress(event);
|
|
66
|
+
}, [debouncedOnPress]);
|
|
67
|
+
const hasHandler = onPressOut || onKeyUpPress || onLongPress || onPress;
|
|
55
68
|
return {
|
|
56
|
-
|
|
69
|
+
onKeyUpPressHandler: hasHandler ? onKeyUpPressHandler : undefined,
|
|
57
70
|
onKeyDownPressHandler,
|
|
58
|
-
|
|
71
|
+
onPressHandler: onPress ? onPressHandler : undefined
|
|
59
72
|
};
|
|
60
73
|
};
|
|
61
74
|
exports.useKeyboardPress = useKeyboardPress;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","require","ANDROID_SPACE_KEY_CODE","exports","ANDROID_DPAD_CENTER_CODE","ANDROID_ENTER_CODE","ANDROID_TRIGGER_CODES","
|
|
1
|
+
{"version":3,"names":["_react","require","ANDROID_SPACE_KEY_CODE","exports","ANDROID_DPAD_CENTER_CODE","ANDROID_ENTER_CODE","ANDROID_TRIGGER_CODES","useDebouncedCallback","callback","delay","timeoutRef","useRef","useCallback","args","current","clearTimeout","setTimeout","useKeyboardPress","onKeyUpPress","onKeyDownPress","onPressIn","onPressOut","onPress","onLongPress","triggerCodes","isLongPressRef","debouncedOnPress","event","onKeyUpPressHandler","e","nativeEvent","keyCode","isLongPress","includes","onKeyDownPressHandler","useMemo","onPressHandler","hasHandler","undefined"],"sourceRoot":"../../../../src","sources":["utils/useKeyboardPress/useKeyboardPress.android.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAKO,MAAMC,sBAAsB,GAAAC,OAAA,CAAAD,sBAAA,GAAG,EAAE;AACjC,MAAME,wBAAwB,GAAAD,OAAA,CAAAC,wBAAA,GAAG,EAAE;AACnC,MAAMC,kBAAkB,GAAAF,OAAA,CAAAE,kBAAA,GAAG,EAAE;AAE7B,MAAMC,qBAAqB,GAAAH,OAAA,CAAAG,qBAAA,GAAG,CACnCJ,sBAAsB,EACtBE,wBAAwB,EACxBC,kBAAkB,CACnB;AAED,MAAME,oBAAoB,GAAGA,CAC3BC,QAAW,EACXC,KAAa,KACV;EACH,MAAMC,UAAU,GAAG,IAAAC,aAAM,EAAgC,CAAC;EAC1D,OAAO,IAAAC,kBAAW,EAChB,CAAC,GAAGC,IAAmB,KAAK;IAC1B,IAAIH,UAAU,CAACI,OAAO,EAAEC,YAAY,CAACL,UAAU,CAACI,OA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,IAAAC,cAAO,EAAC,MAAM;IAC1C,IAAI,CAACf,SAAS,EAAE,OAAOD,cAAc;IACrC,OAAQU,CAAa,IAAK;MACxBV,cAAc,aAAdA,cAAc,eAAdA,cAAc,CAAGU,CAAC,CAAC;MACnB,IAAIL,YAAY,CAACS,QAAQ,CAACJ,CAAC,CAACC,WAAW,CAACC,OAAO,CAAC,EAAE;QAChDX,SAAS,aAATA,SAAS,eAATA,SAAS,CAAGS,CAAqC,CAAC;MACpD;IACF,CAAC;EACH,CAAC,EAAE,CAACV,cAAc,EAAEC,SAAS,EAAEI,YAAY,CAAC,CAAC;EAE7C,MAAMY,cAAc,GAAG,IAAAxB,kBAAW,EAC/Be,KAA4B,IAAK;IAChCD,gBAAgB,CAACC,KAAK,CAAC;EACzB,CAAC,EACD,CAACD,gBAAgB,CACnB,CAAC;EAED,MAAMW,UAAU,GAAGhB,UAAU,IAAIH,YAAY,IAAIK,WAAW,IAAID,OAAO;EACvE,OAAO;IACLM,mBAAmB,EAAES,UAAU,GAAGT,mBAAmB,GAAGU,SAAS;IACjEJ,qBAAqB;IACrBE,cAAc,EAAEd,OAAO,GAAGc,cAAc,GAAGE;EAC7C,CAAC;AACH,CAAC;AAACnC,OAAA,CAAAc,gBAAA,GAAAA,gBAAA","ignoreList":[]}
|
|
@@ -3,6 +3,15 @@ export const ANDROID_SPACE_KEY_CODE = 62;
|
|
|
3
3
|
export const ANDROID_DPAD_CENTER_CODE = 23;
|
|
4
4
|
export const ANDROID_ENTER_CODE = 66;
|
|
5
5
|
export const ANDROID_TRIGGER_CODES = [ANDROID_SPACE_KEY_CODE, ANDROID_DPAD_CENTER_CODE, ANDROID_ENTER_CODE];
|
|
6
|
+
const useDebouncedCallback = (callback, delay) => {
|
|
7
|
+
const timeoutRef = useRef();
|
|
8
|
+
return useCallback((...args) => {
|
|
9
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
10
|
+
timeoutRef.current = setTimeout(() => {
|
|
11
|
+
callback(...args);
|
|
12
|
+
}, delay);
|
|
13
|
+
}, [callback, delay]);
|
|
14
|
+
};
|
|
6
15
|
export const useKeyboardPress = ({
|
|
7
16
|
onKeyUpPress,
|
|
8
17
|
onKeyDownPress,
|
|
@@ -12,22 +21,15 @@ export const useKeyboardPress = ({
|
|
|
12
21
|
onLongPress,
|
|
13
22
|
triggerCodes = ANDROID_TRIGGER_CODES
|
|
14
23
|
}) => {
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
if (
|
|
24
|
+
const isLongPressRef = useRef(false);
|
|
25
|
+
const debouncedOnPress = useDebouncedCallback(event => {
|
|
26
|
+
if (isLongPressRef.current) {
|
|
27
|
+
onLongPress === null || onLongPress === void 0 || onLongPress();
|
|
28
|
+
} else {
|
|
18
29
|
onPress === null || onPress === void 0 || onPress(event);
|
|
19
30
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (!onPressIn && !onLongPress) return onKeyDownPress;
|
|
23
|
-
return e => {
|
|
24
|
-
pressInRef.current = new Date().getTime();
|
|
25
|
-
onKeyDownPress === null || onKeyDownPress === void 0 || onKeyDownPress(e);
|
|
26
|
-
if (triggerCodes.includes(e.nativeEvent.keyCode)) {
|
|
27
|
-
onPressIn === null || onPressIn === void 0 || onPressIn(e);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}, [onKeyDownPress, onLongPress, onPressIn, triggerCodes]);
|
|
31
|
+
isLongPressRef.current = false;
|
|
32
|
+
}, 40);
|
|
31
33
|
const onKeyUpPressHandler = useCallback(e => {
|
|
32
34
|
const {
|
|
33
35
|
nativeEvent: {
|
|
@@ -39,17 +41,28 @@ export const useKeyboardPress = ({
|
|
|
39
41
|
onKeyUpPress === null || onKeyUpPress === void 0 || onKeyUpPress(e);
|
|
40
42
|
if (triggerCodes.includes(keyCode)) {
|
|
41
43
|
if (isLongPress) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
onPress === null || onPress === void 0 || onPress();
|
|
44
|
+
isLongPressRef.current = true;
|
|
45
|
+
debouncedOnPress();
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
}, [onPressOut, onKeyUpPress, triggerCodes, debouncedOnPress]);
|
|
49
|
+
const onKeyDownPressHandler = useMemo(() => {
|
|
50
|
+
if (!onPressIn) return onKeyDownPress;
|
|
51
|
+
return e => {
|
|
52
|
+
onKeyDownPress === null || onKeyDownPress === void 0 || onKeyDownPress(e);
|
|
53
|
+
if (triggerCodes.includes(e.nativeEvent.keyCode)) {
|
|
54
|
+
onPressIn === null || onPressIn === void 0 || onPressIn(e);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}, [onKeyDownPress, onPressIn, triggerCodes]);
|
|
58
|
+
const onPressHandler = useCallback(event => {
|
|
59
|
+
debouncedOnPress(event);
|
|
60
|
+
}, [debouncedOnPress]);
|
|
61
|
+
const hasHandler = onPressOut || onKeyUpPress || onLongPress || onPress;
|
|
49
62
|
return {
|
|
50
|
-
|
|
63
|
+
onKeyUpPressHandler: hasHandler ? onKeyUpPressHandler : undefined,
|
|
51
64
|
onKeyDownPressHandler,
|
|
52
|
-
|
|
65
|
+
onPressHandler: onPress ? onPressHandler : undefined
|
|
53
66
|
};
|
|
54
67
|
};
|
|
55
68
|
//# sourceMappingURL=useKeyboardPress.android.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useCallback","useMemo","useRef","ANDROID_SPACE_KEY_CODE","ANDROID_DPAD_CENTER_CODE","ANDROID_ENTER_CODE","ANDROID_TRIGGER_CODES","useKeyboardPress","onKeyUpPress","onKeyDownPress","onPressIn","onPressOut","onPress","onLongPress","triggerCodes","
|
|
1
|
+
{"version":3,"names":["useCallback","useMemo","useRef","ANDROID_SPACE_KEY_CODE","ANDROID_DPAD_CENTER_CODE","ANDROID_ENTER_CODE","ANDROID_TRIGGER_CODES","useDebouncedCallback","callback","delay","timeoutRef","args","current","clearTimeout","setTimeout","useKeyboardPress","onKeyUpPress","onKeyDownPress","onPressIn","onPressOut","onPress","onLongPress","triggerCodes","isLongPressRef","debouncedOnPress","event","onKeyUpPressHandler","e","nativeEvent","keyCode","isLongPress","includes","onKeyDownPressHandler","onPressHandler","hasHandler","undefined"],"sourceRoot":"../../../../src","sources":["utils/useKeyboardPress/useKeyboardPress.android.ts"],"mappings":"AAAA,SAASA,WAAW,EAAEC,OAAO,EAAEC,MAAM,QAAQ,OAAO;AAKpD,OAAO,MAAMC,sBAAsB,GAAG,EAAE;AACxC,OAAO,MAAMC,wBAAwB,GAAG,EAAE;AAC1C,OAAO,MAAMC,kBAAkB,GAAG,EAAE;AAEpC,OAAO,MAAMC,qBAAqB,GAAG,CACnCH,sBAAsB,EACtBC,wBAAwB,EACxBC,kBAAkB,CACnB;AAED,MAAME,oBAAoB,GAAGA,CAC3BC,QAAW,EACXC,KAAa,KACV;EACH,MAAMC,UAAU,GAAGR,MAAM,CAAgC,CAAC;EAC1D,OAAOF,WAAW,CAChB,CAAC,GAAGW,IAAmB,KAAK;IAC1B,IAAID,UAAU,CAACE,OAAO,EAAEC,YAAY,CAACH,UAAU,CAACE,OAAO,CAAC;IACxDF,UAAU,CAACE,OAAO,GAAGE,UAAU,CAAC,MAAM;MACpCN,QAAQ,CAAC,GAAGG,IAAI,CAAC;IACnB,CAAC,EAAEF,KAAK,CAAC;EACX,CAAC,EACD,CAACD,QAAQ,EAAEC,KAAK,CAClB,CAAC;AACH,CAAC;AAED,OAAO,MAAMM,gBAAgB,GAAGA,CAG9B;EACAC,YAAY;EACZC,cAAc;EACdC,SAAS;EACTC,UAAU;EACVC,OAAO;EACPC,WAAW;EACXC,YAAY,GAAGhB;AACY,CAAC,KAAK;EACjC,MAAMiB,cAAc,GAAGrB,MAAM,CAAC,KAAK,CAAC;EAEpC,MAAMsB,gBAAgB,GAAGjB,oBAAoB,CAC1CkB,KAA6B,IAAK;IACjC,IAAIF,cAAc,CAACX,OAAO,EAAE;MAC1BS,WAAW,aAAXA,WAAW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AAU,IAAIH,YAAY,IAAIK,WAAW,IAAID,OAAO;EACvE,OAAO;IACLM,mBAAmB,EAAEQ,UAAU,GAAGR,mBAAmB,GAAGS,SAAS;IACjEH,qBAAqB;IACrBC,cAAc,EAAEb,OAAO,GAAGa,cAAc,GAAGE;EAC7C,CAAC;AACH,CAAC","ignoreList":[]}
|
|
@@ -6,8 +6,8 @@ export declare const ANDROID_DPAD_CENTER_CODE = 23;
|
|
|
6
6
|
export declare const ANDROID_ENTER_CODE = 66;
|
|
7
7
|
export declare const ANDROID_TRIGGER_CODES: number[];
|
|
8
8
|
export declare const useKeyboardPress: <T extends (event?: any) => void, K extends (event?: any) => void>({ onKeyUpPress, onKeyDownPress, onPressIn, onPressOut, onPress, onLongPress, triggerCodes, }: UseKeyboardPressProps<T, K>) => {
|
|
9
|
-
|
|
9
|
+
onKeyUpPressHandler: OnKeyPressFn | undefined;
|
|
10
10
|
onKeyDownPressHandler: OnKeyPressFn | undefined;
|
|
11
|
-
|
|
11
|
+
onPressHandler: ((event: GestureResponderEvent) => void) | undefined;
|
|
12
12
|
};
|
|
13
13
|
//# sourceMappingURL=useKeyboardPress.android.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useKeyboardPress.android.d.ts","sourceRoot":"","sources":["../../../../../src/utils/useKeyboardPress/useKeyboardPress.android.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACtE,OAAO,KAAK,EAAc,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE7E,eAAO,MAAM,sBAAsB,KAAK,CAAC;AACzC,eAAO,MAAM,wBAAwB,KAAK,CAAC;AAC3C,eAAO,MAAM,kBAAkB,KAAK,CAAC;AAErC,eAAO,MAAM,qBAAqB,UAIjC,CAAC;
|
|
1
|
+
{"version":3,"file":"useKeyboardPress.android.d.ts","sourceRoot":"","sources":["../../../../../src/utils/useKeyboardPress/useKeyboardPress.android.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AACtE,OAAO,KAAK,EAAc,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE7E,eAAO,MAAM,sBAAsB,KAAK,CAAC;AACzC,eAAO,MAAM,wBAAwB,KAAK,CAAC;AAC3C,eAAO,MAAM,kBAAkB,KAAK,CAAC;AAErC,eAAO,MAAM,qBAAqB,UAIjC,CAAC;AAkBF,eAAO,MAAM,gBAAgB,GAC3B,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,GAAG,KAAK,IAAI,EAC/B,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,GAAG,KAAK,IAAI,EAC/B,8FAQC,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC;;;6BA6ClB,qBAAqB;CAYhC,CAAC"}
|
package/package.json
CHANGED
|
@@ -13,6 +13,22 @@ export const ANDROID_TRIGGER_CODES = [
|
|
|
13
13
|
ANDROID_ENTER_CODE,
|
|
14
14
|
];
|
|
15
15
|
|
|
16
|
+
const useDebouncedCallback = <T extends (...args: any[]) => void>(
|
|
17
|
+
callback: T,
|
|
18
|
+
delay: number
|
|
19
|
+
) => {
|
|
20
|
+
const timeoutRef = useRef<ReturnType<typeof setTimeout>>();
|
|
21
|
+
return useCallback(
|
|
22
|
+
(...args: Parameters<T>) => {
|
|
23
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
24
|
+
timeoutRef.current = setTimeout(() => {
|
|
25
|
+
callback(...args);
|
|
26
|
+
}, delay);
|
|
27
|
+
},
|
|
28
|
+
[callback, delay]
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
16
32
|
export const useKeyboardPress = <
|
|
17
33
|
T extends (event?: any) => void,
|
|
18
34
|
K extends (event?: any) => void,
|
|
@@ -25,28 +41,20 @@ export const useKeyboardPress = <
|
|
|
25
41
|
onLongPress,
|
|
26
42
|
triggerCodes = ANDROID_TRIGGER_CODES,
|
|
27
43
|
}: UseKeyboardPressProps<T, K>) => {
|
|
28
|
-
const
|
|
44
|
+
const isLongPressRef = useRef(false);
|
|
29
45
|
|
|
30
|
-
const
|
|
31
|
-
(event
|
|
32
|
-
if (
|
|
46
|
+
const debouncedOnPress = useDebouncedCallback(
|
|
47
|
+
(event?: GestureResponderEvent) => {
|
|
48
|
+
if (isLongPressRef.current) {
|
|
49
|
+
onLongPress?.();
|
|
50
|
+
} else {
|
|
33
51
|
onPress?.(event);
|
|
34
52
|
}
|
|
53
|
+
isLongPressRef.current = false;
|
|
35
54
|
},
|
|
36
|
-
|
|
55
|
+
40
|
|
37
56
|
);
|
|
38
57
|
|
|
39
|
-
const onKeyDownPressHandler = useMemo(() => {
|
|
40
|
-
if (!onPressIn && !onLongPress) return onKeyDownPress;
|
|
41
|
-
return (e: OnKeyPress) => {
|
|
42
|
-
pressInRef.current = new Date().getTime();
|
|
43
|
-
onKeyDownPress?.(e);
|
|
44
|
-
if (triggerCodes.includes(e.nativeEvent.keyCode)) {
|
|
45
|
-
onPressIn?.(e as unknown as GestureResponderEvent);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}, [onKeyDownPress, onLongPress, onPressIn, triggerCodes]);
|
|
49
|
-
|
|
50
58
|
const onKeyUpPressHandler = useCallback<OnKeyPressFn>(
|
|
51
59
|
(e) => {
|
|
52
60
|
const {
|
|
@@ -58,19 +66,35 @@ export const useKeyboardPress = <
|
|
|
58
66
|
|
|
59
67
|
if (triggerCodes.includes(keyCode)) {
|
|
60
68
|
if (isLongPress) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
onPress?.();
|
|
69
|
+
isLongPressRef.current = true;
|
|
70
|
+
debouncedOnPress();
|
|
64
71
|
}
|
|
65
72
|
}
|
|
66
|
-
pressInRef.current = undefined;
|
|
67
73
|
},
|
|
68
|
-
[onPressOut, onKeyUpPress, triggerCodes,
|
|
74
|
+
[onPressOut, onKeyUpPress, triggerCodes, debouncedOnPress]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const onKeyDownPressHandler = useMemo(() => {
|
|
78
|
+
if (!onPressIn) return onKeyDownPress;
|
|
79
|
+
return (e: OnKeyPress) => {
|
|
80
|
+
onKeyDownPress?.(e);
|
|
81
|
+
if (triggerCodes.includes(e.nativeEvent.keyCode)) {
|
|
82
|
+
onPressIn?.(e as unknown as GestureResponderEvent);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}, [onKeyDownPress, onPressIn, triggerCodes]);
|
|
86
|
+
|
|
87
|
+
const onPressHandler = useCallback(
|
|
88
|
+
(event: GestureResponderEvent) => {
|
|
89
|
+
debouncedOnPress(event);
|
|
90
|
+
},
|
|
91
|
+
[debouncedOnPress]
|
|
69
92
|
);
|
|
70
93
|
|
|
94
|
+
const hasHandler = onPressOut || onKeyUpPress || onLongPress || onPress;
|
|
71
95
|
return {
|
|
72
|
-
|
|
96
|
+
onKeyUpPressHandler: hasHandler ? onKeyUpPressHandler : undefined,
|
|
73
97
|
onKeyDownPressHandler,
|
|
74
|
-
|
|
98
|
+
onPressHandler: onPress ? onPressHandler : undefined,
|
|
75
99
|
};
|
|
76
100
|
};
|