license import { MDCFoundation } from '@material/base/foundation' ; import { MDCSwitchAdapter } from './adapter' ; import { cssClasses , strings } from './constants' ; export class MDCSwitchFoundation extends MDCFoundation < MDCSwitchAdapter > { /** The string constants used by the switch. */ static override get strings ( ) { return strings ; } /** The CSS classes used by the switch. */ static override get cssClasses ( ) { return cssClasses ; } /** The default Adapter for the switch. */ static override get defaultAdapter ( ) : MDCSwitchAdapter { return { addClass : ( ) => undefined , removeClass : ( ) => undefined , setNativeControlChecked : ( ) => undefined , setNativeControlDisabled : ( ) => undefined , setNativeControlAttr : ( ) => undefined , } ; } constructor ( adapter ? ) { super ( { ... MDCSwitchFoundation . defaultAdapter , ... adapter } ) ; } /** Sets the checked state of the switch. */ setChecked ( checked ) { this . adapter . setNativeControlChecked ( checked ) ; this . updateAriaChecked ( checked ) ; this . updateCheckedStyling ( checked ) ; } /** Sets the disabled state of the switch. */ setDisabled ( disabled ) { this . adapter . setNativeControlDisabled ( disabled ) ; if ( disabled ) { this . adapter . addClass ( cssClasses . DISABLED ) ; } else { this . adapter . removeClass ( cssClasses . DISABLED ) ; } } /** Handles the change event for the switch native control. */ handleChange ( evt ) { const nativeControl = evt . target as HTMLInputElement ; this . updateAriaChecked ( nativeControl . checked ) ; this . updateCheckedStyling ( nativeControl . checked ) ; } /** Updates the styling of the switch based on its checked state. */ private updateCheckedStyling ( checked ) { if ( checked ) { this . adapter . addClass ( cssClasses . CHECKED ) ; } else { this . adapter . removeClass ( cssClasses . CHECKED ) ; } } private updateAriaChecked ( checked ) { this . adapter . setNativeControlAttr ( strings . ARIA_CHECKED_ATTR , `${ ! ! checked }` ) ; } } export default MDCSwitchFoundation ;