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

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

There is a newer version: 1.0.139
Show newest version
package org.dominokit.domino.ui.forms;

import org.gwtproject.editor.client.TakesValue;
import elemental2.dom.*;
import org.dominokit.domino.ui.grid.flex.FlexItem;
import org.dominokit.domino.ui.style.Color;
import org.dominokit.domino.ui.style.Style;
import org.dominokit.domino.ui.utils.*;
import org.gwtproject.safehtml.shared.SafeHtml;
import org.jboss.elemento.IsElement;

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

import static java.util.Objects.nonNull;
import static org.jboss.elemento.Elements.*;

public class Radio extends BaseDominoElement> implements HasName>, HasValue, T>, HasLabel>,
        Switchable>, Checkable>, TakesValue {

    private FlexItem container = FlexItem.create().addCss("radio-option");
    private HTMLLabelElement labelElement = label().element();
    private HTMLInputElement inputElement = input("radio").element();
    private DominoElement helperTextElement = DominoElement.of(p());
    private List> changeHandlers;
    private Color color;
    private boolean checked = false;
    private RadioGroup radioGroup;
    private T value;

    public Radio(T value, String label) {
        changeHandlers = new ArrayList<>();
        linkLabelToField();
        container.appendChild(labelElement);
        container.appendChild(inputElement);
        setLabel(label);
        value(value);
        container.addEventListener("click", evt -> {
            if (isEnabled() && !isChecked())
                check();
        });
        inputElement.addEventListener("change", evt -> onCheck());
        init(this);
    }

    public Radio(T value) {
        this(value, String.valueOf(value));
    }

    public static  Radio create(E value, String label) {
        return new Radio<>(value, label);
    }

    public static  Radio create(E value) {
        return new Radio<>(value);
    }

    private void linkLabelToField() {
        DominoElement asDominoElement = DominoElement.of(inputElement);
        if (!asDominoElement.hasAttribute("id")) {
            inputElement.setAttribute("id", asDominoElement.getAttribute(BaseDominoElement.DOMINO_UUID));
        }
        labelElement.setAttribute("for", asDominoElement.getAttribute("id"));
    }

    @Override
    public Radio check() {
        return check(false);
    }

    public Radio uncheck() {
        return uncheck(false);
    }

    @Override
    public Radio check(boolean silent) {
        if (nonNull(radioGroup)) {
            if(!radioGroup.isReadOnly()) {
                radioGroup.getRadios().forEach(radio -> radio.setChecked(false));
                setChecked(true);
                if (!silent)
                    onCheck();
            }
        }else{
            setChecked(true);
            if (!silent)
                onCheck();
        }
        return this;
    }

    @Override
    public Radio uncheck(boolean silent) {
        if (nonNull(radioGroup)) {
            if (!radioGroup.isReadOnly()) {
                setChecked(false);
                if (!silent)
                    onCheck();
            }
        }else {
            setChecked(false);
            if (!silent)
                onCheck();
        }

        return this;
    }

    @Override
    public Radio addChangeHandler(ChangeHandler changeHandler) {
        changeHandlers.add(changeHandler);
        return this;
    }

    private void setChecked(boolean value) {
        inputElement.checked = value;
        this.checked = value;
        if (this.checked) {
            element.css("checked");
        } else {
            element.removeCss("checked");
        }
    }

    @Override
    public Radio removeChangeHandler(ChangeHandler changeHandler) {
        if (changeHandler != null)
            changeHandlers.remove(changeHandler);
        return this;
    }

    @Override
    public boolean hasChangeHandler(ChangeHandler changeHandler) {
        return changeHandlers.contains(changeHandler);
    }

    private void onCheck() {
        for (ChangeHandler checkHandler : changeHandlers)
            checkHandler.onValueChanged(isChecked());
    }

    @Override
    public boolean isChecked() {
        return this.checked;
    }

    public Radio withGap() {
        Style.of(inputElement).add("with-gap");
        element.css("with-gap");
        return this;
    }

    public Radio withoutGap() {
        Style.of(inputElement).remove("with-gap");
        element.removeCss("with-gap");
        return this;
    }

    public Radio setColor(Color color) {
        if (this.color != null) {
            element.removeCss(this.color.getStyle());
        }
        element.css(color.getStyle());
        this.color = color;
        return this;
    }

    @Override
    public HTMLDivElement element() {
        return container.element();
    }

    @Override
    public String getName() {
        return inputElement.name;
    }

    @Override
    public Radio setName(String name) {
        inputElement.name = name;
        return this;
    }

    @Override
    public Radio value(T value) {
        setValue(value);
        return this;
    }

    @Override
    public void setValue(T value) {
        this.value = value;
    }

    @Override
    public T getValue() {
        return this.value;
    }

    @Override
    public Radio setLabel(String label) {
        labelElement.textContent = label;
        return this;
    }

    public Radio setLabel(SafeHtml safeHtml) {
        labelElement.innerHTML = safeHtml.asString();
        return this;
    }

    public Radio setLabel(Node node) {
        DominoElement.of(labelElement)
                .clearElement()
                .appendChild(node);
        return this;
    }

    public Radio setLabel(IsElement element) {
        return setLabel(element.element());
    }

    @Override
    public String getLabel() {
        return labelElement.textContent;
    }

    @Override
    public Radio enable() {
        inputElement.disabled = false;
        element.removeCss("disabled");
        return this;
    }

    @Override
    public Radio disable() {
        inputElement.disabled = true;
        element.css("disabled");
        return this;
    }

    public Radio setHelperText(String text) {
        helperTextElement.setTextContent(text);
        if (!DominoElement.of(labelElement).contains(helperTextElement.element())) {
            labelElement.appendChild(helperTextElement.element());
        }
        return this;
    }

    @Override
    public boolean isEnabled() {
        return !inputElement.disabled;
    }

    void setGroup(RadioGroup radioGroup) {
        this.radioGroup = radioGroup;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy