Skip to content

Commit

Permalink
chore: internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 466147485
  • Loading branch information
material-web-copybara authored and copybara-github committed Aug 8, 2022
1 parent fe86936 commit 647a767
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions segmented_button_set/lib/segmented-button-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down

0 comments on commit 647a767

Please sign in to comment.