license import { MDCFoundation } from '@material/base/foundation' ; import { isNavigationEvent , KEY , normalizeKey } from '@material/dom/keyboard' ; import { MDCChipTrailingActionAdapter } from './adapter' ; import { InteractionTrigger , strings } from './constants' ; export class MDCChipTrailingActionFoundation extends MDCFoundation < MDCChipTrailingActionAdapter > { static override get strings ( ) { return strings ; } static override get defaultAdapter ( ) : MDCChipTrailingActionAdapter { return { focus : ( ) => undefined , getAttribute : ( ) => null , setAttribute : ( ) => undefined , notifyInteraction : ( ) => undefined , notifyNavigation : ( ) => undefined , } ; } constructor ( adapter ? ) { super ( { ... MDCChipTrailingActionFoundation . defaultAdapter , ... adapter } ) ; } handleClick ( evt ) { evt . stopPropagation ( ) ; this . adapter . notifyInteraction ( InteractionTrigger . CLICK ) ; } handleKeydown ( evt ) { evt . stopPropagation ( ) ; const key = normalizeKey ( evt ) ; if ( this . shouldNotifyInteractionFromKey ( key ) ) { const trigger = this . getTriggerFromKey ( key ) ; this . adapter . notifyInteraction ( trigger ) ; return ; } if ( isNavigationEvent ( evt ) ) { this . adapter . notifyNavigation ( key ) ; return ; } } removeFocus ( ) { this . adapter . setAttribute ( strings . TAB_INDEX , '-1' ) ; } focus ( ) { this . adapter . setAttribute ( strings . TAB_INDEX , '0' ) ; this . adapter . focus ( ) ; } isNavigable ( ) { return this . adapter . getAttribute ( strings . ARIA_HIDDEN ) !== 'true' ; } private shouldNotifyInteractionFromKey ( key ) { const isFromActionKey = key === KEY . ENTER || key === KEY . SPACEBAR ; const isFromDeleteKey = key === KEY . BACKSPACE || key === KEY . DELETE ; return isFromActionKey || isFromDeleteKey ; } private getTriggerFromKey ( key ) { if ( key === KEY . SPACEBAR ) { return InteractionTrigger . SPACEBAR_KEY ; } if ( key === KEY . ENTER ) { return InteractionTrigger . ENTER_KEY ; } if ( key === KEY . DELETE ) { return InteractionTrigger . DELETE_KEY ; } if ( key === KEY . BACKSPACE ) { return InteractionTrigger . BACKSPACE_KEY ; } return InteractionTrigger . UNSPECIFIED ; } } export default MDCChipTrailingActionFoundation ;