vaadin/flow-components

TabSheet: extend the API to use setAutoselect()

to-do42 opened this issue · 0 comments

Describe your motivation

Currently, the tabSheet component automatically set autoselection=true for the underlying Tabs component. This leads to the following issue: if one use lazy initialization for the tabsheet content and preselect another as the first tab, this tab is in fact selected and lazy initialised, but the first tab is also always initialised. This is not neccessary and may lead to bad performance.

Describe the solution you'd like

It could be prevented, if there is a delegate method setAutoselect(boolean value)in TabSheet.java for the underlying private Tabs tabs field.

public void setAutoselect(boolean value) {
    this.tabs.setAutoselect(value);
}

Describe alternatives you've considered

With some reflection magic, we can toggle this value the ugly way:

private void setAutoselect(TabSheet tabSheet, boolean b) {
    try {
        Field field = TabSheet.class.getDeclaredField("tabs");
        field.setAccessible(true);
        Method method = Tabs.class.getMethod("setAutoselect", boolean.class);
        Tabs tabs = (Tabs) field.get(tabSheet);
        method.invoke(tabs, false);
    } catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

Additional context

This issue was first reported in the Vaadin Flow forum:
https://vaadin.com/forum/t/tabsheet-always-lazy-initialise-the-first-tab/166199/1