All files / packages/sds-components/src/sds/listboxOption listboxOption.js

0% Statements 0/11
0% Branches 0/4
0% Functions 0/6
0% Lines 0/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                                                                                                                     
import { LightningElement, api } from 'lwc';
import { reflectAttribute, normalizeInput } from 'sds/utils';
import 'sds/privateThemeProvider';
 
export default class ListboxOption extends LightningElement {
  static shadowSupportMode = 'native';
 
  _id;
  _optionsElement;
 
  /**
   * Sets the role attribute of the component.
   */
  connectedCallback() {
    this.setAttribute('role', 'option');
  }
 
  /**
   * Gets the ID of the Listbox Option.
   * @type {string}
   * @returns {string} The ID of the Listbox Option.
   */
  @api
  get id() {
    return this._id;
  }
  set id(value) {
    this._id = normalizeInput(value);
    reflectAttribute(this, 'id', this._id);
  }
 
  /**
   * Returns the option element.
   * @returns {Element} The option element.
   */
  get option() {
    this._optionsElement = this._optionsElement || this.template.querySelector('[part="listbox-option"]');
 
    return this._optionsElement;
  }
 
  /**
   * Handles the click event on the option.
   * @param {Event} e - The click event.
   */
  handleClick(e) {
    this.dispatchEvent(new CustomEvent('click'));
  }
 
  /**
   * Handles the blur event on the option.
   * @param {Event} e - The blur event.
   */
  handleBlur(e) {
    this.dispatchEvent(new CustomEvent('blur'));
  }
 
  /**
   * Validates the component and logs an error if the id is not set.
   */
  renderedCallback() {
    if (!this._id) {
      console.error('ListboxOption: id is required');
    }
  }
}