From 647a767029eba16c539a9d17e9c801cb1d78a357 Mon Sep 17 00:00:00 2001 From: Material Web Team Date: Mon, 8 Aug 2022 14:22:44 -0700 Subject: [PATCH] chore: internal change PiperOrigin-RevId: 466147485 --- .../lib/segmented-button-set.ts | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/segmented_button_set/lib/segmented-button-set.ts b/segmented_button_set/lib/segmented-button-set.ts index 2c6e0e0fbe..c5c160f22e 100644 --- a/segmented_button_set/lib/segmented-button-set.ts +++ b/segmented_button_set/lib/segmented-button-set.ts @@ -30,19 +30,36 @@ export class SegmentedButtonSet extends LitElement { @queryAssignedElements({flatten: true}) buttons!: SegmentedButton[]; - private handleSegmentedButtonInteraction(e: CustomEvent) { - const index = this.buttons.indexOf(e.target as SegmentedButton); - this.toggleSelection(index); + getButtonDisabled(index: number): boolean { + if (this.indexOutOfBounds(index)) return false; + return this.buttons[index].disabled; } - private toggleSelection(index: number) { + setButtonDisabled(index: number, disabled: boolean) { if (this.indexOutOfBounds(index)) return; + this.buttons[index].disabled = disabled; + } + + getButtonSelected(index: number): boolean { + if (this.indexOutOfBounds(index)) return false; + return this.buttons[index].selected; + } + + setButtonSelected(index: number, selected: boolean) { + // Ignore out-of-index values. + if (this.indexOutOfBounds(index)) return; + // Ignore disabled buttons. + if (this.getButtonDisabled(index)) return; + if (this.multiselect) { - this.buttons[index].selected = !this.buttons[index].selected; + this.buttons[index].selected = selected; this.emitSelectionEvent(index); return; } + // Single-select segmented buttons are not unselectable. + if (!selected) return; + this.buttons[index].selected = true; this.emitSelectionEvent(index); // Deselect all other buttons for single-select. @@ -52,6 +69,16 @@ export class SegmentedButtonSet extends LitElement { } } + private handleSegmentedButtonInteraction(e: CustomEvent) { + const index = this.buttons.indexOf(e.target as SegmentedButton); + this.toggleSelection(index); + } + + private toggleSelection(index: number) { + if (this.indexOutOfBounds(index)) return; + this.setButtonSelected(index, !this.buttons[index].selected); + } + private indexOutOfBounds(index: number): boolean { return index < 0 || index >= this.buttons.length; }