Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.0 kB
2
Indexable
selectDropdownOption(e, block) {
    e.stopPropagation();
    const selectedValue = e.target.getAttribute('value');
    const dropdownButton = block.querySelector('.dropdown-button');
    const target = block.querySelector(`.performance-data-${selectedValue}`);
    const items = Array.from(block.querySelectorAll('.performance-data__item'));
    const dropdownOptions = Array.from(block.querySelectorAll('.dropdown-option'));

    // Update the dropdown button text
    if (dropdownButton) {
        dropdownButton.innerText = e.target.innerText;
    }

    // Unselect all items
    items.forEach(item => item.setAttribute('aria-selected', false));
    dropdownOptions.forEach(option => option.classList.remove('selected'));

    // Select the target item
    if (target) {
        target.setAttribute('aria-selected', true);
    }

    // Mark the clicked option as selected
    e.target.classList.add('selected');

    // Close the dropdown
    this.closeDropdown(block);

    // Send GTM data
    assignEventAction(e);
}

updateTabsForDropdown(dropdownOptions, windowhashdata, block) {
    let defaultOption = null;

    if (this.selectedState) {
        const defaultOptions = dropdownOptions.filter(option => {
            const defaultInStates = option.dataset.defaultInStates?.split(' ');
            return defaultInStates && defaultInStates.includes(this.selectedState);
        });

        if (defaultOptions.length && !windowhashdata) {
            [defaultOption] = defaultOptions;

            const event = new Event('click', { bubbles: true });
            defaultOption.dispatchEvent(event);

            const dropdownButton = block.querySelector('.dropdown-button');
            if (dropdownButton) {
                dropdownButton.innerText = defaultOption.innerText;
            }

            // Ensure the selected option is highlighted
            dropdownOptions.forEach(option => option.classList.remove('selected'));
            defaultOption.classList.add('selected');
        }
    }
}
Leave a Comment