import { Fragment, isValidElement, memo, type PropsWithChildren, type ReactNode, useRef, } from 'react'; import { useAutoFocus } from '../hooks/use-auto-focus'; import { hasComponentType } from '../use-lunatic/commons/component'; import { ComponentWrapper } from './shared/ComponentWrapper'; import { library } from './library'; import { ErrorBoundary } from 'react-error-boundary'; import { SlotsProvider, type LunaticSlotComponents, slottableComponent, } from './shared/HOC/slottableComponent'; import type { LunaticComponentProps } from './type'; type Props = { /** List of components to display (coming from getComponents). */ components: LunaticComponentProps[]; /** Should we memoized children. */ memo?: boolean; /** Key that trigger autofocus when it changes (pageTag). */ autoFocusKey?: string; /** Returns the list of extra props to add to components. */ componentProps?: (component: LunaticComponentProps) => V; /** Forbidden components. */ blocklist?: string[]; /** Add additional wrapper around each component. */ wrapper?: ( props: PropsWithChildren ) => ReactNode; /** Customized deep components. */ slots?: Partial; }; const LunaticComponentWrapper = slottableComponent( 'ComponentWrapper', ({ children }: PropsWithChildren) => { return
{children}
; } ); /** * Entry point for orchestrators, this component display the list of fields. * * Components offered by Lunatic can be seen at {@link https://github.com/InseeFr/Lunatic/tree/4.0/src/components} */ export function LunaticComponents({ components, autoFocusKey, componentProps, blocklist, memo, slots, wrapper = (args) => , }: Readonly>) { const wrapperRef = useRef(null); const hasComponents = components.length > 0; const WrapperComponent = autoFocusKey ? 'div' : Fragment; useAutoFocus(wrapperRef, hasComponents ? autoFocusKey : undefined); return ( {components.map((component, k) => { if (hasComponentType(component)) { if (blocklist?.includes(component.componentType)) { return ( {wrapper( { children: (
Component "{component.componentType}" is not allowed here
), index: k, ...component, } as any /* this is too dynamic */ )}
); } const props = { ...component, ...componentProps?.(component), propsTransformer: componentProps, }; return ( {wrapper( { children: memo ? ( ) : ( ), index: k, ...props, } as any /* this is too dynamic */ )} ); } // In some case (table for instance) we have static component that only have a label (no componentType) if (hasLabel(component)) { return ( {wrapper({ ...component, children: component.label, index: k, } as any)} ); } // Component is a ReactNode if (isValidElement(component)) { return ( {wrapper({ children: component, index: k, } as any)} ); } return null; })}
); } function LunaticComponent(props: LunaticComponentProps) { // Component is too dynamic to be typed const Component = (library as any)[props.componentType!]; return ( ); } function LunaticError({ error, }: Readonly<{ error: { toString: () => string } }>) { console.error(error); return (

Cannot render this component : {error.toString()}

); } const LunaticComponentMemo = memo(LunaticComponent); function computeId( component: unknown, fallback: number | string ): number | string { if ( component && typeof component === 'object' && 'id' in component && typeof component.id === 'string' ) { return component.id; } return fallback; } export function hasLabel( component: T ): component is { label: ReactNode } & T { return ( !!component && typeof component === 'object' && 'label' in component && isValidElement(component.label) ); }