Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-binding): add "then" and "else" props to If component #700

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
)

Loading