All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dominokit.domino.ui.forms.SelectOptionGroup Maven / Gradle / Ivy

package org.dominokit.domino.ui.forms;

import elemental2.dom.DomGlobal;
import elemental2.dom.HTMLElement;
import elemental2.dom.HTMLLIElement;
import elemental2.dom.Node;
import org.dominokit.domino.ui.utils.BaseDominoElement;
import org.dominokit.domino.ui.utils.DominoElement;
import org.jboss.gwt.elemento.core.IsElement;

import java.util.ArrayList;
import java.util.List;

import static org.jboss.gwt.elemento.core.Elements.li;

public class SelectOptionGroup extends BaseDominoElement> {

    private DominoElement element = DominoElement.of(li().css("dropdown-header"));
    private List> options = new ArrayList<>();
    private Node titleElement;

    public SelectOptionGroup(Node titleElement) {
        this.titleElement = titleElement;
        element.addEventListener("click", evt -> {
            evt.preventDefault();
            evt.stopPropagation();
        });
        element.appendChild(titleElement);
        init(this);
    }

    public static  SelectOptionGroup create(String title) {
        return create(DomGlobal.document.createTextNode(title));
    }

    public static  SelectOptionGroup create(Node titleElement) {
        return new SelectOptionGroup<>(titleElement);
    }

    public static  SelectOptionGroup create(HTMLElement titleElement) {
        return create((Node) titleElement);
    }

    public static  SelectOptionGroup create(IsElement titleElement) {
        return create(titleElement.asElement());
    }

    /**
     * @deprecated use {@link #appendChild(SelectOption)}
     */
    @Deprecated
    public SelectOptionGroup addOption(SelectOption option) {
        return appendChild(option);
    }

    public SelectOptionGroup appendChild(SelectOption option) {
        option.style().add("opt");
        options.add(option);
        return this;
    }

    public List> getOptions() {
        return options;
    }

    @Override
    public HTMLLIElement asElement() {
        return element.asElement();
    }

    boolean isAllHidden() {
        return options.stream().allMatch(SelectOption::isHidden);
    }

    void addOptionsTo(Select select) {
        for (SelectOption option : options) {
            option.addHideHandler(this::changeVisibility);
            option.addShowHandler(this::changeVisibility);
            select.appendChild(option);
        }
    }

    void changeVisibility() {
        if (isAllHidden()) {
            hide();
        } else {
            show();
        }
    }

    public Node getTitleElement() {
        return titleElement;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy