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 com.google.gwt.user.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.gwt.elemento.core.IsElement;

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

import static java.util.Objects.nonNull;
import static org.jboss.gwt.elemento.core.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<>();
        container.appendChild(inputElement);
        container.appendChild(labelElement);
        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);
    }

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

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

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

    @Override
    public Radio uncheck(boolean silent) {
        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;
    }

    @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");
        return this;
    }

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

    public Radio setColor(Color color) {
        if (this.color != null)
            Style.of(inputElement).remove(this.color.getStyle());
        Style.of(inputElement).add(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;
        return this;
    }

    @Override
    public Radio disable() {
        inputElement.disabled = true;
        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