Skip to content

Commit

Permalink
perf(tab-view): shouldUseTab method replaces index with tab.key (#5239)
Browse files Browse the repository at this point in the history
  • Loading branch information
betavs authored Nov 5, 2023
1 parent 17b82c9 commit 278c145
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions components/lib/tabview/TabView.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export const TabView = React.forwardRef((inProps, ref) => {
const isSelected = (index) => index === activeIndex;
const getTabProp = (tab, name) => TabPanelBase.getCProp(tab, name);

const shouldUseTab = (tab, index) => {
return tab && ObjectUtils.isValidChild(tab, 'TabPanel') && hiddenTabsState.every((_i) => _i !== index);
const shouldUseTab = (tab) => {
return tab && ObjectUtils.isValidChild(tab, 'TabPanel') && hiddenTabsState.every((_i) => _i !== tab.key);
};

const findVisibleActiveTab = (i) => {
const tabsInfo = React.Children.map(props.children, (tab, index) => {
if (shouldUseTab(tab, index)) {
if (shouldUseTab(tab)) {
return { tab, index };
}
});
Expand All @@ -73,15 +73,18 @@ export const TabView = React.forwardRef((inProps, ref) => {
const onTabHeaderClose = (event, index) => {
event.preventDefault();

const { onBeforeTabClose, onTabClose, children } = props;
const { key } = children[index];

// give caller a chance to stop the selection
if (props.onBeforeTabClose && props.onBeforeTabClose({ originalEvent: event, index }) === false) {
if (onBeforeTabClose && onBeforeTabClose({ originalEvent: event, index }) === false) {
return;
}

setHiddenTabsState([...hiddenTabsState, index]);
setHiddenTabsState([...hiddenTabsState, key]);

if (props.onTabClose) {
props.onTabClose({ originalEvent: event, index });
if (onTabClose) {
onTabClose({ originalEvent: event, index });
}
};

Expand Down Expand Up @@ -275,7 +278,7 @@ export const TabView = React.forwardRef((inProps, ref) => {

const createTabHeaders = () => {
return React.Children.map(props.children, (tab, index) => {
if (shouldUseTab(tab, index)) {
if (shouldUseTab(tab)) {
return createTabHeader(tab, index);
}
});
Expand Down Expand Up @@ -331,7 +334,7 @@ export const TabView = React.forwardRef((inProps, ref) => {
ptm('panelcontainer')
);
const contents = React.Children.map(props.children, (tab, index) => {
if (shouldUseTab(tab, index) && (!props.renderActiveOnly || isSelected(index))) {
if (shouldUseTab(tab) && (!props.renderActiveOnly || isSelected(index))) {
const selected = isSelected(index);
const contentId = idState + '_content_' + index;
const ariaLabelledBy = idState + '_header_' + index;
Expand Down
2 changes: 1 addition & 1 deletion components/lib/tabview/tabview.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export interface TabViewState {
/**
* Current state of hidden tab.
*/
hiddenTabsState: number[];
hiddenTabsState: (number | string)[];
/**
* Current state of previous button.
* @defaultValue true
Expand Down

0 comments on commit 278c145

Please sign in to comment.