diff --git a/build/api/react-binding.api.md b/build/api/react-binding.api.md index 06f753c7f..727303dcd 100644 --- a/build/api/react-binding.api.md +++ b/build/api/react-binding.api.md @@ -434,6 +434,10 @@ export interface IfCallbackProps { children?: ReactNode; // (undocumented) condition: (accessor: EntityAccessor) => boolean; + // (undocumented) + else?: ReactNode; + // (undocumented) + then?: ReactNode; } // @public (undocumented) @@ -442,6 +446,10 @@ export interface IfFilterProps { children?: ReactNode; // (undocumented) condition: string | Filter; + // (undocumented) + else?: ReactNode; + // (undocumented) + then?: ReactNode; } // @public (undocumented) diff --git a/packages/react-binding/src/helperComponents/If.tsx b/packages/react-binding/src/helperComponents/If.tsx index 87e92173f..ea887d8b6 100644 --- a/packages/react-binding/src/helperComponents/If.tsx +++ b/packages/react-binding/src/helperComponents/If.tsx @@ -15,30 +15,37 @@ export type IfProps = export interface IfFilterProps { condition: string | Filter children?: ReactNode + then?: ReactNode + else?: ReactNode } export interface IfCallbackProps { condition: (accessor: EntityAccessor) => boolean children?: ReactNode + then?: ReactNode + else?: ReactNode } /** * @group Logic Components */ -export const If = Component(props => { - return typeof props.condition !== 'function' - ? {props.children} - : {props.children} +export const If = Component(({ condition, ...props }) => { + if (props.children && props.then) { + throw new Error('If component cannot have both children and then prop') + } + return typeof condition !== 'function' + ? + : }, 'If', ) const IfCallback = Component( - ({ children, condition }) => { + ({ children, condition, then, else: elseIn }) => { const entity = useEntity() const evaluated = useMemo(() => condition(entity), [condition, entity]) - return evaluated ? <>{children} : null + return <>{evaluated ? (children ?? then) : elseIn} }, ({ children }) => { return <>{children} @@ -47,7 +54,7 @@ const IfCallback = Component( ) const IfFilter = Component( - ({ children, condition }) => { + ({ children, condition, then, else: elseIn }) => { const env = useEnvironment() const entity = useEntity() @@ -56,8 +63,7 @@ const IfFilter = Component( () => new FilterEvaluator(schema).evaluateFilter(entity, QueryLanguage.desugarFilter(condition, env)), [condition, entity, env, schema], ) - - return evaluated ? <>{children} : null + return <>{evaluated ? (children ?? then) : elseIn} }, ({ children, condition }, env) => { const desugaredFilter = QueryLanguage.desugarFilter(condition, env) @@ -71,4 +77,3 @@ const IfFilter = Component( }, 'IfFilter', ) -