import { useConstantValueInvariant } from '@contember/react-utils' import { ComponentType, ReactElement, ReactNode } from 'react' import { useEntityListSubTree } from '../accessorPropagation' import type { SugaredQualifiedEntityList, SugaredUnconstrainedQualifiedEntityList, TreeRootId } from '@contember/binding' import { Environment, MarkerFactory, PRIMARY_KEY_NAME, TreeNodeEnvironmentFactory } from '@contember/binding' import { Component } from './Component' import { EntityList, EntityListBaseProps } from './EntityList' import { Field } from './Field' export interface EntityListSubTreeAdditionalProps { variables?: Environment.ValuesMapWithFactory } export type EntityListSubTreeProps = { treeRootId?: TreeRootId children?: ReactNode } & EntityListSubTreeAdditionalProps & (SugaredQualifiedEntityList | SugaredUnconstrainedQualifiedEntityList) & ( | {} | { listComponent: ComponentType listProps?: ListProps } ) /** * Creates a subtree with list of entities in current data binding context. * * @example * ``` * * ``` * * @group Data binding */ export const EntityListSubTree = Component( (props: EntityListSubTreeProps) => { useConstantValueInvariant(props.isCreating, 'EntityListSubTree: cannot update isCreating') const accessor = useEntityListSubTree(props, ...props.treeRootId ? [props.treeRootId] : []) // TODO revive top level hasOneRelationPath // {parameters.value.hasOneRelationPath.length ? // {props.children} }, { generateBranchMarker: (props, fields, environment) => { if ('isCreating' in props && props.isCreating) { return MarkerFactory.createUnconstrainedEntityListSubTreeMarker(props, fields, environment) } return MarkerFactory.createEntityListSubTreeMarker(props, fields, environment) }, staticRender: props => ( {props.children} ), generateEnvironment: (props, oldEnvironment) => { const environment = oldEnvironment.withVariables(props.variables) return TreeNodeEnvironmentFactory.createEnvironmentForEntityListSubtree(environment, props) }, }, 'EntityListSubTree', ) as (props: EntityListSubTreeProps) => ReactElement