Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.vaadin.flow.component.orderedlayout.FlexLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.select.Select;
import com.vaadin.flow.data.binder.HasItems;
import com.vaadin.flow.dom.Style;
import com.vaadin.flow.shared.Registration;

Expand All @@ -55,15 +56,16 @@
public class ChipComboBox<T> extends Composite<VerticalLayout> implements
HasValue<ComponentValueChangeEvent<ChipComboBox<T>, Collection<T>>, Collection<T>>,
HasStyle,
HasSize
HasSize,
HasItems<T>
{

/*
* UI-Components
*/
protected ComboBox<T> cbAvailableItems = new ComboBox<>();
protected FlexLayout chipsContainer = new FlexLayout();

/*
* Suppliers / Configuration
*/
Expand All @@ -88,7 +90,6 @@ protected void initUI()
chipsContainerStyle.set("flex-flow", "wrap");
chipsContainerStyle.set("flex-direction", "row");


this.getContent().setPadding(false);
this.getContent().setSpacing(false);
this.setSizeUndefined();
Expand Down Expand Up @@ -146,6 +147,7 @@ protected void addNewItem(final T newItem)

/**
* Updates/Rebuilds the UI form the fields
*
* @implNote Will not fire a {@link ValueChangeEvent}
*/
public void updateUI()
Expand All @@ -157,7 +159,7 @@ public void updateUI()
protected void updateSelectedChips()
{
this.chipsContainer.removeAll();
this.chipsContainer.add(this.selectedItems.values().toArray(new ChipComponent[] {}));
this.chipsContainer.add(this.selectedItems.values().toArray(new ChipComponent[]{}));
}

protected void updateAvailableItems()
Expand All @@ -168,7 +170,7 @@ protected void updateAvailableItems()
}

///////////////////////////////////////////////////////////////////////////
// setters + getters //
// setters + getters //
///////////////////////

public Supplier<ChipComponent> getChipsSupplier()
Expand Down Expand Up @@ -212,28 +214,44 @@ public ChipComboBox<T> withAllAvailableItems(final List<T> allAvailableItems, fi
return this;
}

@Override
public void setItems(final Collection<T> items)
{
this.withAllAvailableItems(new ArrayList<>(items), true);
}

public String getLabel()
{
return this.cbAvailableItems.getLabel();
}

public ChipComboBox<T> withLabel(final String label)
{
this.setLabel(label);
return this;
}

public void setLabel(final String label)
{
Objects.requireNonNull(label);
this.cbAvailableItems.setLabel(label);
return this;
}

public String getPlaceholder()
{
return this.cbAvailableItems.getPlaceholder();
}

public ChipComboBox<T> withPlaceholder(final String placeholder)
{
this.setPlaceholder(placeholder);
return this;
}

public void setPlaceholder(final String placeholder)
{
Objects.requireNonNull(placeholder);
this.cbAvailableItems.setPlaceholder(placeholder);
return this;
}

public ChipComboBox<T> withFullComboBoxWidth()
Expand All @@ -242,6 +260,12 @@ public ChipComboBox<T> withFullComboBoxWidth()
}

public ChipComboBox<T> withFullComboBoxWidth(final boolean useFullWidth)
{
this.setFullComboBoxWidth(useFullWidth);
return this;
}

public void setFullComboBoxWidth(final boolean useFullWidth)
{
if(useFullWidth)
{
Expand All @@ -251,9 +275,8 @@ public ChipComboBox<T> withFullComboBoxWidth(final boolean useFullWidth)
{
this.cbAvailableItems.setWidth(null);
}
return this;
}

@Override
public void setValue(final Collection<T> value)
{
Expand All @@ -266,7 +289,7 @@ public void setValue(final Collection<T> value)

this.updateUI();
}

@Override
public Collection<T> getValue()
{
Expand All @@ -275,62 +298,67 @@ public Collection<T> getValue()

protected void fireValueChange(final Collection<T> oldValue, final boolean fromClient)
{
ComponentUtil.fireEvent(this, new ComponentValueChangeEvent<>(this, this, oldValue, fromClient));
ComponentUtil.fireEvent(this, new ComponentValueChangeEvent<>(this, this, oldValue, fromClient));
}

@Override
@SuppressWarnings("unchecked")
public Registration addValueChangeListener(
final ValueChangeListener<? super ComponentValueChangeEvent<ChipComboBox<T>, Collection<T>>> listener)
{
@SuppressWarnings("rawtypes")
final
ComponentEventListener componentListener = event -> {
final ComponentValueChangeEvent<ChipComboBox<T>, Collection<T>> valueChangeEvent = (ComponentValueChangeEvent<ChipComboBox<T>, Collection<T>>) event;
listener.valueChanged(valueChangeEvent);
};
return ComponentUtil.addListener(this,
ComponentValueChangeEvent.class, componentListener);
final ComponentEventListener componentListener = event ->
{
final ComponentValueChangeEvent<ChipComboBox<T>, Collection<T>> valueChangeEvent =
(ComponentValueChangeEvent<ChipComboBox<T>, Collection<T>>)event;
listener.valueChanged(valueChangeEvent);
};
return ComponentUtil.addListener(
this,
ComponentValueChangeEvent.class,
componentListener);
}

@Override
public void setReadOnly(final boolean readOnly)
{
this.cbAvailableItems.setReadOnly(readOnly);
this.selectedItems.values().forEach(comp -> comp.setReadonly(readOnly));
}

@Override
public boolean isReadOnly()
{
return this.cbAvailableItems.isReadOnly();
}

@Override
public void setRequiredIndicatorVisible(final boolean requiredIndicatorVisible)
{
this.cbAvailableItems.setRequiredIndicatorVisible(requiredIndicatorVisible);
}

@Override
public boolean isRequiredIndicatorVisible()
{
return this.cbAvailableItems.isRequiredIndicatorVisible();
}

/**
* Returns the {@link ComboBox} which contains the available items.<br/>
* NOTE: If the contents of the {@link ComboBox} are modified from the outside this component may break
*
* @return
*/
public ComboBox<T> getCbAvailableItems()
{
return this.cbAvailableItems;
}

/**
* Returns the {@link FlexLayout} with the select items (as {@link ChipComponent}s).<br/>
* NOTE: If the contents of the {@link FlexLayout} are modified from the outside this component may break
*
* @return
*/
public FlexLayout getChipsContainer()
Expand Down