Skip to content

Commit

Permalink
fix(dashboard): dashboard doesn't load properly if it has tabs (#21576)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>
  • Loading branch information
stephenLYZ and michael-s-molina committed Sep 27, 2022
1 parent 2cdd88a commit 24412e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
50 changes: 34 additions & 16 deletions superset-frontend/src/dashboard/components/gridComponents/Tabs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,7 @@ const StyledTabsContainer = styled.div`
export class Tabs extends React.PureComponent {
constructor(props) {
super(props);
let tabIndex = Math.max(
0,
findTabIndexByComponentId({
currentComponent: props.component,
directPathToChild: props.directPathToChild,
}),
);
if (tabIndex === 0 && props.activeTabs?.length) {
props.component.children.forEach((tabId, index) => {
if (tabIndex === 0 && props.activeTabs.includes(tabId)) {
tabIndex = index;
}
});
}
const { children: tabIds } = props.component;
const activeKey = tabIds[tabIndex];
const { tabIndex, activeKey } = this.getTabInfo(props);

this.state = {
tabIndex,
Expand Down Expand Up @@ -164,6 +149,15 @@ export class Tabs extends React.PureComponent {
this.setState(() => ({ tabIndex: maxIndex }));
}

// reset tab index if dashboard was changed
if (nextProps.dashboardId !== this.props.dashboardId) {
const { tabIndex, activeKey } = this.getTabInfo(nextProps);
this.setState(() => ({
tabIndex,
activeKey,
}));
}

if (nextProps.isComponentVisible) {
const nextFocusComponent = getLeafComponentIdFromPath(
nextProps.directPathToChild,
Expand Down Expand Up @@ -196,6 +190,30 @@ export class Tabs extends React.PureComponent {
}
}

getTabInfo = props => {
let tabIndex = Math.max(
0,
findTabIndexByComponentId({
currentComponent: props.component,
directPathToChild: props.directPathToChild,
}),
);
if (tabIndex === 0 && props.activeTabs?.length) {
props.component.children.forEach((tabId, index) => {
if (tabIndex === 0 && props.activeTabs.includes(tabId)) {
tabIndex = index;
}
});
}
const { children: tabIds } = props.component;
const activeKey = tabIds[tabIndex];

return {
tabIndex,
activeKey,
};
};

showDeleteConfirmModal = key => {
const { component, deleteComponent } = this.props;
AntdModal.confirm({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('Tabs', () => {
editMode: false,
availableColumnCount: 12,
columnWidth: 50,
dashboardId: 1,
onResizeStart() {},
onResize() {},
onResizeStop() {},
Expand Down Expand Up @@ -202,4 +203,15 @@ describe('Tabs', () => {
expect(modalMock.mock.calls).toHaveLength(1);
expect(deleteComponent.callCount).toBe(0);
});

it('should set new tab key if dashboardId was changed', () => {
const wrapper = shallow(<Tabs {...props} />);
expect(wrapper.state('activeKey')).toBe('TAB_ID');
wrapper.setProps({
...props,
dashboardId: 2,
component: dashboardLayoutWithTabs.present.TAB_ID,
});
expect(wrapper.state('activeKey')).toBe('ROW_ID');
});
});

0 comments on commit 24412e2

Please sign in to comment.