Skip to content

Commit

Permalink
Merge pull request #700 from contember/feat/if2
Browse files Browse the repository at this point in the history
feat(react-binding): add "then" and "else" props to If component
  • Loading branch information
matej21 authored May 27, 2024
2 parents 531ce33 + 1a93d07 commit d88bb2b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
8 changes: 8 additions & 0 deletions build/api/react-binding.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ export interface IfCallbackProps {
children?: ReactNode;
// (undocumented)
condition: (accessor: EntityAccessor) => boolean;
// (undocumented)
else?: ReactNode;
// (undocumented)
then?: ReactNode;
}

// @public (undocumented)
Expand All @@ -442,6 +446,10 @@ export interface IfFilterProps {
children?: ReactNode;
// (undocumented)
condition: string | Filter;
// (undocumented)
else?: ReactNode;
// (undocumented)
then?: ReactNode;
}

// @public (undocumented)
Expand Down
25 changes: 15 additions & 10 deletions packages/react-binding/src/helperComponents/If.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<IfProps>(props => {
return typeof props.condition !== 'function'
? <IfFilter condition={props.condition}>{props.children}</IfFilter>
: <IfCallback condition={props.condition}>{props.children}</IfCallback>
export const If = Component<IfProps>(({ condition, ...props }) => {
if (props.children && props.then) {
throw new Error('If component cannot have both children and then prop')
}
return typeof condition !== 'function'
? <IfFilter condition={condition} {...props} />
: <IfCallback condition={condition} {...props} />
},
'If',
)

const IfCallback = Component<IfCallbackProps>(
({ 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}</>
Expand All @@ -47,7 +54,7 @@ const IfCallback = Component<IfCallbackProps>(
)

const IfFilter = Component<IfFilterProps>(
({ children, condition }) => {
({ children, condition, then, else: elseIn }) => {
const env = useEnvironment()
const entity = useEntity()

Expand All @@ -56,8 +63,7 @@ const IfFilter = Component<IfFilterProps>(
() => 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)
Expand All @@ -71,4 +77,3 @@ const IfFilter = Component<IfFilterProps>(
},
'IfFilter',
)

0 comments on commit d88bb2b

Please sign in to comment.