Skip to content

Commit

Permalink
#518 done (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaadin-miki authored Aug 29, 2024
1 parent eec7c23 commit f32b61a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.miki.demo.builders;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.combobox.ComboBox;
import org.vaadin.miki.demo.ContentBuilder;
Expand Down Expand Up @@ -30,6 +31,8 @@ public void buildContent(SuperTabs<?> component, Consumer<Component[]> callback)
component.setTabHandler(event.getValue());
});

callback.accept(new Component[]{multilineTabs, tabHandlers});
@SuppressWarnings("unchecked")
final Button tabSelect = new Button("Switch to \"Open source\" tab with .setSelected()", event -> ((SuperTabs<String>)component).getTabHeader("Open source").ifPresent(tab -> tab.setSelected(true)));
callback.accept(new Component[]{multilineTabs, tabHandlers, tabSelect});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.tabs.Tab;
import com.vaadin.flow.component.tabs.Tabs;
import com.vaadin.flow.dom.PropertyChangeEvent;
import org.vaadin.miki.markers.WithHelperMixin;
import org.vaadin.miki.markers.WithHelperPositionableMixin;
import org.vaadin.miki.markers.WithIdMixin;
Expand Down Expand Up @@ -207,6 +208,20 @@ protected void addNewTab(T value, Tab tab, Component content, boolean select) {
else
this.updateValue();
}
tab.getElement().addPropertyChangeListener("selected", this::tabSelectedPropertyChanged);
}

private void tabSelectedPropertyChanged(PropertyChangeEvent propertyChangeEvent) {
if(propertyChangeEvent.getPropertyName().equals("selected") &&
(propertyChangeEvent.getOldValue() == null || !Boolean.parseBoolean(propertyChangeEvent.getOldValue().toString())) &&
propertyChangeEvent.getValue() != null &&
Boolean.parseBoolean(propertyChangeEvent.getValue().toString())) {
this.tabsToContents.entrySet().stream()
.filter(entry -> Objects.equals(entry.getKey().getElement(), propertyChangeEvent.getSource()))
.findFirst()
.map(Map.Entry::getKey)
.ifPresent(this.tabs::setSelectedTab);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class SuperTabsTest {

Expand Down Expand Up @@ -243,4 +244,20 @@ public void testChangingTabHandler() {
}
}

@Test
public void testTabSetSelected() {
final String tabTitle = "foo";
this.tabs.addTab("something", "anything", tabTitle, "another thing");
Assert.assertNotEquals(tabTitle, this.tabs.getValue());
final Optional<Tab> perhapsHeader = this.tabs.getTabHeader(tabTitle);
Assert.assertTrue(perhapsHeader.isPresent());
final Tab tab = perhapsHeader.get();
tab.setSelected(true);
Assert.assertEquals(tabTitle, this.tabs.getValue());

final Tab notThere = new Tab("oh wow");
notThere.setSelected(true);
Assert.assertEquals(tabTitle, this.tabs.getValue());
}

}

0 comments on commit f32b61a

Please sign in to comment.