diff options
author | Knoobie <Knoobie@gmx.de> | 2018-12-18 14:29:59 +0100 |
---|---|---|
committer | Sun Zhe <31067185+ZheSun88@users.noreply.github.com> | 2018-12-18 15:29:59 +0200 |
commit | 353ba29cfdefddb032122cbeae5f02f6d9de76ba (patch) | |
tree | 05ee1da595faaab05976e9ab339472799648135b /client | |
parent | 9c9c962549eb04adac87b0cfe26d24b17843fb6a (diff) | |
download | vaadin-framework-353ba29cfdefddb032122cbeae5f02f6d9de76ba.tar.gz vaadin-framework-353ba29cfdefddb032122cbeae5f02f6d9de76ba.zip |
Checkbox allow customizing of input and label classNames. (#11372)
* add client side integration for custom styles for checkbox.label and checkbox.input
* add server side integration for checkbox element styling
* add server side tests
* add client side test
Diffstat (limited to 'client')
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/VCheckBox.java | 19 | ||||
-rw-r--r-- | client/src/main/java/com/vaadin/client/ui/checkbox/CheckBoxConnector.java | 42 |
2 files changed, 58 insertions, 3 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VCheckBox.java b/client/src/main/java/com/vaadin/client/ui/VCheckBox.java index 2c2df0a27e..f535086d94 100644 --- a/client/src/main/java/com/vaadin/client/ui/VCheckBox.java +++ b/client/src/main/java/com/vaadin/client/ui/VCheckBox.java @@ -84,19 +84,32 @@ public class VCheckBox extends com.google.gwt.user.client.ui.CheckBox * * @return Element of the CheckBox itself */ - private Element getCheckBoxElement() { + public Element getInputElement() { + // public to allow CheckBoxState to access it. // FIXME: Would love to use a better way to access the checkbox element return getElement().getFirstChildElement(); } + /** + * Gives access to the label element. + * + * @return Element of the Label itself + * @since + */ + public Element getLabelElement() { + // public to allow CheckBoxState to access it. + // FIXME: Would love to use a better way to access the label element + return getInputElement().getNextSiblingElement(); + } + @Override public void setAriaRequired(boolean required) { - AriaHelper.handleInputRequired(getCheckBoxElement(), required); + AriaHelper.handleInputRequired(getInputElement(), required); } @Override public void setAriaInvalid(boolean invalid) { - AriaHelper.handleInputInvalid(getCheckBoxElement(), invalid); + AriaHelper.handleInputInvalid(getInputElement(), invalid); } @Override diff --git a/client/src/main/java/com/vaadin/client/ui/checkbox/CheckBoxConnector.java b/client/src/main/java/com/vaadin/client/ui/checkbox/CheckBoxConnector.java index e9582d1755..df9b9b11cf 100644 --- a/client/src/main/java/com/vaadin/client/ui/checkbox/CheckBoxConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/checkbox/CheckBoxConnector.java @@ -15,6 +15,8 @@ */ package com.vaadin.client.ui.checkbox; +import com.google.gwt.core.client.JsArrayString; +import com.google.gwt.dom.client.Element; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.DOM; @@ -30,11 +32,14 @@ import com.vaadin.client.ui.Icon; import com.vaadin.client.ui.VCheckBox; import com.vaadin.shared.EventId; import com.vaadin.shared.MouseEventDetails; +import com.vaadin.shared.ui.ComponentStateUtil; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc; import com.vaadin.shared.ui.checkbox.CheckBoxState; import com.vaadin.ui.CheckBox; +import java.util.List; + /** * The client-side connector for the {@code CheckBoxGroup} component. * @@ -45,6 +50,22 @@ import com.vaadin.ui.CheckBox; public class CheckBoxConnector extends AbstractFieldConnector implements ClickHandler { + /** + * The style names from getState().inputStyles which are currently applied + * to the checkbox. + * + * @since + */ + private JsArrayString inputStyleNames = JsArrayString.createArray().cast(); + + /** + * The style names from getState().labelStyles which are currently applied + * to the checkbox. + * + * @since + */ + private JsArrayString labelStyleNames = JsArrayString.createArray().cast(); + @Override public boolean delegateCaptionHandling() { return false; @@ -88,6 +109,10 @@ public class CheckBoxConnector extends AbstractFieldConnector VCaption.setCaptionText(getWidget(), getState()); getWidget().setValue(getState().checked); + + // Set styles for input and label + updateStyles(getWidget().getInputElement(), inputStyleNames, getState().inputStyles); + updateStyles(getWidget().getLabelElement(), labelStyleNames, getState().labelStyles); } @Override @@ -134,4 +159,21 @@ public class CheckBoxConnector extends AbstractFieldConnector contextEventSunk = true; } } + + private void updateStyles(Element clientElement, JsArrayString clientSideStyles, List<String> serverSideStyes) { + // Remove all old stylenames + for (int i = 0; i < clientSideStyles.length(); i++) { + clientElement.removeClassName(clientSideStyles.get(i)); + } + clientSideStyles.setLength(0); + + if (ComponentStateUtil.hasStyles(serverSideStyes)) { + // add new style names + for (String newStyle : serverSideStyes) { + clientElement.addClassName(newStyle); + clientSideStyles.push(newStyle); + } + + } + } } |