Skip to content

Commit

Permalink
[Lens] refactor dnd
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Jul 11, 2023
1 parent 15a86c3 commit 17b52fe
Show file tree
Hide file tree
Showing 33 changed files with 1,209 additions and 1,110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* Side Public License, v 1.
*/

import React, { useContext, useMemo } from 'react';
import { DragContext, DragDrop, DropOverlayWrapper, DropType } from '@kbn/dom-drag-drop';
import React, { useMemo } from 'react';
import { DragDrop, DropOverlayWrapper, DropType, useDragDropContext } from '@kbn/dom-drag-drop';
import { EuiEmptyPrompt, EuiPanel } from '@elastic/eui';

const DROP_PROPS = {
Expand All @@ -34,8 +34,8 @@ export interface ExampleDropZoneProps {
}

export const ExampleDropZone: React.FC<ExampleDropZoneProps> = ({ onDropField }) => {
const dragDropContext = useContext(DragContext);
const draggingFieldName = dragDropContext.dragging?.id;
const [{ dragging }] = useDragDropContext();
const draggingFieldName = dragging?.id;

const onDroppingField = useMemo(() => {
if (!draggingFieldName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* Side Public License, v 1.
*/

import React, { useCallback, useContext, useMemo, useRef } from 'react';
import React, { useCallback, useMemo, useRef } from 'react';
import type { DataView } from '@kbn/data-views-plugin/public';
import { generateFilters } from '@kbn/data-plugin/public';
import { ChildDragDropProvider, DragContext } from '@kbn/dom-drag-drop';
import { ChildDragDropProvider, useDragDropContext } from '@kbn/dom-drag-drop';
import {
UnifiedFieldListSidebarContainer,
type UnifiedFieldListSidebarContainerProps,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const FieldListSidebar: React.FC<FieldListSidebarProps> = ({
onAddFieldToWorkspace,
onRemoveFieldFromWorkspace,
}) => {
const dragDropContext = useContext(DragContext);
const dragDropContext = useDragDropContext();
const unifiedFieldListContainerRef = useRef<UnifiedFieldListSidebarContainerApi>(null);
const filterManager = services.data?.query?.filterManager;

Expand All @@ -80,7 +80,7 @@ export const FieldListSidebar: React.FC<FieldListSidebarProps> = ({
}, [unifiedFieldListContainerRef]);

return (
<ChildDragDropProvider {...dragDropContext}>
<ChildDragDropProvider value={dragDropContext}>
<UnifiedFieldListSidebarContainer
ref={unifiedFieldListContainerRef}
variant="responsive"
Expand Down
14 changes: 7 additions & 7 deletions packages/kbn-dom-drag-drop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ We aren't using EUI or another library, due to the fact that Lens visualizations
First, place a RootDragDropProvider at the root of your application.

```js
<RootDragDropProvider onTrackUICounterEvent={...}>
<RootDragDropProvider customMiddleware={...}>
... your app here ...
</RootDragDropProvider>
```

If you have a child React application (e.g. a visualization), you will need to pass the drag / drop context down into it. This can be obtained like so:

```js
const context = useContext(DragContext);
const context = useDragDropContext();
```

In your child application, place a `ChildDragDropProvider` at the root of that, and spread the context into it:
In your child application, place a `ChildDragDropProvider` at the root of that, and assign the context into it:

```js
<ChildDragDropProvider {...context}>... your child app here ...</ChildDragDropProvider>
<ChildDragDropProvider value={context}>... your child app here ...</ChildDragDropProvider>
```

This enables your child application to share the same drag / drop context as the root application.
Expand All @@ -49,7 +49,7 @@ To enable dragging an item, use `DragDrop` with both a `draggable` and a `value`
To enable dropping, use `DragDrop` with both a `dropTypes` attribute that should be an array with at least one value and an `onDrop` handler attribute. `dropType` should only be truthy if is an item being dragged, and if a drop of the dragged item is supported.

```js
const { dragging } = useContext(DragContext);
const [ dndState ] = useDragDropContext()

return (
<DragDrop
Expand All @@ -69,13 +69,13 @@ return (
To create a reordering group, surround the elements from the same group with a `ReorderProvider`:

```js
<ReorderProvider id="groupId">... elements from one group here ...</ReorderProvider>
<ReorderProvider>... elements from one group here ...</ReorderProvider>
```

The children `DragDrop` components must have props defined as in the example:

```js
<ReorderProvider id="groupId">
<ReorderProvider>
<div className="field-list">
{fields.map((f) => (
<DragDrop
Expand Down
6 changes: 4 additions & 2 deletions packages/kbn-dom-drag-drop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

export {
type DragDropIdentifier,
type DragContextValue,
type DragContextState,
type DropType,
type DraggingIdentifier,
type DragDropAction,
type DropOverlayWrapperProps,
DragDrop,
DragContext,
useDragDropContext,
RootDragDropProvider,
ChildDragDropProvider,
ReorderProvider,
DropOverlayWrapper,
type DropOverlayWrapperProps,
} from './src';

export { DropTargetSwapDuplicateCombine } from './src/drop_targets';
Loading

0 comments on commit 17b52fe

Please sign in to comment.