import { ViewProps } from '../View' ; import { mapHash } from '../util/object' ; import { ComponentType , Component , createElement } from '../vdom' ; import { ViewRoot } from '../common/ViewRoot' ; import { RenderHook , MountArg } from '../common/render-hook' ; import { ViewContext , ViewContextType } from '../ViewContext' ; import { ViewOptions } from '../options' ; import { Duration } from '../datelib/duration' ; export type ViewComponent = Component < ViewProps > ; export type ViewComponentType = ComponentType < ViewProps > ; export type ViewConfigInput = ViewComponentType | ViewOptions ; export type ViewConfigInputHash = { [ viewType ] : ViewConfigInput ; } ; export interface ViewConfig { superType : string ; component : ViewComponentType | null ; rawOptions : ViewOptions ; } export type ViewConfigHash = { [ viewType ] : ViewConfig ; } ; export function parseViewConfigs ( inputs ) { return mapHash ( inputs , parseViewConfig ) ; } function parseViewConfig ( input ) { let rawOptions = typeof input === 'function' ? { component : input } : input ; let { component } = rawOptions ; if ( rawOptions . content ) { component = createViewHookComponent ( rawOptions ) ; } return { superType : rawOptions . type as any , component : component as any , rawOptions , } ; } export interface SpecificViewContentArg extends ViewProps { nextDayThreshold : Duration ; } export type SpecificViewMountArg = MountArg < SpecificViewContentArg > ; function createViewHookComponent ( options ) { return ( viewProps ) => ( < ViewContextType . Consumer > { ( context ) => ( < ViewRoot viewSpec = { context . viewSpec } > { ( viewElRef , viewClassNames ) => { let hookProps = { ... viewProps , nextDayThreshold : context . options . nextDayThreshold , } ; return ( < RenderHook hookProps = { hookProps } classNames = { options . classNames as any } content = { options . content as any } didMount = { options . didMount as any } willUnmount = { options . willUnmount as any } elRef = { viewElRef } > { ( rootElRef , customClassNames , innerElRef , innerContent ) => ( < div className = { viewClassNames . concat ( customClassNames ) . join ( ' ' ) } ref = { rootElRef } > { innerContent } < /div>)} < /RenderHook>); } } < /ViewRoot>)} < /ViewContextType.Consumer>); }