Skip to content

Commit

Permalink
feat: add targetId and targetInnerId to options on copy item (#201)
Browse files Browse the repository at this point in the history
* feat: add targetId to options on copy item

* feat: add targetInnerId

* fix: update usage of vars
  • Loading branch information
mournfulCoroner authored Sep 23, 2024
1 parent ae6147d commit 3739a79
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
30 changes: 26 additions & 4 deletions src/components/OverlayControls/OverlayControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ import {
} from '../../constants';
import {DashkitOvelayControlsContext} from '../../context/DashKitContext';
import {i18n} from '../../i18n';
import type {ConfigItem, ConfigLayout, ItemState, PluginBase, StringParams} from '../../shared';
import {
type ConfigItem,
type ConfigLayout,
type ItemState,
type ItemsStateAndParamsBase,
type PluginBase,
type StringParams,
isItemWithTabs,
resolveItemInnerId,
} from '../../shared';
import {MenuItem, Settings} from '../../typings';
import {cn} from '../../utils/cn';

Expand Down Expand Up @@ -77,6 +86,8 @@ type PreparedCopyItemOptionsArg = Pick<ConfigItem, 'data' | 'type' | 'defaults'
w: number;
h: number;
};
targetId: string;
targetInnerId?: string;
};

export type PreparedCopyItemOptions<C extends object = {}> = PreparedCopyItemOptionsArg & {
Expand All @@ -87,8 +98,8 @@ type DashKitCtx = React.Context<{
overlayControls?: Record<string, OverlayControlItem[]>;
context: Record<string, any>;
menu: MenuItem[];
itemsStateAndParams: ItemsStateAndParamsBase;
itemsParams: Record<string, StringParams>;
itemsState: Record<string, ItemState>;
editItem: (item: ConfigItem) => void;
removeItem: (id: string) => void;
getLayoutItem: (id: string) => ConfigLayout | void;
Expand Down Expand Up @@ -251,11 +262,11 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
}
private renderDropdownMenu(isOnlyOneItem: boolean) {
const {view, size, onItemClick} = this.props;
const {menu: contextMenu, itemsParams, itemsState} = this.context;
const {menu: contextMenu, itemsParams, itemsStateAndParams} = this.context;

const configItem = this.props.configItem;
const itemParams = itemsParams[configItem.id];
const itemState = itemsState[configItem.id];
const itemState = itemsStateAndParams[configItem.id]?.state || {};

const menu = contextMenu?.length > 0 ? contextMenu : DEFAULT_DROPDOWN_MENU;

Expand Down Expand Up @@ -370,6 +381,15 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
const {configItem} = this.props;
const correspondedItemLayout = this.context.getLayoutItem(configItem.id);

let targetInnerId;

if (isItemWithTabs(this.props.configItem)) {
targetInnerId = resolveItemInnerId({
item: this.props.configItem,
itemsStateAndParams: this.context.itemsStateAndParams,
});
}

let options: PreparedCopyItemOptions = {
timestamp: Date.now(),
data: configItem.data,
Expand All @@ -380,6 +400,8 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
w: correspondedItemLayout!.w,
h: correspondedItemLayout!.h,
},
targetId: this.props.configItem.id,
targetInnerId,
};

if (typeof this.context.context?.getPreparedCopyItemOptions === 'function') {
Expand Down
12 changes: 6 additions & 6 deletions src/hocs/withContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,20 +493,20 @@ function useMemoStateContext(props) {
overlayControls: props.overlayControls,
context: props.context,
menu: overlayMenuItems,
itemsParams: itemsParams,
itemsState: itemsState,
itemsStateAndParams: props.itemsStateAndParams,
itemsParams,
editItem: props.onItemEdit,
removeItem: onItemRemove,
getLayoutItem: getLayoutItem,
}),
[
itemsParams,
itemsState,
props.overlayControls,
props.context,
props.itemsStateAndParams,
props.onItemEdit,
onItemRemove,
props.overlayControls,
overlayMenuItems,
itemsParams,
onItemRemove,
getLayoutItem,
],
);
Expand Down
1 change: 1 addition & 0 deletions src/shared/modules/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export function resolveItemInnerId({
const {id} = item;
const stateTabId: string | undefined = (itemsStateAndParams as ItemsStateAndParamsBase)[id]
?.state?.tabId;

const {tabs} = (item as ConfigItemWithTabs).data;
if (stateTabId && tabs.some((tab) => tab.id === stateTabId)) {
return stateTabId;
Expand Down

0 comments on commit 3739a79

Please sign in to comment.