license import { MDCFoundation } from '@material/base/foundation' ; import { SpecificEventListener } from '@material/base/types' ; import { MDCTextFieldIconAdapter } from './adapter' ; import { cssClasses , strings } from './constants' ; type InteractionEventType = 'click' | 'keydown' ; const INTERACTION_EVENTS = [ 'click' , 'keydown' ] ; export class MDCTextFieldIconFoundation extends MDCFoundation < MDCTextFieldIconAdapter > { static override get strings ( ) { return strings ; } static override get cssClasses ( ) { return cssClasses ; } /**
     * See  MDCTextFieldIconAdapter for typing information on parameters and return types.
      static override get defaultAdapter ( ) : MDCTextFieldIconAdapter { return { getAttr : ( ) => null , setAttr : ( ) => undefined , removeAttr : ( ) => undefined , setContent : ( ) => undefined , registerInteractionHandler : ( ) => undefined , deregisterInteractionHandler : ( ) => undefined , notifyIconAction : ( ) => undefined , } ; } private savedTabIndex : string | null = null ; private readonly interactionHandler : SpecificEventListener < InteractionEventType > ; constructor ( adapter ? ) { super ( { ... MDCTextFieldIconFoundation . defaultAdapter , ... adapter } ) ; this . interactionHandler = ( evt ) => { this . handleInteraction ( evt ) ; } ; } override init ( ) { this . savedTabIndex = this . adapter . getAttr ( 'tabindex' ) ; for ( const evtType of INTERACTION_EVENTS ) { this . adapter . registerInteractionHandler ( evtType , this . interactionHandler ) ; } } override destroy ( ) { for ( const evtType of INTERACTION_EVENTS ) { this . adapter . deregisterInteractionHandler ( evtType , this . interactionHandler ) ; } } setDisabled ( disabled ) { if ( ! this . savedTabIndex ) { return ; } if ( disabled ) { this . adapter . setAttr ( 'tabindex' , '-1' ) ; this . adapter . removeAttr ( 'role' ) ; } else { this . adapter . setAttr ( 'tabindex' , this . savedTabIndex ) ; this . adapter . setAttr ( 'role' , strings . ICON_ROLE ) ; } } setAriaLabel ( label ) { this . adapter . setAttr ( 'aria-label' , label ) ; } setContent ( content ) { this . adapter . setContent ( content ) ; } handleInteraction ( evt ) { const isEnterKey = ( evt as KeyboardEvent ) . key === 'Enter' || ( evt as KeyboardEvent ) . keyCode === 13 ; if ( evt . type === 'click' || isEnterKey ) { evt . preventDefault ( ) ; this . adapter . notifyIconAction ( ) ; } } } export default MDCTextFieldIconFoundation ;