diff --git a/README.md b/README.md index 5f1917a6..233107f5 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ Unlike other Tree Components, react-arborist is designed as a [controlled compon | isOpenAccessor | "isOpen" | Used to get a node's openness state if it exists on a property other than "isOpen". | | openByDefault | true | Choose if the node should be open or closed when it has an undefined openness state. | | className | undefined | Adds a class to the containing div. | +| dndRootElement | undefined | The element for react-dnd to bind it's events to. Defaults to window. See https://github.com/brimdata/react-arborist/pull/33 | The only child of the Tree Component must be a NodeRenderer function as described below. diff --git a/package.json b/package.json index 38595916..587c6586 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "./packages/*" ], "scripts": { - "bump": "yarn workspace react-arborist version" + "bump": "yarn workspace react-arborist version", + "dev": "yarn workspace demo start" }, "private": true, "packageManager": "yarn@3.2.0", diff --git a/packages/demo/src/app.tsx b/packages/demo/src/app.tsx index ba628859..3b6825c2 100644 --- a/packages/demo/src/app.tsx +++ b/packages/demo/src/app.tsx @@ -31,7 +31,7 @@ export default function App() { return (
-

React Arborist

+

React Arborist

In this demo, we have the Game of Thrones family tree rendered with the Tree component. You can drag and drop the items in diff --git a/packages/demo/src/got.tsx b/packages/demo/src/got.tsx index dbe4818a..56d43a0c 100644 --- a/packages/demo/src/got.tsx +++ b/packages/demo/src/got.tsx @@ -1,39 +1,43 @@ -import React from "react"; +import React, { useRef } from "react"; // @ts-ignore import AutoSize from "react-virtualized-auto-sizer"; import { Tree, TreeApi } from "react-arborist"; import { Node } from "./node"; -import { useBackend } from "./backend"; +import { MyData, useBackend } from "./backend"; export function GotLineage() { const backend = useBackend(); + const rootElement = useRef(null); return ( - - {(props: any) => ( - { - // @ts-ignore - global.tree = tree; - }} - className="react-aborist" - data={backend.data} - getChildren="children" - isOpen="isOpen" - disableDrop={(d) => d.name === "House Arryn"} - hideRoot - indent={24} - onMove={backend.onMove} - onToggle={backend.onToggle} - onEdit={backend.onEdit} - rowHeight={22} - width={props.width} - height={props.height} - onClick={() => console.log("clicked the tree")} - onContextMenu={() => console.log("context menu the tree")} - > - {Node} - - )} - +

+ + {(props: any) => ( + { + // @ts-ignore + global.tree = tree; + }} + className="react-aborist" + data={backend.data} + getChildren="children" + isOpen="isOpen" + disableDrop={(d: MyData) => d.name === "House Arryn"} + hideRoot + indent={24} + onMove={backend.onMove} + onToggle={backend.onToggle} + onEdit={backend.onEdit} + rowHeight={22} + width={props.width} + height={props.height} + onClick={() => console.log("clicked the tree")} + onContextMenu={() => console.log("context menu the tree")} + dndRootElement={undefined} + > + {Node} + + )} + +
); } diff --git a/packages/react-arborist/package.json b/packages/react-arborist/package.json index daa68d38..84d2b82c 100644 --- a/packages/react-arborist/package.json +++ b/packages/react-arborist/package.json @@ -17,7 +17,8 @@ "scripts": { "build": "run-p 'build:**'", "build:js": "parcel build --target main --target module", - "build:types": "tsc --outDir dist" + "build:types": "tsc --outDir dist", + "watch": "yarn build:types --watch" }, "peerDependencies": { "react": ">= 16.14", diff --git a/packages/react-arborist/src/components/tree.tsx b/packages/react-arborist/src/components/tree.tsx index a10c3c86..1f89a6e1 100644 --- a/packages/react-arborist/src/components/tree.tsx +++ b/packages/react-arborist/src/components/tree.tsx @@ -1,4 +1,10 @@ -import { forwardRef, MouseEventHandler, ReactElement, useMemo, useRef } from "react"; +import { + forwardRef, + MouseEventHandler, + ReactElement, + useMemo, + useRef, +} from "react"; import { DndProvider } from "react-dnd"; import { HTML5Backend } from "react-dnd-html5-backend"; import { FixedSizeList } from "react-window"; @@ -20,8 +26,13 @@ const OuterElement = forwardRef(function Outer( const { children, ...rest } = props; const tree = useStaticContext(); return ( - // @ts-ignore -
+
@@ -91,6 +102,7 @@ export const Tree = forwardRef(function Tree( props.openByDefault, ] ); + return ( ( onClick={props.onClick} onContextMenu={props.onContextMenu} > - + - + diff --git a/packages/react-arborist/src/types.ts b/packages/react-arborist/src/types.ts index f2e35351..3a7027cc 100644 --- a/packages/react-arborist/src/types.ts +++ b/packages/react-arborist/src/types.ts @@ -102,26 +102,30 @@ export type StateContext = { selection: SelectionState; visibleIds: string[]; }; + +type BoolFunc = (data: T) => boolean; + export interface TreeProps { children: NodeRenderer; + className?: string | undefined; data: T; + disableDrag?: string | boolean | BoolFunc; + disableDrop?: string | boolean | BoolFunc; + dndRootElement?: globalThis.Node | null; + getChildren?: string | ((d: T) => T[]); + handle?: Ref>; // Deprecated height?: number; - width?: number; - rowHeight?: number; - indent?: number; hideRoot?: boolean; - onToggle?: ToggleHandler; - onMove?: MoveHandler; - onEdit?: EditHandler; - getChildren?: string | ((d: T) => T[]); - isOpen?: string | ((d: T) => boolean); - disableDrag?: string | boolean | ((d: T) => boolean); - disableDrop?: string | boolean | ((d: T) => boolean); - openByDefault?: boolean; - className?: string | undefined; - handle?: Ref>; + indent?: number; + isOpen?: string | BoolFunc; onClick?: MouseEventHandler; onContextMenu?: MouseEventHandler; + onEdit?: EditHandler; + onMove?: MoveHandler; + onToggle?: ToggleHandler; + openByDefault?: boolean; + rowHeight?: number; + width?: number; } export type TreeProviderProps = {