Skip to content

Commit

Permalink
fix: error typing & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
DaryaLari committed Nov 14, 2024
1 parent 6bc95c6 commit 48e04b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ interface DashKitProps {
noOverlay?: boolean;
focusable?: boolean;
draggableHandleClassName?: string;
getPreparedCopyItemOptions?: (options: PreparedCopyItemOptions) => PreparedCopyItemOptions;
onCopyFulfill?: (error: null | Error, data?: PreparedCopyItemOptions) => void;
}
```

Expand All @@ -84,6 +86,8 @@ interface DashKitProps {
- **onResizeStart**: ReactGridLayout called when item resize started
- **onResize**: ReactGridLayout called while item resizing
- **onResizeStop**: ReactGridLayout called when item resize stoped
- **getPreparedCopyItemOptions**: Called for converting copied item to serializable object before saving it to localstorage. It should be used instead of deprecated `context.getPreparedCopyItemOptions` prop
- **onCopyFulfill**: Called when item copy finished with `error=null` and defined `data` on successful operation done and with `error: Error` without `data` otherwise

## Usage

Expand Down
11 changes: 9 additions & 2 deletions src/components/OverlayControls/OverlayControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface OverlayControlsCtxShape {
removeItem: (id: string) => void;
getLayoutItem: (id: string) => ConfigLayout | void;
getPreparedCopyItemOptions?: (options: PreparedCopyItemOptions) => PreparedCopyItemOptions;
onCopyFulfill?: (error: null | unknown, data?: PreparedCopyItemOptions) => void;
onCopyFulfill?: (error: null | Error, data?: PreparedCopyItemOptions) => void;
}

type OverlayControlsCtx = React.Context<OverlayControlsCtxShape>;
Expand Down Expand Up @@ -409,6 +409,12 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
targetInnerId,
};

if (this.context.context?.getPreparedCopyItemOptions) {
console.warn?.(
'`context.getPreparedCopyItemOptions` is deprecated. Please use `getPreparedCopyItemOptions` prop instead',
);
}

const getPreparedCopyItemOptions =
this.context?.getPreparedCopyItemOptions ??
this.context.context?.getPreparedCopyItemOptions;
Expand All @@ -421,7 +427,8 @@ class OverlayControls extends React.Component<OverlayControlsProps> {
localStorage.setItem(COPIED_WIDGET_STORE_KEY, JSON.stringify(options));
this.context.onCopyFulfill?.(null, options);
} catch (e) {
this.context.onCopyFulfill?.(e);
const error = e instanceof Error ? e : new Error('Unknown error while copying item');
this.context.onCopyFulfill?.(error);
}
// https://stackoverflow.com/questions/35865481/storage-event-not-firing
window.dispatchEvent(new Event('storage'));
Expand Down

0 comments on commit 48e04b3

Please sign in to comment.