-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: adressed comments on pr about <ConditionalRender />
- Loading branch information
1 parent
444209a
commit ebc0f7e
Showing
4 changed files
with
75 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 26 additions & 33 deletions
59
packages/core/src/components/renderers/conditional-render/ConditionalRender.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,44 @@ | ||
|
||
import { QuestionModel } from "../../../model/QuestionModel"; | ||
import { useEffect, useState } from "react"; | ||
import React from "react"; | ||
import { resolveQuickFormService } from "../../../services/QuickFormServices"; | ||
import { useQuickForm } from "../../../state"; | ||
import { Question } from "../../../components/question/Question"; | ||
|
||
type ConditionalRenderProps = { | ||
model: QuestionModel; | ||
style?: React.CSSProperties; | ||
engine: string; | ||
rule: string; | ||
children: JSX.Element; | ||
} | ||
|
||
export const ConditionalRender: React.FC<ConditionalRenderProps> = ({ model, style }) => { | ||
const logger = resolveQuickFormService("logger"); | ||
export const ConditionalRender = ({ engine, rule, children }: ConditionalRenderProps) => { | ||
const [visible, setIsVisible] = useState(false); | ||
const { getCurrentSlide } = useQuickForm(); | ||
logger.log("ConditionalRender for question {@model} InputProps", model); | ||
// const logger = resolveQuickFormService("logger"); | ||
// logger.log("ConditionalRender for question {@model} InputProps", model); | ||
|
||
// function evalInScope(js: string, contextAsScope: any) { | ||
// return new Function(`with (this) { return (${js}); }`).call(contextAsScope); | ||
// } | ||
useEffect(() => { | ||
const shouldRender = functionInScope(rule, { getCurrentSlide }); | ||
setIsVisible(shouldRender) | ||
}, [getCurrentSlide().questions]) | ||
|
||
interface Context { | ||
[key: string]: any; | ||
if (!visible) { | ||
return null; | ||
} | ||
|
||
function functionInScope(js: string, context: Context): boolean { | ||
const keys = Object.keys(context); | ||
const values = keys.map(key => context[key]); | ||
return children; | ||
} | ||
|
||
const func: Function = new Function(...keys, `return ${js};`); | ||
interface Context { | ||
[key: string]: any; | ||
} | ||
|
||
return (func as (...args: any[]) => any)(...values); | ||
} | ||
function functionInScope(js: string, context: Context): boolean { | ||
const keys = Object.keys(context); | ||
const values = keys.map(key => context[key]); | ||
|
||
useEffect(() => { | ||
if (model.visible && model.visible?.rule) { | ||
const shouldRender = functionInScope(model.visible.rule, { getCurrentSlide }); | ||
setIsVisible(shouldRender) | ||
} | ||
}, [getCurrentSlide().questions]) | ||
const func: Function = new Function(...keys, `return ${js};`); | ||
|
||
if (!visible) { | ||
return null; | ||
} | ||
return (func as (...args: any[]) => any)(...values); | ||
} | ||
|
||
return ( | ||
<Question key={model.logicalName} style={style} model={model} /> | ||
) | ||
} | ||
// function evalInScope(js: string, contextAsScope: any) { | ||
// return new Function(`with (this) { return (${js}); }`).call(contextAsScope); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters