license import { MDCFoundation } from '@material/base/foundation' ; import { SegmentDetail } from '../types' ; import { MDCSegmentedButtonAdapter } from './adapter' ; import { cssClasses } from './constants' ; export class MDCSegmentedButtonFoundation extends MDCFoundation < MDCSegmentedButtonAdapter > { static override get defaultAdapter ( ) : MDCSegmentedButtonAdapter { return { hasClass : ( ) => false , getSegments : ( ) => [ ] , selectSegment : ( ) => undefined , unselectSegment : ( ) => undefined , notifySelectedChange : ( ) => undefined } ; } constructor ( adapter ? ) { super ( { ... MDCSegmentedButtonFoundation . defaultAdapter , ... adapter } ) ; } param indexOrSegmentId selectSegment ( indexOrSegmentId ) { this . adapter . selectSegment ( indexOrSegmentId ) ; } param indexOrSegmentId unselectSegment ( indexOrSegmentId ) { this . adapter . unselectSegment ( indexOrSegmentId ) ; } return getSelectedSegments ( ) { return this . adapter . getSegments ( ) . filter ( segmentDetail => segmentDetail . selected ) ; } param indexOrSegmentId return isSegmentSelected ( indexOrSegmentId ) { return this . adapter . getSegments ( ) . some ( segmentDetail => ( segmentDetail . index === indexOrSegmentId || segmentDetail . segmentId === indexOrSegmentId ) && segmentDetail . selected ) ; } return isSingleSelect ( ) { return this . adapter . hasClass ( cssClasses . SINGLE_SELECT ) ; } param detail event handleSelected ( detail ) { if ( this . isSingleSelect ( ) ) { this . unselectPrevSelected ( detail . index ) ; } this . adapter . notifySelectedChange ( detail ) ; } param index private unselectPrevSelected ( index ) { for ( let selectedSegment of this . getSelectedSegments ( ) ) { if ( selectedSegment . index !== index ) { this . unselectSegment ( selectedSegment . index ) ; } } } }