Skip to content

Commit

Permalink
feat: introduce gettabcount and deprecate getcomponentcount (#6495)
Browse files Browse the repository at this point in the history
* feat: introduce gettabcount and deprecate getcomponentcount

* Update vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/Tabs.java

Co-authored-by: Sascha Ißbrücker <[email protected]>

* Update vaadin-tabs-flow-parent/vaadin-tabs-flow/src/main/java/com/vaadin/flow/component/tabs/TabSheet.java

Co-authored-by: Sascha Ißbrücker <[email protected]>

---------

Co-authored-by: Sascha Ißbrücker <[email protected]>
  • Loading branch information
ugur-vaadin and sissbruecker authored Aug 6, 2024
1 parent 6312d50 commit d5ba93d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ public void setSelectedTab(Tab selectedTab) {
tabs.setSelectedTab(selectedTab);
}

/**
* Gets the number of tabs.
*
* @return the number of tabs
*/
public int getTabCount() {
return tabs.getTabCount();
}

/**
* Returns the tab at the given position.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void add(Component... components) {
*/
public void add(Tab... tabs) {
Objects.requireNonNull(tabs, "Tabs should not be null");
boolean wasEmpty = getComponentCount() == 0;
boolean wasEmpty = getTabCount() == 0;
Arrays.stream(tabs).map(
tab -> Objects.requireNonNull(tab, "Tab to add cannot be null"))
.map(Tab::getElement).forEach(getElement()::appendChild);
Expand Down Expand Up @@ -248,11 +248,11 @@ public void remove(Tab... tabs) {
int newSelectedIndex = getSelectedIndex() - lowerIndices;

// In case the last tab was removed
if (newSelectedIndex > 0 && newSelectedIndex >= getComponentCount()) {
newSelectedIndex = getComponentCount() - 1;
if (newSelectedIndex > 0 && newSelectedIndex >= getTabCount()) {
newSelectedIndex = getTabCount() - 1;
}

if (getComponentCount() == 0 || (isSelectedTab && !isAutoselect())) {
if (getTabCount() == 0 || (isSelectedTab && !isAutoselect())) {
newSelectedIndex = -1;
}

Expand Down Expand Up @@ -749,8 +749,19 @@ public int indexOf(Tab tab) {
* Gets the number of children tabs.
*
* @return the number of tabs
* @deprecated since 24.5, use {@link #getTabCount} instead.
*/
@Deprecated
public int getComponentCount() {
return getTabCount();
}

/**
* Gets the number of tabs.
*
* @return the number of tabs
*/
public int getTabCount() {
return (int) getChildren().count();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ public void addSecondTab_contentDisabled() {
Assert.assertFalse(content1.isEnabled());
}

@Test
public void addTabs_tabCountCorrect() {
Assert.assertEquals(0, tabSheet.getTabCount());

tabSheet.add("Tab 0", new Span("Content 0"));
tabSheet.add("Tab 1", new Span("Content 1"));

Assert.assertEquals(2, tabSheet.getTabCount());
}

@Test
public void changeTab_contentEnabled() {
tabSheet.add("Tab 0", new Span("Content 0"));
Expand Down Expand Up @@ -261,6 +271,7 @@ public void removeTab_removesTab() {
var tab = tabSheet.add("Tab 0", new Span("Content 0"));
tabSheet.remove(tab);
Assert.assertFalse(tab.getParent().isPresent());
Assert.assertEquals(0, tabSheet.getTabCount());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public class TabsTest {
public void createTabsInDefaultState() {
Tabs tabs = new Tabs();

assertThat("Initial child count is invalid", tabs.getComponentCount(),
is(0));
assertThat("Initial tab count is invalid", tabs.getTabCount(), is(0));
assertThat("Initial orientation is invalid", tabs.getOrientation(),
is(Tabs.Orientation.HORIZONTAL));
assertThat("Initial selected index is invalid", tabs.getSelectedIndex(),
Expand All @@ -59,8 +58,7 @@ public void createTabsWithChildren() {
Tab tab3 = new Tab("Tab three");
Tabs tabs = new Tabs(tab1, tab2, tab3);

assertThat("Initial child count is invalid", tabs.getComponentCount(),
is(3));
assertThat("Initial tab count is invalid", tabs.getTabCount(), is(3));
assertThat("Initial orientation is invalid", tabs.getOrientation(),
is(Tabs.Orientation.HORIZONTAL));
assertThat("Initial selected tab is invalid", tabs.getSelectedTab(),
Expand Down

0 comments on commit d5ba93d

Please sign in to comment.