org.dominokit.domino.ui.forms.Select Maven / Gradle / Ivy
package org.dominokit.domino.ui.forms;
import org.dominokit.domino.ui.dropdown.DropDownMenu;
import org.dominokit.domino.ui.utils.ElementUtil;
import java.util.List;
import java.util.Objects;
import static java.util.Objects.nonNull;
public class Select extends AbstractSelect> {
private SelectOption selectedOption;
public static Select create() {
return new Select<>();
}
public static Select create(String label) {
return new Select<>(label);
}
public static Select create(String label, List> options) {
return new Select<>(label, options);
}
public static Select create(List> options) {
return new Select<>(options);
}
public static > Select ofEnum(String label, T[] values) {
Select select = create(label);
for (T value : values) {
select.appendChild(SelectOption.create(value, value.name(), value.toString()));
}
return select;
}
public Select() {
}
public Select(String label) {
super(label);
}
public Select(List> options) {
super(options);
}
public Select(String label, List> options) {
super(label, options);
}
@Override
public DropDownMenu getOptionsMenu() {
return super.getOptionsMenu();
}
@Override
public Select select(SelectOption option, boolean silent) {
if (selectedOption != null)
if (!option.isEqualNode(selectedOption.element()))
selectedOption.deselect();
floatLabel();
this.selectedOption = option;
option.select();
buttonValueContainer.setTextContent(option.getDisplayValue());
if (!silent)
onSelection(option);
return this;
}
public SelectOption getSelectedOption() {
return selectedOption;
}
@Override
public Select setValue(T value, boolean silent) {
for (SelectOption option : getOptions()) {
if (Objects.equals(option.getValue(), value)) {
select(option, silent);
}
}
return this;
}
@Override
public T getValue() {
return isSelected() ? getSelectedOption().getValue() : null;
}
@Override
public String getStringValue() {
SelectOption selectedOption = getSelectedOption();
if (nonNull(selectedOption)) {
return selectedOption.getDisplayValue();
}
return null;
}
@Override
public Select clear() {
this.selectedOption = null;
return super.clear();
}
public int getSelectedIndex() {
return options.indexOf(getSelectedOption());
}
@Override
protected void scrollToSelectedOption() {
if(nonNull(selectedOption)){
ElementUtil.scrollIntoParent(selectedOption.element(), getOptionsMenu().getMenuElement().element());
}
}
}