]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add css class to selected items in CheckboxGroup and RadiobuttonGroup (#10394)
authorMarco Collovati <mcollovati@gmail.com>
Thu, 28 Dec 2017 09:54:17 +0000 (10:54 +0100)
committerPekka Hyvönen <pekka@vaadin.com>
Thu, 28 Dec 2017 09:54:17 +0000 (11:54 +0200)
Adds v-select-option-selected class to the selected
group items in CheckboxGroup and RadiobuttonGroup
Fixes #3387

client/src/main/java/com/vaadin/client/ui/VCheckBoxGroup.java
client/src/main/java/com/vaadin/client/ui/VRadioButtonGroup.java
documentation/components/components-optiongroups.asciidoc
uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java
uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupTest.java

index b1132a487c484b41ffa549da225cd58a8908e3fb..93975d292630c70b19a6b5811d937650fa650f69 100644 (file)
@@ -50,6 +50,7 @@ public class VCheckBoxGroup extends FocusableFlowPanelComposite
 
     public static final String CLASSNAME = "v-select-optiongroup";
     public static final String CLASSNAME_OPTION = "v-select-option";
+    public static final String CLASSNAME_OPTION_SELECTED = "v-select-option-selected";
 
     private final Map<VCheckBox, JsonObject> optionsToItems;
 
@@ -135,6 +136,7 @@ public class VCheckBoxGroup extends FocusableFlowPanelComposite
         widget.setValue(
                 item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED));
         setOptionEnabled(widget, item);
+        widget.setStyleName(CLASSNAME_OPTION_SELECTED, widget.getValue());
 
         if (requireInitialization) {
             widget.addStyleName(CLASSNAME_OPTION);
index 89f8eb16e3c6a6d8410374478d3d4eed6be624e2..3a010c238037d280ac2a9051858bd5c103dc3f4e 100644 (file)
@@ -40,7 +40,6 @@ import com.vaadin.client.widgets.FocusableFlowPanelComposite;
 import com.vaadin.shared.Registration;
 import com.vaadin.shared.data.DataCommunicatorConstants;
 import com.vaadin.shared.ui.ListingJsonConstants;
-
 import elemental.json.JsonObject;
 
 /**
@@ -54,6 +53,7 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
 
     public static final String CLASSNAME = "v-select-optiongroup";
     public static final String CLASSNAME_OPTION = "v-select-option";
+    public static final String CLASSNAME_OPTION_SELECTED = "v-select-option-selected";
 
     private final Map<RadioButton, JsonObject> optionsToItems;
     private final Map<String, RadioButton> keyToOptions;
@@ -150,8 +150,6 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
         }
 
         button.setHTML(itemHtml);
-        button.setValue(
-                item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED));
         boolean optionEnabled = !item
                 .getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
         boolean enabled = optionEnabled && !isReadonly() && isEnabled();
@@ -159,6 +157,7 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
         // #9258 apply the v-disabled class when disabled for UX
         button.setStyleName(StyleConstants.DISABLED,
                 !isEnabled() || !optionEnabled);
+        updateItemSelection(button, item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED));
 
         String key = item.getString(DataCommunicatorConstants.KEY);
 
@@ -259,13 +258,19 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite
     }
 
     public void selectItemKey(String selectedItemKey) {
+        // At most one item could be selected so reset all radio buttons
+        // before applying current selection
+        keyToOptions.values().forEach(button -> updateItemSelection(button, false));
         if (selectedItemKey != null) {
             RadioButton radioButton = keyToOptions.get(selectedItemKey);
             if (radioButton != null) { // Items might not be loaded yet
-                radioButton.setValue(true);
+                updateItemSelection(radioButton, true);
             }
-        } else {
-            keyToOptions.values().forEach(button -> button.setValue(false));
         }
     }
+
+    protected void updateItemSelection(RadioButton radioButton, boolean value) {
+        radioButton.setValue(value);
+        radioButton.setStyleName(CLASSNAME_OPTION_SELECTED, value);
+    }
 }
index dbb8647234a76f2fdbd5d4c53fc1c56c95f6d1c5..bc2e261cf3b02815b7bd76e430f9dde92604d5a9 100644 (file)
@@ -87,7 +87,8 @@ Each check box will have the [literal]#++v-checkbox++# style, borrowed from the
 [classname]#CheckBox# component, and each radio button the
 [literal]#++v-radiobutton++# style. Both the radio buttons and check boxes will
 also have the [literal]#++v-select-option++# style that allows styling
-regardless of the option type. Disabled items have additionally the
+regardless of the option type and additionally [literal]#++v-select-option-selected++#
+when the item is selected. Disabled items have additionally the
 [literal]#++v-disabled++# style.
 
 
index 66589956b99820c9d8335bd455379b4f8a8fe7c0..98b09a6e6069b2fab38f2e72ef44a61162316052 100644 (file)
@@ -375,6 +375,18 @@ public class CheckBoxGroupTest extends MultiBrowserTest {
         verifyCheckboxDisabledClassNames(className, true);
     }
 
+    @Test // #3387
+    public void shouldApplySelectedClassToSelectedItems() {
+        openTestURL("theme=valo");
+        selectMenuPath("Component", "Selection", "Toggle Item 5");
+
+        String className = getSelect().getOptionElements().get(5).getAttribute("className");
+        assertTrue("No v-select-option-selected class, was " + className, className.contains("v-select-option-selected"));
+
+        selectMenuPath("Component", "Selection", "Toggle Item 5");
+        className = getSelect().getOptionElements().get(5).getAttribute("className");
+        assertFalse("Extra v-select-option-selected class, was " + className, className.contains("v-select-option-selected"));
+    }
     // needed to make tooltips work in IE tests
     @Override
     protected boolean requireWindowFocusForIE() {
index 96fc66fd1de415690b127b95329e9e63e81bcc05..a8fa91c51c67a4907782ba2b698ac136421c526c 100644 (file)
@@ -16,6 +16,7 @@
 package com.vaadin.tests.components.radiobuttongroup;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import java.util.List;
@@ -195,6 +196,32 @@ public class RadioButtonGroupTest extends MultiBrowserTest {
         verifyRadioButtonDisabledClassNames(className, true);
     }
 
+    @Test // #3387
+    public void shouldApplySelectedClassToSelectedItems() {
+        openTestURL("theme=valo");
+        selectMenuPath("Component", "Selection", "Toggle Item 5");
+
+        String className = getSelect().findElements(By.tagName("span")).get(5).getAttribute("className");
+        assertTrue("No v-select-option-selected class, was " + className, className.contains("v-select-option-selected")
+        );
+
+        getSelect().selectByText("Item 5");
+        className = getSelect().findElements(By.tagName("span")).get(5).getAttribute("className");
+        assertTrue("No v-select-option-selected class, was " + className, className.contains("v-select-option-selected"));
+
+        getSelect().selectByText("Item 10");
+        List<WebElement> options = getSelect().findElements(By.tagName("span"));
+        className = options.get(5).getAttribute("className");
+        assertFalse("Extra v-select-option-selected class, was " + className, className.contains("v-select-option-selected"));
+        className = options.get(10).getAttribute("className");
+        assertTrue("No v-select-option-selected class, was " + className, className.contains("v-select-option-selected"));
+
+        selectMenuPath("Component", "Selection", "Toggle Item 10");
+        className = getSelect().findElements(By.tagName("span")).get(10).getAttribute("className");
+        assertFalse("Extra v-select-option-selected class, was " + className, className.contains("v-select-option-selected"));
+    }
+
+
     @Test
     public void itemIconGenerator() {
         selectMenuPath("Component", "Item Icon Generator",