@@ -3,23 +3,23 @@ import { forwardRef, useRef, useEffect } from 'react';
3
3
import { isValidElementType } from 'react-is' ;
4
4
import { useRendererContext } from '../api/context' ;
5
5
import { reactiveStateFactory } from './reactiveState' ;
6
- import { type ReactComponent , type ReactWidget , createElementByWidget } from './components' ;
7
- import { ModelContextProvider } from './context' ;
6
+ import { type ReactComponent , type ReactWidget , createElementByWidget } from './elements' ;
8
7
import { appendExternalStyle } from '../utils/element' ;
9
8
10
9
import type {
11
10
RenderContext ,
12
11
IComponentTreeModel ,
13
- ComponentTreeModelOptions ,
12
+ CreateComponentTreeModelOptions ,
14
13
} from '@alilc/lowcode-renderer-core' ;
15
- import type { ReactInstance , CSSProperties , ForwardedRef } from 'react' ;
14
+ import type { ReactInstance , CSSProperties , ForwardedRef , ReactNode } from 'react' ;
16
15
17
16
export interface ComponentOptions {
18
17
displayName ?: string ;
19
- modelOptions ?: ComponentTreeModelOptions ;
18
+ modelOptions ?: Pick < CreateComponentTreeModelOptions , 'id' | 'metadata' > ;
20
19
21
- widgetCreated ?( widget : ReactWidget ) : void ;
22
- componentRefAttached ?( widget : ReactWidget , instance : ReactInstance ) : void ;
20
+ beforeElementCreate ?( widget : ReactWidget ) : ReactWidget ;
21
+ elementCreated ?( widget : ReactWidget , element : ReactNode ) : ReactNode ;
22
+ componentRefAttached ?( widget : ReactWidget , instance : ReactInstance | null ) : void ;
23
23
}
24
24
25
25
export interface LowCodeComponentProps {
@@ -37,6 +37,7 @@ const lowCodeComponentsCache = new Map<string, ReactComponent>();
37
37
export function getComponentByName (
38
38
name : string ,
39
39
{ packageManager, boostsManager } : RenderContext ,
40
+ componentOptions : ComponentOptions = { } ,
40
41
) : ReactComponent {
41
42
const result = lowCodeComponentsCache . get ( name ) || packageManager . getComponent ( name ) ;
42
43
@@ -58,7 +59,8 @@ export function getComponentByName(
58
59
} ) ;
59
60
}
60
61
61
- const lowCodeComponent = createComponentBySchema ( componentsTree [ 0 ] , {
62
+ const lowCodeComponent = createComponent ( componentsTree [ 0 ] , {
63
+ ...componentOptions ,
62
64
displayName : name ,
63
65
modelOptions : {
64
66
id : metadata . id ,
@@ -76,40 +78,49 @@ export function getComponentByName(
76
78
return result ;
77
79
}
78
80
79
- export function createComponentBySchema (
81
+ export function createComponent (
80
82
schema : string | Spec . ComponentTreeRoot ,
81
- { displayName = '__LowCodeComponent__' , modelOptions } : ComponentOptions = { } ,
83
+ componentOptions : ComponentOptions = { } ,
82
84
) {
85
+ const { displayName = '__LowCodeComponent__' , modelOptions } = componentOptions ;
86
+
83
87
const LowCodeComponent = forwardRef ( function (
84
88
props : LowCodeComponentProps ,
85
89
ref : ForwardedRef < any > ,
86
90
) {
87
- const renderContext = useRendererContext ( ) ;
88
- const { options, componentTreeModel } = renderContext ;
91
+ const context = useRendererContext ( ) ;
92
+ const { options : globalOptions , componentTreeModel } = context ;
89
93
90
94
const modelRef = useRef < IComponentTreeModel < ReactComponent , ReactInstance > > ( ) ;
91
95
92
96
if ( ! modelRef . current ) {
97
+ const finalOptions : CreateComponentTreeModelOptions = {
98
+ ...modelOptions ,
99
+ codeScopeValue : {
100
+ props,
101
+ } ,
102
+ stateCreator : reactiveStateFactory ,
103
+ dataSourceCreator : globalOptions . dataSourceCreator ,
104
+ } ;
105
+
93
106
if ( typeof schema === 'string' ) {
94
- modelRef . current = componentTreeModel . createById ( schema , modelOptions ) ;
107
+ modelRef . current = componentTreeModel . createById ( schema , finalOptions ) ;
95
108
} else {
96
- modelRef . current = componentTreeModel . create ( schema , modelOptions ) ;
109
+ modelRef . current = componentTreeModel . create ( schema , finalOptions ) ;
97
110
}
111
+ console . log (
112
+ '%c [ model ]-103' ,
113
+ 'font-size:13px; background:pink; color:#bf2c9f;' ,
114
+ modelRef . current ,
115
+ ) ;
98
116
}
99
117
100
118
const model = modelRef . current ! ;
101
- console . log ( '%c [ model ]-103' , 'font-size:13px; background:pink; color:#bf2c9f;' , model ) ;
102
119
103
120
const isConstructed = useRef ( false ) ;
104
121
const isMounted = useRef ( false ) ;
105
122
106
123
if ( ! isConstructed . current ) {
107
- model . initialize ( {
108
- defaultProps : props ,
109
- stateCreator : reactiveStateFactory ,
110
- dataSourceCreator : options . dataSourceCreator ,
111
- } ) ;
112
-
113
124
model . triggerLifeCycle ( 'constructor' ) ;
114
125
isConstructed . current = true ;
115
126
@@ -142,11 +153,9 @@ export function createComponentBySchema(
142
153
} , [ ] ) ;
143
154
144
155
return (
145
- < ModelContextProvider value = { model } >
146
- < div id = { props . id } className = { props . className } style = { props . style } ref = { ref } >
147
- { model . widgets . map ( ( w ) => createElementByWidget ( w , model . codeScope ) ) }
148
- </ div >
149
- </ ModelContextProvider >
156
+ < div id = { props . id } className = { props . className } style = { props . style } ref = { ref } >
157
+ { model . widgets . map ( ( w ) => createElementByWidget ( w , w . model . codeRuntime , componentOptions ) ) }
158
+ </ div >
150
159
) ;
151
160
} ) ;
152
161
0 commit comments