1
1
import { KeyActions } from '@stardust-ui/accessibility'
2
- import * as _ from 'lodash'
2
+ // @ts -ignore
3
3
import * as keyboardKey from 'keyboard-key'
4
4
import * as React from 'react'
5
5
6
6
import shouldHandleOnKeys from './shouldHandleOnKeys'
7
- import { AccessibilityActionHandlers , AccessibilityKeyHandlers } from './accessibility/reactTypes '
7
+ import { AccessibilityActionHandlers , AccessibilityKeyHandlers } from './types '
8
8
9
9
const rtlKeyMap = {
10
10
[ keyboardKey . ArrowRight ] : keyboardKey . ArrowLeft ,
@@ -14,31 +14,33 @@ const rtlKeyMap = {
14
14
/**
15
15
* Assigns onKeyDown handler to the slot element, based on Component's actions
16
16
* and keys mappings defined in Accessibility behavior
17
- * @param {AccessibilityActionHandlers } componentActionHandlers Actions handlers defined in a component.
18
- * @param {KeyActions } behaviorKeyActions Mappings of actions and keys defined in Accessibility behavior.
17
+ * @param {AccessibilityActionHandlers } actionHandlers Actions handlers defined in a component.
18
+ * @param {KeyActions } behaviorActions Mappings of actions and keys defined in Accessibility behavior.
19
19
* @param {boolean } isRtlEnabled Indicates if Left and Right arrow keys should be swapped in RTL mode.
20
20
*/
21
21
const getKeyDownHandlers = (
22
- componentActionHandlers : AccessibilityActionHandlers ,
23
- behaviorKeyActions : KeyActions ,
22
+ actionHandlers : AccessibilityActionHandlers ,
23
+ behaviorActions : KeyActions ,
24
24
isRtlEnabled ?: boolean ,
25
25
) : AccessibilityKeyHandlers => {
26
+ const componentHandlerNames = Object . keys ( actionHandlers )
26
27
const keyHandlers = { }
27
28
28
- if ( ! componentActionHandlers || ! behaviorKeyActions ) return keyHandlers
29
+ if ( ! actionHandlers || ! behaviorActions ) return keyHandlers
29
30
30
- for ( const componentPart in behaviorKeyActions ) {
31
- const componentPartKeyAction = behaviorKeyActions [ componentPart ]
32
- const handledActions = _ . intersection (
33
- _ . keys ( componentPartKeyAction ) ,
34
- _ . keys ( componentActionHandlers ) ,
31
+ for ( const slotName in behaviorActions ) {
32
+ const behaviorSlotAction = behaviorActions [ slotName ]
33
+ const handledActions = Object . keys ( behaviorSlotAction ) . filter (
34
+ actionName => componentHandlerNames . indexOf ( actionName ) !== - 1 ,
35
35
)
36
+
36
37
if ( ! handledActions . length ) continue
37
38
38
- keyHandlers [ componentPart ] = {
39
+ // @ts -ignore FIX ME
40
+ keyHandlers [ slotName ] = {
39
41
onKeyDown : ( event : React . KeyboardEvent ) => {
40
42
handledActions . forEach ( actionName => {
41
- let keyCombinations = componentPartKeyAction [ actionName ] . keyCombinations
43
+ let keyCombinations = behaviorSlotAction [ actionName ] . keyCombinations
42
44
43
45
if ( isRtlEnabled ) {
44
46
keyCombinations = keyCombinations . map ( keyCombination => {
@@ -51,7 +53,7 @@ const getKeyDownHandlers = (
51
53
}
52
54
53
55
if ( shouldHandleOnKeys ( event , keyCombinations ) ) {
54
- componentActionHandlers [ actionName ] ( event )
56
+ actionHandlers [ actionName ] ( event )
55
57
}
56
58
} )
57
59
} ,
0 commit comments