Search in sources :

Example 6 with CheckboxGroup

use of com.vaadin.flow.component.checkbox.CheckboxGroup in project flow-components by vaadin.

the class CheckboxGroupTest method selectDisabledItem_noRedundantEvent.

@Test
public void selectDisabledItem_noRedundantEvent() {
    CheckboxGroup<String> group = new CheckboxGroup<>();
    group.setItems("enabled", "disabled");
    group.setItemEnabledProvider("enabled"::equals);
    List<HasValue.ValueChangeEvent<Set<String>>> events = new ArrayList<>();
    group.addValueChangeListener(events::add);
    List<String> keys = group.getChildren().map(Component::getElement).map(element -> element.getProperty("value")).collect(Collectors.toList());
    String enabledKey = keys.get(0);
    String disabledKey = keys.get(1);
    JsonArray array = Json.createArray();
    array.set(0, disabledKey);
    group.getElement().setPropertyJson("value", array);
    Assert.assertThat(group.getValue(), IsEmptyCollection.empty());
    Assert.assertTrue(events.isEmpty());
    array = Json.createArray();
    array.set(0, enabledKey);
    group.getElement().setPropertyJson("value", array);
    Assert.assertEquals(Collections.singleton("enabled"), group.getValue());
    Assert.assertEquals(1, events.size());
    ValueChangeEvent<Set<String>> event = events.get(0);
    Assert.assertThat(event.getOldValue(), IsEmptyCollection.empty());
    Assert.assertEquals(Collections.singleton("enabled"), event.getValue());
}
Also used : ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) HasValue(com.vaadin.flow.component.HasValue) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) Json(elemental.json.Json) Div(com.vaadin.flow.component.html.Div) JsonArray(elemental.json.JsonArray) IsEmptyCollection(org.hamcrest.collection.IsEmptyCollection) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) CheckboxGroupListDataView(com.vaadin.flow.component.checkbox.dataview.CheckboxGroupListDataView) MultiSelectionEvent(com.vaadin.flow.data.selection.MultiSelectionEvent) DataProvider(com.vaadin.flow.data.provider.DataProvider) Element(com.vaadin.flow.dom.Element) UI(com.vaadin.flow.component.UI) ExpectedException(org.junit.rules.ExpectedException) VaadinSession(com.vaadin.flow.server.VaadinSession) Instantiator(com.vaadin.flow.di.Instantiator) Collection(java.util.Collection) ValueChangeEvent(com.vaadin.flow.component.HasValue.ValueChangeEvent) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Mockito(org.mockito.Mockito) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) VaadinService(com.vaadin.flow.server.VaadinService) CheckboxGroup(com.vaadin.flow.component.checkbox.CheckboxGroup) Assert(org.junit.Assert) Collections(java.util.Collections) Set(java.util.Set) CheckboxGroup(com.vaadin.flow.component.checkbox.CheckboxGroup) ArrayList(java.util.ArrayList) JsonArray(elemental.json.JsonArray) ValueChangeEvent(com.vaadin.flow.component.HasValue.ValueChangeEvent) Component(com.vaadin.flow.component.Component) Test(org.junit.Test)

Example 7 with CheckboxGroup

use of com.vaadin.flow.component.checkbox.CheckboxGroup in project flow-components by vaadin.

the class CheckboxGroupDemoPage method addComponentWithThemeVariant.

private void addComponentWithThemeVariant() {
    CheckboxGroup<String> group = new CheckboxGroup<>();
    group.setItems("foo", "bar", "baz");
    group.setId("checkbox-group-theme-variants");
    group.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
    Button removeVariantButton = new Button("Remove theme variant", e -> {
        group.removeThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
    });
    removeVariantButton.setId("remove-theme-variant-button");
    addCard("Button theme variants", group, removeVariantButton);
}
Also used : Button(com.vaadin.flow.component.button.Button) NativeButton(com.vaadin.flow.component.html.NativeButton) CheckboxGroup(com.vaadin.flow.component.checkbox.CheckboxGroup)

Example 8 with CheckboxGroup

use of com.vaadin.flow.component.checkbox.CheckboxGroup in project flow-components by vaadin.

the class CheckboxGroupDemoPage method addItemLabelGenerator.

private void addItemLabelGenerator() {
    Div message = new Div();
    CheckboxGroup<Person> group = new CheckboxGroup<>();
    group.setItems(new Person("Joe"), new Person("John"), new Person("Bill"));
    group.setItemLabelGenerator(Person::getName);
    group.addValueChangeListener(event -> message.setText(String.format("Checkbox group value changed from '%s' to '%s'", getNames(event.getOldValue()), getNames(event.getValue()))));
    group.setId("checkbox-group-with-item-generator");
    message.setId("checkbox-group-gen-value");
    addCard("Checkbox group with label generator", group, message);
}
Also used : Div(com.vaadin.flow.component.html.Div) CheckboxGroup(com.vaadin.flow.component.checkbox.CheckboxGroup)

Example 9 with CheckboxGroup

use of com.vaadin.flow.component.checkbox.CheckboxGroup in project flow-components by vaadin.

the class CheckboxGroupDemoPage method addComponentWithLabelAndErrorMessage.

private void addComponentWithLabelAndErrorMessage() {
    CheckboxGroup<String> group = new CheckboxGroup<>();
    group.setItems("foo", "bar", "baz");
    group.setLabel("Group label");
    group.setErrorMessage("Field has been set to invalid from server side");
    NativeButton button = new NativeButton("Switch validity state", event -> group.setInvalid(!group.isInvalid()));
    group.setId("group-with-label-and-error-message");
    button.setId("group-with-label-button");
    addCard("Group with label and error message", group, button);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) CheckboxGroup(com.vaadin.flow.component.checkbox.CheckboxGroup)

Example 10 with CheckboxGroup

use of com.vaadin.flow.component.checkbox.CheckboxGroup in project flow-components by vaadin.

the class CheckboxGroupDemoPage method addReadOnlyGroup.

private void addReadOnlyGroup() {
    Div valueInfo = new Div();
    CheckboxGroup<String> group = new CheckboxGroup<>();
    group.setItems("foo", "bar", "baz");
    group.setReadOnly(true);
    NativeButton button = new NativeButton("Switch read-only state", event -> group.setReadOnly(!group.isReadOnly()));
    group.addValueChangeListener(event -> valueInfo.setText(toString(group.getValue())));
    group.setId("checkbox-group-read-only");
    valueInfo.setId("selected-value-info");
    button.setId("switch-read-only");
    addCard("Read-only checkbox group", group, button, valueInfo);
}
Also used : Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) CheckboxGroup(com.vaadin.flow.component.checkbox.CheckboxGroup)

Aggregations

CheckboxGroup (com.vaadin.flow.component.checkbox.CheckboxGroup)10 Div (com.vaadin.flow.component.html.Div)6 NativeButton (com.vaadin.flow.component.html.NativeButton)4 UI (com.vaadin.flow.component.UI)3 Test (org.junit.Test)3 Instantiator (com.vaadin.flow.di.Instantiator)2 Element (com.vaadin.flow.dom.Element)2 VaadinService (com.vaadin.flow.server.VaadinService)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 JsonArray (elemental.json.JsonArray)2 Component (com.vaadin.flow.component.Component)1 HasValue (com.vaadin.flow.component.HasValue)1 ValueChangeEvent (com.vaadin.flow.component.HasValue.ValueChangeEvent)1 Button (com.vaadin.flow.component.button.Button)1 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)1 CheckboxGroupListDataView (com.vaadin.flow.component.checkbox.dataview.CheckboxGroupListDataView)1 Span (com.vaadin.flow.component.html.Span)1 DataProvider (com.vaadin.flow.data.provider.DataProvider)1 ListDataProvider (com.vaadin.flow.data.provider.ListDataProvider)1 MultiSelectionEvent (com.vaadin.flow.data.selection.MultiSelectionEvent)1