1- import { observable , reaction } from 'mobx' ;
1+ import { observable } from 'mobx' ;
22
33import { ComponentTag , WebCellProps , FunctionComponent } from './utility' ;
44import { WebCellClass , WebCell } from './WebCell' ;
5- import { component , observer } from './decorator' ;
5+ import { component , observer , reaction } from './decorator' ;
66import { createCell } from './renderer' ;
77
88export interface AsyncBoxProps extends WebCellProps {
99 loader : ( ) => Promise < ComponentTag > ;
10+ delegatedProps ?: WebCellProps ;
1011}
1112
1213@component ( {
@@ -20,40 +21,28 @@ export class AsyncBox extends WebCell<AsyncBoxProps>() {
2021 @observable
2122 component ?: ComponentTag ;
2223
23- get delegatedProps ( ) {
24- return Object . fromEntries (
25- Object . entries ( Object . getOwnPropertyDescriptors ( this ) )
26- . map ( ( [ key , { value } ] ) => value != null && [ key , value ] )
27- . filter ( Boolean )
28- ) ;
29- }
24+ @observable
25+ delegatedProps ?: AsyncBoxProps [ 'delegatedProps' ] ;
26+
3027 connectedCallback ( ) {
31- if ( this . load instanceof Function ) this . load ( ) ;
28+ super . connectedCallback ( ) ;
3229
33- this . disposers . push ( reaction ( ( ) => this . loader , this . load ) ) ;
30+ this . load ( ) ;
3431 }
3532
36- protected load = async ( ) => {
33+ @reaction ( ( element : AsyncBox ) => element . loader )
34+ protected async load ( ) {
3735 this . component = undefined ;
3836 this . component = await this . loader ( ) ;
3937
4038 this . emit ( 'load' , this . component ) ;
41- } ;
39+ }
4240
4341 render ( ) {
44- const {
45- component : Tag ,
46- props : { defaultSlot, ...props } ,
47- delegatedProps
48- } = this ;
42+ const { component : Tag , props, delegatedProps } = this ;
43+ const { defaultSlot, ...data } = { ...props , ...delegatedProps } ;
4944
50- return (
51- Tag && (
52- < Tag { ...delegatedProps } { ...props } >
53- { defaultSlot }
54- </ Tag >
55- )
56- ) ;
45+ return Tag && < Tag { ...data } > { defaultSlot } </ Tag > ;
5746 }
5847}
5948
@@ -67,6 +56,9 @@ export function lazy<
6756 T extends ( ) => Promise < { default : FunctionComponent | WebCellClass } >
6857> ( loader : T ) {
6958 return ( props : GetAsyncProps < T > ) => (
70- < AsyncBox { ...props } loader = { async ( ) => ( await loader ( ) ) . default } />
59+ < AsyncBox
60+ delegatedProps = { props }
61+ loader = { async ( ) => ( await loader ( ) ) . default }
62+ />
7163 ) ;
7264}
0 commit comments