Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DragAndDropListItemContext to assign the correct id to the parent #12589

Merged
merged 7 commits into from
Apr 4, 2024
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React from 'react';
import classes from './SchemaEditor.module.css';
import { TypesInspector } from '../TypesInspector';
import { SchemaInspector } from '../SchemaInspector';
Expand All @@ -11,35 +11,22 @@ import { useAddReference } from './hooks/useAddReference';
import { NodePanel } from '../NodePanel';

export const SchemaEditor = () => {
const { schemaModel, selectedTypePointer, selectedNodePointer } = useSchemaEditorAppContext();
const { schemaModel, selectedTypePointer } = useSchemaEditorAppContext();
const moveProperty = useMoveProperty();
const addReference = useAddReference();

const [expandedPropNodes, setExpandedPropNodes] = useState<string[]>([]);
const [expandedDefNodes, setExpandedDefNodes] = useState<string[]>([]);

const selectedPropertyParent = schemaModel.getParentNode(selectedNodePointer);

useEffect(() => {
if (selectedPropertyParent && !expandedPropNodes.includes(selectedPropertyParent.pointer)) {
setExpandedPropNodes((prevState) => [...prevState, selectedPropertyParent.pointer]);
}
}, [selectedPropertyParent, expandedPropNodes]);

const selectedDefinitionParent = schemaModel.getParentNode(selectedTypePointer);
useEffect(() => {
if (selectedDefinitionParent && !expandedDefNodes.includes(selectedDefinitionParent.pointer)) {
setExpandedDefNodes((prevState) => [...prevState, selectedDefinitionParent.pointer]);
}
}, [selectedPropertyParent, expandedDefNodes, selectedDefinitionParent]);

if (schemaModel.isEmpty()) return null;
const definitions: UiSchemaNodes = schemaModel.getDefinitions();
const selectedType = selectedTypePointer && schemaModel.getNode(selectedTypePointer);

return (
<>
<DragAndDropTree.Provider onAdd={addReference} onMove={moveProperty} rootId={ROOT_POINTER}>
<DragAndDropTree.Provider
onAdd={addReference}
onMove={moveProperty}
rootId={ROOT_POINTER}
itemId={selectedTypePointer ?? null}
>
<aside className={classes.inspector}>
<TypesInspector schemaItems={definitions} />
</aside>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import { DragAndDropRootContext } from './DragAndDropRootContext';
import type { HandleAdd, HandleDrop, HandleMove } from 'app-shared/types/dndTypes';
import { DragAndDropListItemContext } from '../DragAndDropListItem';

export interface DragAndDropProviderProps<T> {
children: ReactNode;
gap?: string;
onAdd: HandleAdd<T>;
onMove: HandleMove;
rootId: string;
itemId?: string;
}

export function DragAndDropProvider<T>({
Expand All @@ -19,14 +21,17 @@ export function DragAndDropProvider<T>({
onAdd,
onMove,
rootId,
itemId,
}: DragAndDropProviderProps<T>) {
const onDrop: HandleDrop<T> = (item, position) =>
item.isNew === true ? onAdd(item.payload, position) : onMove(item.id, position);
const uniqueDomId = useId(); // Can not be the same as root id because root id might not be unique (if there are multiple drag and drop lists)
return (
<DndProvider backend={HTML5Backend}>
<DragAndDropRootContext.Provider value={{ gap, rootId, onDrop, uniqueDomId }}>
{children}
<DragAndDropListItemContext.Provider value={{ isDisabled: false, itemId }}>
{children}
</DragAndDropListItemContext.Provider>
</DragAndDropRootContext.Provider>
</DndProvider>
);
Expand Down
Loading