Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: allow changing the icon #4594

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -31,14 +31,13 @@
public class Icon extends AbstractIcon<Icon> {

private static final String ICON_ATTRIBUTE_NAME = "icon";
private static final String ICON_COLLECTION_NAME = "vaadin";
private static final String VAADIN_ICON_COLLECTION_NAME = "vaadin";
private static final String STYLE_FILL = "fill";

/**
* Creates an Icon component that displays a Vaadin logo.
* Creates an empty Icon.
*/
public Icon() {
this(VaadinIcon.VAADIN_H);
vursen marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -49,8 +48,7 @@ public Icon() {
* the icon to display
*/
public Icon(VaadinIcon icon) {
this(ICON_COLLECTION_NAME,
icon.name().toLowerCase(Locale.ENGLISH).replace('_', '-'));
setIcon(icon);
}

/**
Expand All @@ -61,7 +59,7 @@ public Icon(VaadinIcon icon) {
* the icon name
*/
public Icon(String icon) {
this(ICON_COLLECTION_NAME, icon);
setIcon(icon);
}

/**
Expand All @@ -85,9 +83,94 @@ public Icon(String icon) {
* the icon name
*/
public Icon(String collection, String icon) {
setIcon(collection, icon);
}

/**
* Sets the icon to the given icon.
* <p>
* If the icon name contains a ":", the first part is used as the collection
* and the second part as the icon name. If the icon name does not contain a
* ":", the icon is assumed to be from the "vaadin" collection.
*
* @param icon
* the icon name
*/
public void setIcon(String icon) {
if (icon.contains(":")) {
String[] parts = icon.split(":", 2);
setIcon(parts[0], parts[1]);
} else {
String collection = getCollection();
if (collection == null) {
collection = VAADIN_ICON_COLLECTION_NAME;
}
setIcon(collection, icon);
}
}

/**
* /** Sets the icon to the given Vaadin icon.
Artur- marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* If the icon name contains a ":", the first part is used as the collection
* and the second part as the icon name. If the icon name does not contain a
* ":", the icon is assumed to be from the "vaadin" collection.
*
* @param icon
* the icon name
*/
public void setIcon(VaadinIcon icon) {
setIcon(VAADIN_ICON_COLLECTION_NAME,
icon.name().toLowerCase(Locale.ENGLISH).replace('_', '-'));
}

/**
* Gets the full icon name, including the collection.
*
* @return the icon name or {@code null} if no icon is set
*/
public String getIcon() {
return getElement().getAttribute(ICON_ATTRIBUTE_NAME);
}

/**
* Sets the icon to the given {@code icon} from the given
* {@code collection}.
*
* If you want to use a custom {@code <vaadin-iconset>} -based icon set, you
* also need to add a dependency and an import for it, example:
*
* <pre>
* <code>
* &#64;NpmPackage(value = "custom-icons", version = "1.0.0")
* &#64;JsModule("custom-icons/iconset.js")
* public class MyView extends Div {
* </code>
* </pre>
*
* @param collection
* the icon collection
* @param icon
* the icon name
*/
public void setIcon(String collection, String icon) {
getElement().setAttribute(ICON_ATTRIBUTE_NAME, collection + ':' + icon);
}

/**
* Gets the collection of the icon (the part before {@literal :}).
*
* @return the collection of the icon or {@code null} if no collection is
* set
*/
public String getCollection() {
String icon = getIcon();
if (icon != null && icon.contains(":")) {
return icon.substring(0, icon.indexOf(':'));
}
return null;
}

@Override
public void setColor(String color) {
if (color == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,50 @@
import org.junit.Test;

import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.shared.HasTooltip;

public class IconTest {

@Test
public void implementsHasTooltip() {
Icon icon = new Icon();
Assert.assertTrue(icon instanceof HasTooltip);
}

@Test
public void emptyIconIsEmpty() {
Assert.assertNull(new Icon().getIcon());
}

@Test
public void usesVaadinCollectionByDefault() {
Assert.assertEquals("vaadin:foo", new Icon("foo").getIcon());
}

@Test
public void canDefineCollectionInConstructor() {
Assert.assertEquals("bar:foo", new Icon("bar:foo").getIcon());
}

@Test
public void canDefineCollectionInSetter() {
Icon icon = new Icon();
icon.setIcon("bar:foo");
Assert.assertEquals("bar:foo", icon.getIcon());
}

@Test
public void setterUsesCurrentCollection() {
Icon icon = new Icon("bar:foo");
icon.setIcon("baz");
Assert.assertEquals("bar:baz", icon.getIcon());
}

@Test
public void canSetNewVaadinIcon() {
Copy link
Contributor

@vursen vursen May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since getCollection is a public method, it would be great to have a test for it too

Icon icon = new Icon("bar:foo");
icon.setIcon(VaadinIcon.ABSOLUTE_POSITION);
Assert.assertEquals("vaadin:absolute-position", icon.getIcon());
}
}