index.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import React from "react";
  2. import { Label, Node, NodeProps } from "reaflow";
  3. import ObjectNode from "./ObjectNode";
  4. import TextNode from "./TextNode";
  5. export interface CustomNodeProps<T> {
  6. width: number;
  7. height: number;
  8. value: T;
  9. isParent?: boolean;
  10. x: number;
  11. y: number;
  12. }
  13. const baseLabelStyle = {
  14. fill: "transparent",
  15. stroke: "transparent",
  16. strokeWidth: 0,
  17. };
  18. export const CustomNode = (nodeProps: NodeProps) => {
  19. const { properties } = nodeProps;
  20. return (
  21. <Node {...nodeProps} label={<Label style={baseLabelStyle} />}>
  22. {({ width, height, x, y, node }) => {
  23. if (Array.isArray(properties.text)) {
  24. return (
  25. <ObjectNode
  26. value={properties.text}
  27. width={width}
  28. height={height}
  29. x={x}
  30. y={y}
  31. />
  32. );
  33. }
  34. return (
  35. <TextNode
  36. node={node as NodeData}
  37. isParent={properties.data.isParent}
  38. value={properties.text}
  39. width={width}
  40. height={height}
  41. hasCollapse={properties.data.hasChild}
  42. x={x}
  43. y={y}
  44. />
  45. );
  46. }}
  47. </Node>
  48. );
  49. };