import { KeyActions } from '@fluentui/accessibility' ; import * as keyboardKey from 'keyboard-key' ; import * as React from 'react' ; import shouldHandleOnKeys from './shouldHandleOnKeys' ; import { AccessibilityActionHandlers , AccessibilityKeyHandlers } from './types' ; const rtlKeyMap = { [ keyboardKey . ArrowRight ] : keyboardKey . ArrowLeft , [ keyboardKey . ArrowLeft ] : keyboardKey . ArrowRight , } ; param { AccessibilityActionHandlers } componentActionHandlers param { KeyActions } behaviorActions param { boolean } isRtlEnabled const getKeyDownHandlers = ( componentActionHandlers , behaviorActions , isRtlEnabled ? ) : AccessibilityKeyHandlers => { const slotKeyHandlers = { } ; if ( ! componentActionHandlers || ! behaviorActions ) { return slotKeyHandlers ; } const componentHandlerNames = Object . keys ( componentActionHandlers ) ; Object . keys ( behaviorActions ) . forEach ( slotName => { const behaviorSlotActions = behaviorActions [ slotName ] ; const handledActions = Object . keys ( behaviorSlotActions ) . filter ( actionName => { const slotAction = behaviorSlotActions [ actionName ] ; const actionHasKeyCombinations = Array . isArray ( slotAction . keyCombinations ) && slotAction . keyCombinations . length > 0 ; const actionHandledByComponent = componentHandlerNames . indexOf ( actionName ) !== - 1 ; return actionHasKeyCombinations && actionHandledByComponent ; } ) ; if ( handledActions . length > 0 ) { slotKeyHandlers [ slotName ] = { onKeyDown : ( event ) => { handledActions . forEach ( actionName => { let keyCombinations = behaviorSlotActions [ actionName ] . keyCombinations ; if ( keyCombinations ) { if ( isRtlEnabled ) { keyCombinations = keyCombinations . map ( keyCombination => { const keyToRtlKey = rtlKeyMap [ keyCombination . keyCode ] ; if ( keyToRtlKey ) { keyCombination . keyCode = keyToRtlKey ; } return keyCombination ; } ) ; } if ( shouldHandleOnKeys ( event , keyCombinations ) ) { componentActionHandlers [ actionName ] ( event ) ; } } } ) ; } , } ; } } ) ; return slotKeyHandlers ; } ; export default getKeyDownHandlers ;