import { ResourceComponent } from "./ResourceComponent" ; import * as React from "react" ; import { ReactParsysProps } from "./ReactParsys" ; export interface ComponentConfig { depth ? : number ; parsys ? : ReactParsysProps ; component : any ; props ? : { [ name ] : any ; } ; transform ? : ( props : { [ name ] : any ; } , r : ResourceComponent < any , any , any > ) => { [ name ] : any ; } ; loadingComponent ? : typeof React . Component ; } export default class WrapperFactory { param config param resourceType return { TheWrapper } public static createWrapper ( config , resourceType ) { return class TheWrapper extends Wrapper { constructor ( props ? , context ? ) { super ( config , props , context ) ; } public getResourceType ( ) { return resourceType ; } } ; } } export class Wrapper extends ResourceComponent < any , any , any > { protected config : ComponentConfig ; protected getDepth ( ) { return this . config . depth || 0 ; } public constructor ( config , props ? , context ? ) { super ( props , context ) ; this . config = config ; } public create ( ) { let children ; if ( ! ! this . config . parsys ) { children = this . renderChildren ( this . config . parsys . path , this . config . parsys . childClassName , this . config . parsys . childElementName ) ; } let props = this . getResource ( ) ; if ( this . config . props ) { Object . keys ( this . config . props ) . forEach ( ( key ) => props [ key ] = this . config . props [ key ] ) ; } let newProps ; if ( this . config . transform ) { newProps = this . config . transform ( props , this ) ; } else { newProps = props ; } return React . createElement ( this . config . component , newProps , children ) ; } public renderBody ( ) { return this . create ( ) ; } protected renderLoading ( ) { let loadingComponent = this . config . loadingComponent ; if ( loadingComponent ) { return React . createElement ( loadingComponent , this . props ) ; } return ( < span > Loading < /span>); } }