Skip to content

Commit f7e66db

Browse files
WIP: fix: replace existing slotted icon instead of adding duplicate
1 parent 9bf6096 commit f7e66db

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/main/java/com/flowingcode/vaadin/addons/togglebutton/ToggleButton.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ public ToggleButton setRightLabel(String label) {
218218
* @since 1.0.0
219219
*/
220220
public ToggleButton setLeftIcon(Component icon) {
221-
icon.getElement().setAttribute("slot", "left");
222-
add(icon);
221+
setSlottedIcon(icon, "left");
223222
return this;
224223
}
225224

@@ -231,8 +230,15 @@ public ToggleButton setLeftIcon(Component icon) {
231230
* @since 1.0.0
232231
*/
233232
public ToggleButton setRightIcon(Component icon) {
234-
icon.getElement().setAttribute("slot", "right");
235-
add(icon);
233+
setSlottedIcon(icon, "right");
236234
return this;
237235
}
236+
237+
private void setSlottedIcon(Component icon, String slot) {
238+
getElement().getChildren()
239+
.filter(e -> slot.equals(e.getAttribute("slot")))
240+
.forEach(e -> e.removeFromParent());
241+
icon.getElement().setAttribute("slot", slot);
242+
add(icon);
243+
}
238244
}

0 commit comments

Comments
 (0)