Fix issue where tabs are lost when the user enters an invalid tabs value (#569)
This commit is contained in:
parent
19bd7d272a
commit
0b87c7431b
|
|
@ -46,7 +46,8 @@ const FIELD_VALUES: Record<
|
|||
// onSelectionChange: "Function Call",
|
||||
},
|
||||
TABS_WIDGET: {
|
||||
tabs: "Array<{ label: string, id: string }>",
|
||||
tabs:
|
||||
"Array<{ label: string, id: string(unique), widgetId: string(unique) }>",
|
||||
selectedTab: "string",
|
||||
isVisible: "boolean",
|
||||
},
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import { generateReactKey } from "./generators";
|
|||
import { ChartDataPoint } from "widgets/ChartWidget";
|
||||
import { FlattenedWidgetProps } from "reducers/entityReducers/canvasWidgetsReducer";
|
||||
import { isString } from "lodash";
|
||||
import log from "loglevel";
|
||||
|
||||
export type WidgetOperationParams = {
|
||||
operation: WidgetOperation;
|
||||
|
|
@ -200,18 +201,23 @@ const tabsWidgetTabsPropertyMigration = (
|
|||
?.filter(Boolean)
|
||||
.map((child: WidgetProps) => {
|
||||
if (child.type === WidgetTypes.TABS_WIDGET) {
|
||||
const tabs = isString(child.tabs) ? JSON.parse(child.tabs) : child.tabs;
|
||||
|
||||
const newTabs = tabs.map((tab: any) => {
|
||||
const childForTab = child.children
|
||||
?.filter(Boolean)
|
||||
.find((tabChild: WidgetProps) => tabChild.tabId === tab.id);
|
||||
if (childForTab) {
|
||||
tab.widgetId = childForTab.widgetId;
|
||||
}
|
||||
return tab;
|
||||
});
|
||||
child.tabs = JSON.stringify(newTabs);
|
||||
try {
|
||||
const tabs = isString(child.tabs)
|
||||
? JSON.parse(child.tabs)
|
||||
: child.tabs;
|
||||
const newTabs = tabs.map((tab: any) => {
|
||||
const childForTab = child.children
|
||||
?.filter(Boolean)
|
||||
.find((tabChild: WidgetProps) => tabChild.tabId === tab.id);
|
||||
if (childForTab) {
|
||||
tab.widgetId = childForTab.widgetId;
|
||||
}
|
||||
return tab;
|
||||
});
|
||||
child.tabs = JSON.stringify(newTabs);
|
||||
} catch (migrationError) {
|
||||
log.debug({ migrationError });
|
||||
}
|
||||
}
|
||||
if (child.children && child.children.length) {
|
||||
child = tabsWidgetTabsPropertyMigration(child);
|
||||
|
|
@ -266,6 +272,7 @@ const transformDSL = (currentDSL: ContainerWidgetProps<WidgetProps>) => {
|
|||
}
|
||||
if (currentDSL.version === 5) {
|
||||
currentDSL = tabsWidgetTabsPropertyMigration(currentDSL);
|
||||
currentDSL.version = 6;
|
||||
}
|
||||
|
||||
return currentDSL;
|
||||
|
|
|
|||
|
|
@ -143,26 +143,29 @@ class TabsWidget extends BaseWidget<
|
|||
.map(child => child.widgetId);
|
||||
// If the tabs and children are different,
|
||||
// add and/or remove tab container widgets
|
||||
if (_.xor(childWidgetIds, tabWidgetIds).length > 0) {
|
||||
const widgetIdsToRemove: string[] = _.without(
|
||||
childWidgetIds,
|
||||
...tabWidgetIds,
|
||||
);
|
||||
const widgetIdsToCreate: string[] = _.without(
|
||||
tabWidgetIds,
|
||||
...childWidgetIds,
|
||||
);
|
||||
this.addTabContainer(widgetIdsToCreate);
|
||||
this.removeTabContainer(widgetIdsToRemove);
|
||||
}
|
||||
|
||||
// If all tabs were removed.
|
||||
if (tabWidgetIds.length === 0) {
|
||||
const newTabContainerWidgetId = generateReactKey();
|
||||
const tabs = [
|
||||
{ id: "tab1", widgetId: newTabContainerWidgetId, label: "Tab 1" },
|
||||
];
|
||||
this.updateWidgetProperty("tabs", JSON.stringify(tabs));
|
||||
if (!this.props.invalidProps?.tabs) {
|
||||
if (_.xor(childWidgetIds, tabWidgetIds).length > 0) {
|
||||
const widgetIdsToRemove: string[] = _.without(
|
||||
childWidgetIds,
|
||||
...tabWidgetIds,
|
||||
);
|
||||
const widgetIdsToCreate: string[] = _.without(
|
||||
tabWidgetIds,
|
||||
...childWidgetIds,
|
||||
);
|
||||
this.addTabContainer(widgetIdsToCreate);
|
||||
this.removeTabContainer(widgetIdsToRemove);
|
||||
}
|
||||
|
||||
// If all tabs were removed.
|
||||
if (tabWidgetIds.length === 0) {
|
||||
const newTabContainerWidgetId = generateReactKey();
|
||||
const tabs = [
|
||||
{ id: "tab1", widgetId: newTabContainerWidgetId, label: "Tab 1" },
|
||||
];
|
||||
this.updateWidgetProperty("tabs", JSON.stringify(tabs));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.props.defaultTab) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user