blazorblazor-server-side.net-8.0blazorise

Blazorise SelectedTabChanged method called twice hence ends up selecting wrong tab


In my blazor server project, i am using blazorise UI library. Using the tab component works just fine in almost all the cases. However controlling the tabs programmatically, in a couple instances the SelectedTabChanged method is invoked twice. First with the actual tab invoked via code secondly on its own with default tab. e-g invoking OnSelectedTabChanged with SecondTab results in selection of FirstTab after second invocation.

Following is my markup:

<Tabs @ref="@tabs" SelectedTab="@selectedTab" SelectedTabChanged="@OnSelectedTabChanged">
    <Items>
        <Tab Name="FirstTab">First tab</Tab>
        <Tab Name="SecondTab">Second Tab</Tab>
        <Tab Name="ThirdTab">Third tab</Tab>
    </Items>
</Tabs>
<TabsContent @ref="@tabsContent" SelectedPanel="@selectedPanel" SelectedPanelChanged="@OnSelectedPanelChanged">
                <TabPanel Name=FirstTab">
.
.
.
@code
{
    Tabs tabs = default!;
    TabsContent tabsContent = default!;
    void OnSelectedTabChanged(string name)
    {
        selectedTab = name;
        tabsContent.SelectPanel(name);
    }
    void OnSelectedPanelChanged(string name)
    {
        selectedPanel = name;
    }
}

There was a similar issue reported on github but according to them its fixed.

Any help will be appreciated.


Solution

  • The problem in my code was that i was manually calling tabsContent.SelectPanel(name); which ends up invoking OnSelectedPanelChanged and OnSelectedTabChanged

    Changing the code to following fixed it:

    <Row>
    <Column ColumnSize="ColumnSize.IsAuto.OnDesktop">
      <Tabs @ref="@tabs" @bind-SelectedTab="@selectedTab">
          <Items>
              <Tab Name="FirstTab">First tab</Tab>
              <Tab Name="SecondTab">Second Tab</Tab>
              <Tab Name="ThirdTab">Third tab</Tab>
          </Items>
      </Tabs>
    </Column>
    </Row>
      <TabsContent @bind-SelectedPanel="@selectedTab>
              <TabPanel Name="FirstTab"></TabPanel>
              <TabPanel Name="SecondTab"></TabPanel>
              <TabPanel Name="ThirdTab"></TabPanel>
    .
    .
    .
    
    @code
    {
        public string selectedTab { get; set; }
        Tabs tabs = default!;
        TabsContent tabsContent = default!;
    }
    

    Controlling the tabs in code via selectedTab variable was enough. Details in this github issue from creator.