1
- import React from 'react' ;
1
+ import React , { useEffect } from 'react' ;
2
2
import { Meta } from '@storybook/react/types-6-0' ;
3
3
4
4
import * as ChatUI from '../../src' ;
5
- import { ComponentsMap } from '../../src' ;
5
+ import { ComponentsMap , LazyComponentOnLoadParams } from '../../src' ;
6
6
7
- const { ComponentsProvider, LazyComponent } = ChatUI ;
7
+ const { ComponentsProvider, LazyComponent, useComponents } = ChatUI ;
8
8
9
9
export default {
10
10
title : 'ComponentsProvider' ,
@@ -89,24 +89,51 @@ export const LocalComponent = () => (
89
89
</ ComponentsProvider >
90
90
) ;
91
91
92
- function TestAsyncComponent ( ) {
92
+ function LazySlot ( ) {
93
+ const data = { list : [ { title : 'item-1' } , { title : 'item-2' } ] } ;
93
94
return (
94
- < div >
95
- < h1 > Example:</ h1 >
96
- < LazyComponent
97
- code = "slot"
98
- data = { { list : [ { title : 'item-1' } , { title : 'item-2' } ] } }
99
- meta = { { } }
100
- ctx = { ctx }
101
- onLoad = { ( a : any ) => console . log ( 'onLoad:' , a ) }
102
- />
103
- </ div >
95
+ < LazyComponent
96
+ code = "slot"
97
+ data = { data }
98
+ meta = { { } }
99
+ ctx = { ctx }
100
+ onLoad = { ( r : LazyComponentOnLoadParams ) => console . log ( 'slot - onLoad:' , r ) }
101
+ />
102
+ ) ;
103
+ }
104
+
105
+ function LazyRecommend ( ) {
106
+ const data = {
107
+ recommends : [
108
+ {
109
+ knowledgeId : 'q1' ,
110
+ title : 'Question 1' ,
111
+ } ,
112
+ {
113
+ knowledgeId : 'q2' ,
114
+ title : 'Question 2' ,
115
+ } ,
116
+ {
117
+ knowledgeId : 'q3' ,
118
+ title : 'Question 3' ,
119
+ } ,
120
+ ] ,
121
+ } ;
122
+ return (
123
+ < LazyComponent
124
+ code = "recommend"
125
+ data = { data }
126
+ meta = { { } }
127
+ ctx = { ctx }
128
+ onLoad = { ( r : any ) => console . log ( 'recommend - onLoad:' , r ) }
129
+ onError = { ( e : Error ) => console . log ( 'recommend - onError:' , e ) }
130
+ />
104
131
) ;
105
132
}
106
133
107
134
export const AsyncComponent = ( ) => (
108
135
< ComponentsProvider components = { components } >
109
- < TestAsyncComponent />
136
+ < LazySlot />
110
137
</ ComponentsProvider >
111
138
) ;
112
139
@@ -141,7 +168,7 @@ function TestComponentHasError() {
141
168
code = "slot"
142
169
onError = { ( err , errInfo ) => {
143
170
setErrMsg ( err . message ) ;
144
- console . log ( errInfo ) ;
171
+ console . log ( 222 , err , errInfo ) ;
145
172
} }
146
173
/>
147
174
{ errMsg && < pre > Error message: { errMsg } </ pre > }
@@ -164,6 +191,7 @@ function TestErrorUrl() {
164
191
code = "error-url"
165
192
onError = { ( err ) => {
166
193
setErrMsg ( err . message ) ;
194
+ console . log ( 222 , err . message ) ;
167
195
} }
168
196
/>
169
197
{ errMsg && < pre > Error message: { errMsg } </ pre > }
@@ -221,3 +249,118 @@ export const AsyncDecorator = () => (
221
249
< TestAsyncDecorator />
222
250
</ ComponentsProvider >
223
251
) ;
252
+
253
+ function TestAddComponent ( ) {
254
+ const { addComponent, hasComponent } = useComponents ( ) ;
255
+ const forceUpdate = useForceUpdate ( ) ;
256
+
257
+ function addRecommend ( ) {
258
+ if ( ! hasComponent ( 'recommend' ) ) {
259
+ addComponent ( 'recommend' , {
260
+ name : 'AlimeComponentRecommend' ,
261
+ url : '//g.alicdn.com/alime-components/recommend/0.1.0/index.js' ,
262
+ } ) ;
263
+ // forceUpdate();
264
+ }
265
+ }
266
+
267
+ const onClick = React . useCallback ( ( ) => {
268
+ forceUpdate ( ) ;
269
+ } , [ forceUpdate ] ) ;
270
+
271
+ function handleImgLoad ( e : any ) {
272
+ console . log ( 'img load' , e ) ;
273
+ }
274
+
275
+ return (
276
+ < div >
277
+ < img onLoad = { handleImgLoad } src = "11" alt = "" />
278
+ < button type = "button" onClick = { onClick } >
279
+ update inner
280
+ </ button >
281
+ < button onClick = { addRecommend } > add recommend</ button >
282
+ < LazyRecommend />
283
+ </ div >
284
+ ) ;
285
+ }
286
+
287
+ function useForceUpdate ( ) {
288
+ const [ , forceUpdate ] = React . useState ( false ) ;
289
+
290
+ return React . useCallback ( ( ) => {
291
+ forceUpdate ( ( s ) => ! s ) ;
292
+ } , [ ] ) ;
293
+ }
294
+
295
+ export const AddComponent = ( ) => {
296
+ const forceUpdate = useForceUpdate ( ) ;
297
+ const renderCount = React . useRef ( 0 ) ;
298
+
299
+ React . useEffect ( ( ) => {
300
+ renderCount . current += 1 ;
301
+ } ) ;
302
+
303
+ const onClick = React . useCallback ( ( ) => {
304
+ forceUpdate ( ) ;
305
+ } , [ forceUpdate ] ) ;
306
+
307
+ return (
308
+ < div >
309
+ < button type = "button" onClick = { onClick } >
310
+ update
311
+ </ button >
312
+ < div > Render count: { renderCount . current } </ div >
313
+
314
+ < ComponentsProvider components = { components } >
315
+ < TestAddComponent />
316
+ </ ComponentsProvider >
317
+
318
+ < ComponentsProvider components = { components } >
319
+ < LazySlot />
320
+ </ ComponentsProvider >
321
+ </ div >
322
+ ) ;
323
+ } ;
324
+
325
+ export const MutableComponents = ( ) => {
326
+ const [ myComponents , setMyMyComponents ] = React . useState ( { } ) ;
327
+ const forceUpdate = useForceUpdate ( ) ;
328
+
329
+ function addSlot ( ) {
330
+ setMyMyComponents ( {
331
+ ...myComponents ,
332
+ slot : {
333
+ name : 'AlimeComponentSlot' ,
334
+ url : '//g.alicdn.com/alime-components/slot/0.1.3/index.js' ,
335
+ } ,
336
+ } ) ;
337
+ }
338
+
339
+ function addRecommend ( ) {
340
+ setMyMyComponents ( {
341
+ ...myComponents ,
342
+ recommend : {
343
+ name : 'AlimeComponentRecommend' ,
344
+ url : '//g.alicdn.com/alime-components/recommend/0.1.0/index.js' ,
345
+ } ,
346
+ } ) ;
347
+ }
348
+
349
+ const onClick = React . useCallback ( ( ) => {
350
+ forceUpdate ( ) ;
351
+ } , [ forceUpdate ] ) ;
352
+
353
+ return (
354
+ < div >
355
+ < button type = "button" onClick = { onClick } >
356
+ update
357
+ </ button >
358
+ < button onClick = { addSlot } > add slot</ button >
359
+ < button onClick = { addRecommend } > add recommend</ button >
360
+ < ComponentsProvider components = { myComponents } >
361
+ < LazySlot />
362
+ < LazyRecommend />
363
+ </ ComponentsProvider >
364
+ </ div >
365
+ ) ;
366
+ } ;
0 commit comments