Skip to content

Commit

Permalink
feat: extract getPreparedCopyItemOptions & onCopyFulfill to Daskkit p…
Browse files Browse the repository at this point in the history
…rops
  • Loading branch information
DaryaLari committed Nov 13, 2024
1 parent d80e9e0 commit 7eaa5b6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/components/DashKit/DashKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import {
import {RegisterManager, UpdateManager, reflowLayout} from '../../utils';
import DashKitView from '../DashKitView/DashKitView';
import GridLayout from '../GridLayout/GridLayout';
import {OverlayControlItem} from '../OverlayControls/OverlayControls';
import {OverlayControlItem, OverlayControlsCtxShape} from '../OverlayControls/OverlayControls';

interface DashKitGeneralProps {
interface DashKitGeneralProps
extends Pick<OverlayControlsCtxShape, 'getPreparedCopyItemOptions' | 'onCopyFulfill'> {
config: Config;
editMode: boolean;
draggableHandleClassName?: string;
Expand Down
9 changes: 4 additions & 5 deletions src/components/DashKit/__stories__/DashKitShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {Button, Icon} from '@gravity-ui/uikit';
import {ActionPanel, DashKit, DashKitProps} from '../../..';
import {MenuItems} from '../../../helpers';
import {i18n} from '../../../i18n';
import type {ConfigItem, OverlayControlItem, PreparedCopyItemOptions} from '../../../index';
import type {ConfigItem, OverlayControlItem} from '../../../index';
import {cn} from '../../../utils/cn';

import {Demo, DemoRow} from './Demo';
Expand Down Expand Up @@ -236,10 +236,9 @@ export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {
overlayControls={this.state.enableOverlayControls ? this.controls : null}
overlayMenuItems={this.state.overlayMenuItems}
focusable={true}
context={{
onCopySuccess: (data: PreparedCopyItemOptions) =>
console.info('Copied: ' + JSON.stringify(data)),
}}
onCopyFulfill={(_error, data) =>
console.info('Copied: ' + JSON.stringify(data))
}
/>
</DemoRow>
</Demo>
Expand Down
24 changes: 15 additions & 9 deletions src/components/OverlayControls/OverlayControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ interface OverlayControlsDefaultProps {
size: ButtonSize;
}

interface OverlayControlsProps extends OverlayControlsDefaultProps {
export interface OverlayControlsProps extends OverlayControlsDefaultProps {
configItem: ConfigItem;
onItemClick?: () => void | null;
}
Expand All @@ -94,7 +94,7 @@ export type PreparedCopyItemOptions<C extends object = {}> = PreparedCopyItemOpt
copyContext?: C;
};

type DashKitCtx = React.Context<{
export interface OverlayControlsCtxShape {
overlayControls?: Record<string, OverlayControlItem[]>;
context: Record<string, any>;
menu: MenuItem[];
Expand All @@ -103,7 +103,11 @@ type DashKitCtx = React.Context<{
editItem: (item: ConfigItem) => void;
removeItem: (id: string) => void;
getLayoutItem: (id: string) => ConfigLayout | void;
}>;
getPreparedCopyItemOptions?: (options: PreparedCopyItemOptions) => PreparedCopyItemOptions;
onCopyFulfill?: (error: null | unknown, data?: PreparedCopyItemOptions) => void;
}

type OverlayControlsCtx = React.Context<OverlayControlsCtxShape>;

const DEFAULT_DROPDOWN_MENU = [MenuItems.Copy, MenuItems.Delete];

Expand All @@ -114,7 +118,7 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
view: 'flat',
size: 'm',
};
context!: React.ContextType<DashKitCtx>;
context!: React.ContextType<OverlayControlsCtx>;
render() {
const {position} = this.props;
const items = this.getItems();
Expand Down Expand Up @@ -405,18 +409,20 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
targetInnerId,
};

if (typeof this.context.context?.getPreparedCopyItemOptions === 'function') {
const getPreparedCopyItemOptions =
this.context?.getPreparedCopyItemOptions ??
this.context.context?.getPreparedCopyItemOptions;

if (typeof getPreparedCopyItemOptions === 'function') {
options = this.context.context.getPreparedCopyItemOptions(options);
}

try {
localStorage.setItem(COPIED_WIDGET_STORE_KEY, JSON.stringify(options));
if (typeof this.context.context?.onCopySuccess === 'function') {
this.context.context.onCopySuccess(options);
}
this.context.onCopyFulfill?.(null, options);
} catch (e) {
if (typeof this.context.context?.onCopyError === 'function') {
this.context.context.onCopyError(e);
this.context.onCopyFulfill?.(e);
}
}
// https://stackoverflow.com/questions/35865481/storage-event-not-firing
Expand Down
4 changes: 4 additions & 0 deletions src/hocs/withContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,16 @@ function useMemoStateContext(props) {
editItem: props.onItemEdit,
removeItem: onItemRemove,
getLayoutItem: getLayoutItem,
getPreparedCopyItemOptions: props.getPreparedCopyItemOptions,
onCopyFulfill: props.onCopyFulfill,
}),
[
props.overlayControls,
props.context,
props.itemsStateAndParams,
props.onItemEdit,
props.getPreparedCopyItemOptions,
props.onCopyFulfill,
overlayMenuItems,
itemsParams,
onItemRemove,
Expand Down

0 comments on commit 7eaa5b6

Please sign in to comment.