Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Use theme API for add/remove "icon" theme variant (#95) (#96)
Browse files Browse the repository at this point in the history
Fixes #93
  • Loading branch information
Denis committed Dec 7, 2018
1 parent da5c4c3 commit 9147eed
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
14 changes: 6 additions & 8 deletions src/main/java/com/vaadin/flow/component/button/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public boolean isAutofocus() {
* Enabling the button needs to happen from the server.
*
* @param disableOnClick
* true to disable button immediately when clicked
* true to disable button immediately when clicked
*/
public void setDisableOnClick(boolean disableOnClick) {
this.disableOnClick = disableOnClick;
Expand Down Expand Up @@ -453,16 +453,14 @@ private Element[] getTextNodes() {
}

private void updateThemeAttribute() {
// Set attribute theme="icon" when the button contains only an icon to
// Add theme attribute "icon" when the button contains only an icon to
// fully support themes like Valo. This doesn't override explicitly set
// theme attribute.
String theme = getElement().getAttribute(THEME_ATTRIBUTE);

if (theme == null && getElement().getChildCount() == 1
&& iconComponent != null) {
getElement().setAttribute(THEME_ATTRIBUTE, "icon");
} else if ("icon".equals(theme)) {
getElement().removeAttribute(THEME_ATTRIBUTE);
if (getElement().getChildCount() == 1 && iconComponent != null) {
getThemeNames().add("icon");
} else {
getThemeNames().remove("icon");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2000-2018 Vaadin Ltd.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Expand All @@ -20,8 +20,8 @@
/**
* Set of theme variants applicable for {@code vaadin-button} component.
*/
@Generated({ "Generator: com.vaadin.generator.ComponentGenerator#1.1-SNAPSHOT",
"WebComponent: Vaadin.ButtonElement#2.1.0", "Flow#1.1-SNAPSHOT" })
@Generated({ "Generator: com.vaadin.generator.ComponentGenerator#1.2-SNAPSHOT",
"WebComponent: Vaadin.ButtonElement#2.1.0", "Flow#1.2-SNAPSHOT" })
public enum ButtonVariant {
LUMO_SMALL("small"), LUMO_LARGE("large"), LUMO_TERTIARY(
"tertiary"), LUMO_TERTIARY_INLINE("tertiary-inline"), LUMO_PRIMARY(
Expand All @@ -39,7 +39,7 @@ public enum ButtonVariant {

/**
* Gets the variant name.
*
*
* @return variant name
*/
public String getVariantName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package com.vaadin.flow.component.button.tests;

import java.util.Set;
import java.util.function.Predicate;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.dom.Element;
Expand Down Expand Up @@ -139,19 +141,19 @@ public void changeOrder_setIcon_setText_changeOrder() {
@Test
public void updatingThemeAttribute() {
button = new Button();
assertButtonHasThemeAttribute(null);
assertButtonHasNoThemeAttribute();

button.setIcon(new Icon());
assertButtonHasThemeAttribute(THEME_ATTRIBUTE_ICON);

button.setText("foo");
assertButtonHasThemeAttribute(null);
assertButtonHasNoThemeAttribute();

button.setIcon(null);
assertButtonHasThemeAttribute(null);
assertButtonHasNoThemeAttribute();

button = new Button("foo", new Icon());
assertButtonHasThemeAttribute(null);
assertButtonHasNoThemeAttribute();

button.setText("");
assertButtonHasThemeAttribute(THEME_ATTRIBUTE_ICON);
Expand Down Expand Up @@ -223,6 +225,42 @@ public void setText_slotAttributeIsPreserved() {
Assert.assertEquals("prefix", icon.getElement().getAttribute("slot"));
}

@Test
public void addThemeVariant_setIcon_themeAttributeContiansThemeVariantAndIcon() {
button = new Button();
button.addThemeVariants(ButtonVariant.LUMO_SUCCESS);
button.setIcon(new Icon(VaadinIcon.ARROW_RIGHT));

Set<String> themeNames = button.getThemeNames();
Assert.assertTrue(themeNames.contains("icon"));
Assert.assertTrue(themeNames
.contains(ButtonVariant.LUMO_SUCCESS.getVariantName()));
}

@Test
public void setIcon_addThemeVariant_themeAttributeContiansThemeVariantAndIcon() {
button = new Button();
button.setIcon(new Icon(VaadinIcon.ARROW_RIGHT));
button.addThemeVariants(ButtonVariant.LUMO_SUCCESS);

Set<String> themeNames = button.getThemeNames();
Assert.assertTrue(themeNames.contains("icon"));
Assert.assertTrue(themeNames
.contains(ButtonVariant.LUMO_SUCCESS.getVariantName()));
}

@Test
public void changeIcon_iconThemeIsPreserved() {
button = new Button();
button.setIcon(new Icon(VaadinIcon.ARROW_RIGHT));

Assert.assertEquals("icon", button.getThemeName());

button.setIcon(new Icon(VaadinIcon.ALARM));

Assert.assertEquals("icon", button.getThemeName());
}

private void assertOnlyChildIsText() {
Assert.assertEquals(1, button.getElement().getChildCount());
Element child = getButtonChild(0);
Expand All @@ -248,13 +286,12 @@ private void assertIconAndSpan(boolean iconAfterText) {
}

private void assertButtonHasThemeAttribute(String theme) {
if (theme == null) {
Assert.assertFalse(
button.getElement().hasAttribute(THEME_ATTRIBUTE));
} else {
Assert.assertEquals(theme,
button.getElement().getAttribute(THEME_ATTRIBUTE));
}
Assert.assertTrue("Expected " + theme + " to be in the theme attribute",
button.getThemeNames().contains(theme));
}

private void assertButtonHasNoThemeAttribute() {
Assert.assertNull(button.getElement().getAttribute("theme"));
}

private void assertIconBeforeText() {
Expand Down

0 comments on commit 9147eed

Please sign in to comment.