import { BaseComponent } from "./ave-base-component.js"; class AveSelect extends BaseComponent { static get observedAttributes() { return ['value', 'placeholder', 'disabled', 'name', 'simple', 'options']; } constructor() { super(); this._selectId = `select-${Math.random().toString(36).substr(2, 9)}`; this._trigger = null; this._popover = null; this._listbox = null; this._options = []; this._activeIndex = -1; this._handleTriggerClick = this._handleTriggerClick.bind(this); this._handleKeydown = this._handleKeydown.bind(this); this._handleOptionClick = this._handleOptionClick.bind(this); this._handleDocumentClick = this._handleDocumentClick.bind(this); this._handleNativeChange = this._handleNativeChange.bind(this); } // Property getters/setters get value() { return this._currentValue || this.getAttribute('value') || ''; } set value(value) { this._currentValue = value; this.setAttribute('value', value); this._updateSelectedOption(); } get placeholder() { return this.getAttribute('placeholder') || 'Select an option'; } set placeholder(value) { this.setAttribute('placeholder', value); } get disabled() { return this.hasAttribute('disabled'); } set disabled(value) { if (value === true || value === 'true' || value === '') { this.setAttribute('disabled', ''); } else { this.removeAttribute('disabled'); } } get name() { return this.getAttribute('name') || ''; } set name(value) { this.setAttribute('name', value); } get simple() { return this.hasAttribute('simple'); } set simple(value) { if (value === true || value === 'true' || value === '') { this.setAttribute('simple', ''); } else { this.removeAttribute('simple'); } } get options() { const optionsAttr = this.getAttribute('options'); if (optionsAttr && typeof optionsAttr === 'string') { try { return JSON.parse(optionsAttr); } catch (e) { console.warn('Invalid JSON in options attribute:', optionsAttr); return null; } } return null; } set options(value) { if (Array.isArray(value)) { this.setAttribute('options', JSON.stringify(value)); } else { this.setAttribute('options', value); } } setupEventListeners() { if (this.simple) { // Native select element const selectElement = this.shadowRoot.querySelector('select'); if (selectElement) { selectElement.addEventListener('change', this._handleNativeChange); } } else { // Custom select implementation this._trigger = this.shadowRoot.querySelector('.select-trigger'); this._popover = this.shadowRoot.querySelector('[popover]'); this._listbox = this.shadowRoot.querySelector('[role="listbox"]'); if (this._trigger && this._popover && this._listbox) { this._trigger.addEventListener('click', this._handleTriggerClick); this.addEventListener('keydown', this._handleKeydown); this._listbox.addEventListener('click', this._handleOptionClick); document.addEventListener('click', this._handleDocumentClick); this._updateOptions(); } } } _handleNativeChange(e) { this._currentValue = e.target.value; this._dispatch('ave-select-change', { value: this._currentValue, name: this.name }); } _handleTriggerClick() { if (this.disabled) return; const isOpen = this._popover.matches(':popover-open'); if (isOpen) { this._popover.hidePopover(); } else { this._popover.showPopover(); this._updateOptions(); if (this._options.length > 0) { this._setActiveOption(0); } } } _handleKeydown(e) { const isOpen = this._popover?.matches(':popover-open'); if (e.key === 'Escape') { if (isOpen) { this._popover.hidePopover(); this._trigger.focus(); } return; } if (!isOpen) { if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(e.key)) { e.preventDefault(); this._popover.showPopover(); this._updateOptions(); if (this._options.length > 0) { this._setActiveOption(0); } } return; } if (this._options.length === 0) return; let nextIndex = this._activeIndex; switch (e.key) { case 'ArrowDown': e.preventDefault(); nextIndex = this._activeIndex < this._options.length - 1 ? this._activeIndex + 1 : 0; break; case 'ArrowUp': e.preventDefault(); nextIndex = this._activeIndex > 0 ? this._activeIndex - 1 : this._options.length - 1; break; case 'Home': e.preventDefault(); nextIndex = 0; break; case 'End': e.preventDefault(); nextIndex = this._options.length - 1; break; case 'Enter': case ' ': e.preventDefault(); if (this._activeIndex > -1) { const activeOption = this._options[this._activeIndex]; this._selectOption(activeOption); } this._popover.hidePopover(); return; } if (nextIndex !== this._activeIndex) { this._setActiveOption(nextIndex); } } _handleOptionClick(e) { const option = e.target.closest('[role="option"]'); if (option) { this._selectOption(option); this._popover.hidePopover(); } } _handleDocumentClick(e) { if (!this.contains(e.target)) { this._popover?.hidePopover(); } } _updateOptions() { if (!this._listbox) return; this._options = Array.from(this._listbox.querySelectorAll('[role="option"]')); } _setActiveOption(index) { // Remove previous active state if (this._activeIndex > -1 && this._options[this._activeIndex]) { this._options[this._activeIndex].classList.remove('active'); } this._activeIndex = index; if (this._activeIndex > -1 && this._options[this._activeIndex]) { const activeOption = this._options[this._activeIndex]; activeOption.classList.add('active'); activeOption.scrollIntoView({ block: 'nearest', behavior: 'smooth' }); } } _selectOption(option) { const value = option.getAttribute('data-value'); this._currentValue = value; // Update visual state this._updateSelectedOption(); // Update hidden input if present const hiddenInput = this.shadowRoot.querySelector('input[type="hidden"]'); if (hiddenInput) { hiddenInput.value = value; } // Dispatch change event this._dispatch('ave-select-change', { value: value, text: option.textContent.trim(), name: this.name }); } _updateSelectedOption() { if (!this._listbox) return; // Update aria-selected attributes const options = this._listbox.querySelectorAll('[role="option"]'); options.forEach(option => { const isSelected = option.getAttribute('data-value') === this.value; option.setAttribute('aria-selected', isSelected.toString()); }); // Update trigger text if (this._trigger) { const triggerText = this._trigger.querySelector('span'); if (triggerText) { triggerText.textContent = this.value ? this._getSelectedText() : this.placeholder; } } } // Get current value getCurrentValue() { return this.value; } render() { if (this.simple) { // Simple native select this.shadowRoot.innerHTML = ` `; } else { // Complex popover-based select const slotContent = this.innerHTML; const triggerId = `${this._selectId}-trigger`; const listboxId = `${this._selectId}-listbox`; this.shadowRoot.innerHTML = `