diff --git a/changelogs/upcoming/7713.md b/changelogs/upcoming/7713.md new file mode 100644 index 00000000000..1f53a7e94ff --- /dev/null +++ b/changelogs/upcoming/7713.md @@ -0,0 +1,3 @@ +**Bug fixes** + +- Fixed an `EuiTabbedContent` edge case bug that occurred when updated with a completely different set of `tabs` diff --git a/src/components/tabs/tabbed_content/__snapshots__/tabbed_content.test.tsx.snap b/src/components/tabs/tabbed_content/__snapshots__/tabbed_content.test.tsx.snap index 7d16b9bd716..374403f8198 100644 --- a/src/components/tabs/tabbed_content/__snapshots__/tabbed_content.test.tsx.snap +++ b/src/components/tabs/tabbed_content/__snapshots__/tabbed_content.test.tsx.snap @@ -1,125 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`EuiTabbedContent behavior when selected tab state isn't controlled by the owner, select the first tab by default 1`] = ` -
- Elasticsearch content -
-- updated Kibana content -
-updated Kibana content
, - }, - ], - }); + it("does not reset the existing selected tab if the tab's contents update", () => { + const { rerender, getAllByRole, getByTestSubject } = render( +World
, + }, + ]} + /> + ); + expect(getAllByRole('tab')[0]).toHaveAttribute('aria-selected', 'true'); }); }); }); diff --git a/src/components/tabs/tabbed_content/tabbed_content.tsx b/src/components/tabs/tabbed_content/tabbed_content.tsx index 99ed28a5bcc..7a81e25ea25 100644 --- a/src/components/tabs/tabbed_content/tabbed_content.tsx +++ b/src/components/tabs/tabbed_content/tabbed_content.tsx @@ -86,8 +86,7 @@ export class EuiTabbedContent extends Component< // Only track selection state if it's not controlled externally. let selectedTabId; if (!selectedTab) { - selectedTabId = - (initialSelectedTab && initialSelectedTab.id) || tabs[0].id; + selectedTabId = initialSelectedTab?.id || tabs[0].id; } this.state = { @@ -154,11 +153,10 @@ export class EuiTabbedContent extends Component< // Allow the consumer to control tab selection. const selectedTab = externalSelectedTab || - tabs.find( - (tab: EuiTabbedContentTab) => tab.id === this.state.selectedTabId - ); + tabs.find((tab) => tab.id === this.state.selectedTabId) || + tabs[0]; // Fall back to the first tab if a selected tab can't be found - const { content: selectedTabContent, id: selectedTabId } = selectedTab!; + const { content: selectedTabContent, id: selectedTabId } = selectedTab; return (