<commit_before>/**
 * @requirecss {segmented_button_set.lib.shared_styles}
 *
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {ariaProperty} from '@material/web/decorators/aria-property';
import {html, LitElement, TemplateResult} from 'lit';
import {property, queryAssignedElements} from 'lit/decorators';
import {ClassInfo, classMap} from 'lit/directives/class-map';
import {ifDefined} from 'lit/directives/if-defined';

import {SegmentedButton} from '../../segmented_button/lib/segmented-button';

/**
 * SegmentedButtonSet is the parent component for two or more
 * `SegmentedButton` components. **Only** `SegmentedButton` components may be
 * used as children.
 * @soyCompatible
 */
export class SegmentedButtonSet extends LitElement {
  @property({type: Boolean}) multiselect = false;

  /** @soyPrefixAttribute */
  @ariaProperty
  @property({type: String, attribute: 'aria-label'})
  override ariaLabel!: string;

  @queryAssignedElements({flatten: true}) buttons!: SegmentedButton[];

  private handleSegmentedButtonInteraction(e: CustomEvent) {
    // TODO(b/229912696): Support interactions.
    const index = this.buttons.indexOf(e.target as SegmentedButton);
    this.toggleSelection(index);
  }

  private toggleSelection(index: number) {
    if (this.indexOutOfBounds(index)) return;
    if (this.multiselect) {
      this.buttons[index].selected = !this.buttons[index].selected;
      return;
    }
    // Single-select segmented buttons are not unselectable.
    this.buttons[index].selected = true;
    // Deselect all other buttons for single-select.
    for (let i = 0; i < this.buttons.length; i++) {
      if (i === index) continue;
      this.buttons[i].selected = false;
    }
  }

  private indexOutOfBounds(index: number): boolean {
    return index < 0 || index >= this.buttons.length;
  }

  /** @soyTemplate */
  override render(): TemplateResult {
    return html`
    <span
      role="group"
      @segmented-button-interaction="${this.handleSegmentedButtonInteraction}"
      aria-label="${ifDefined(this.ariaLabel)}"
      class="md3-segmented-button-set ${classMap(this.getRenderClasses())}">
      <slot></slot>
    </span>
    `;
  }

  /** @soyTemplate */
  protected getRenderClasses(): ClassInfo {
    return {
        // TODO(b/213634341): Write styles.
    };
  }
}
<commit_msg>chore: internal change<commit_after>/**
 * @requirecss {segmented_button_set.lib.shared_styles}
 *
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {ariaProperty} from '@material/web/decorators/aria-property';
import {html, LitElement, TemplateResult} from 'lit';
import {property, queryAssignedElements} from 'lit/decorators';
import {ClassInfo, classMap} from 'lit/directives/class-map';
import {ifDefined} from 'lit/directives/if-defined';

import {SegmentedButton} from '../../segmented_button/lib/segmented-button';

/**
 * SegmentedButtonSet is the parent component for two or more
 * `SegmentedButton` components. **Only** `SegmentedButton` components may be
 * used as children.
 * @soyCompatible
 */
export class SegmentedButtonSet extends LitElement {
  @property({type: Boolean}) multiselect = false;

  /** @soyPrefixAttribute */
  @ariaProperty
  @property({type: String, attribute: 'aria-label'})
  override ariaLabel!: string;

  @queryAssignedElements({flatten: true}) buttons!: SegmentedButton[];

  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;
    if (this.multiselect) {
      this.buttons[index].selected = !this.buttons[index].selected;
      this.emitSelectionEvent(index);
      return;
    }
    // Single-select segmented buttons are not unselectable.
    this.buttons[index].selected = true;
    this.emitSelectionEvent(index);
    // Deselect all other buttons for single-select.
    for (let i = 0; i < this.buttons.length; i++) {
      if (i === index) continue;
      this.buttons[i].selected = false;
    }
  }

  private indexOutOfBounds(index: number): boolean {
    return index < 0 || index >= this.buttons.length;
  }

  private emitSelectionEvent(index: number) {
    this.dispatchEvent(new CustomEvent('segmented-button-set-selection', {
      detail: {
        button: this.buttons[index],
        selected: this.buttons[index].selected,
        index,
      },
      bubbles: true,
      composed: true
    }));
  }

  /** @soyTemplate */
  override render(): TemplateResult {
    return html`
     <span
       role="group"
       @segmented-button-interaction="${this.handleSegmentedButtonInteraction}"
       aria-label="${ifDefined(this.ariaLabel)}"
       class="md3-segmented-button-set ${classMap(this.getRenderClasses())}">
       <slot></slot>
     </span>
     `;
  }

  /** @soyTemplate */
  protected getRenderClasses(): ClassInfo {
    return {};
  }
}

{'fraction_non_alphanumeric': 0.13639984070091596, 'fraction_numerical': 0.007168458781362007, 'mean_word_length': 3.7521286660359507, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6123502', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1499, 'n_words': 335}