Skip to content

Commit

Permalink
refactor(react): update components getter for TreeNodeWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jun 11, 2021
1 parent ee780e6 commit ad9f1a1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/react/src/widgets/ComponentTreeWidget/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, { Fragment, useEffect } from 'react'
import React, { Fragment, useEffect, useContext, createContext } from 'react'
import { useTree, useDesigner, useRegistry } from '../../hooks'
import { TreeNodeContext } from '../../context'
import { TreeNode } from '@designable/core'
import { observer } from '@formily/reactive-react'

const ComponentsContext = createContext<IComponents>({})
export interface IComponents {
[key: string]: React.JSXElementConstructor<any>
}
Expand All @@ -14,22 +16,19 @@ export interface IComponentTreeWidgetProps {
}

export interface ITreeNodeWidgetProps {
components: IComponents
node: TreeNode
children?: React.ReactChild
}

export const TreeNodeWidget: React.FC<ITreeNodeWidgetProps> = observer(
(props: ITreeNodeWidgetProps) => {
const designer = useDesigner(props.node?.designerProps?.effects)
const components = props.components
const components = useContext(ComponentsContext)
const node = props.node
const renderChildren = () => {
if (node?.designerProps?.selfRenderChildren) return []
return node?.children?.map((child) => {
return (
<TreeNodeWidget key={child.id} node={child} components={components} />
)
return <TreeNodeWidget key={child.id} node={child} />
})
}
const renderProps = (extendsProps: any = {}) => {
Expand Down Expand Up @@ -96,7 +95,9 @@ export const ComponentTreeWidget: React.FC<IComponentTreeWidgetProps> =
}
return (
<div style={props.style} className={props.className} {...dataId}>
<TreeNodeWidget node={tree} components={props.components} />
<ComponentsContext.Provider value={props.components}>
<TreeNodeWidget node={tree} />
</ComponentsContext.Provider>
</div>
)
})
Expand Down

0 comments on commit ad9f1a1

Please sign in to comment.