index.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {
  2. memo,
  3. useMemo,
  4. } from 'react'
  5. import type { NodeProps } from 'reactflow'
  6. import type { Node } from '../types'
  7. import { CUSTOM_NODE } from '../constants'
  8. import {
  9. NodeComponentMap,
  10. PanelComponentMap,
  11. } from './constants'
  12. import BaseNode from './_base/node'
  13. import BasePanel from './_base/panel'
  14. const CustomNode = (props: NodeProps) => {
  15. const nodeData = props.data
  16. const NodeComponent = NodeComponentMap[nodeData.type]
  17. return (
  18. <>
  19. <BaseNode { ...props }>
  20. <NodeComponent />
  21. </BaseNode>
  22. </>
  23. )
  24. }
  25. CustomNode.displayName = 'CustomNode'
  26. export const Panel = memo((props: Node) => {
  27. const nodeClass = props.type
  28. const nodeData = props.data
  29. const PanelComponent = useMemo(() => {
  30. if (nodeClass === CUSTOM_NODE)
  31. return PanelComponentMap[nodeData.type]
  32. return () => null
  33. }, [nodeClass, nodeData.type])
  34. if (nodeClass === CUSTOM_NODE) {
  35. return (
  36. <BasePanel key={props.id} {...props}>
  37. <PanelComponent />
  38. </BasePanel>
  39. )
  40. }
  41. return null
  42. })
  43. Panel.displayName = 'Panel'
  44. export default memo(CustomNode)