-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to #796
- Loading branch information
Showing
8 changed files
with
135 additions
and
31 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
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
9 changes: 9 additions & 0 deletions
9
packages/form-js-viewer/src/render/context/LocalExpressionContext.js
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createContext } from 'preact'; | ||
|
||
const LocalExpressionContext = createContext({ | ||
this: null, | ||
parent: null, | ||
i: null | ||
}); | ||
|
||
export default LocalExpressionContext; |
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
28 changes: 18 additions & 10 deletions
28
packages/form-js-viewer/src/render/hooks/useExpressionEvaluation.js
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,26 +1,34 @@ | ||
import useService from './useService'; | ||
import useFilteredFormData from './useFilteredFormData'; | ||
import { useMemo } from 'preact/hooks'; | ||
import LocalExpressionContext from '../context/LocalExpressionContext'; | ||
import { useContext, useMemo } from 'preact/hooks'; | ||
import { wrapExpressionContext } from '../../util'; | ||
|
||
/** | ||
* Evaluate a string reactively based on the expressionLanguage and form data. | ||
* If the string is not an expression, it is returned as is. | ||
* Memoised to minimize re-renders. | ||
* | ||
* @param {string} value | ||
* The function is memoized to minimize re-renders. | ||
* | ||
* @param {string} value - The string to evaluate. | ||
* @returns {any} - Evaluated value or the original value if not an expression. | ||
*/ | ||
export default function useExpressionEvaluation(value) { | ||
const formData = useFilteredFormData(); | ||
|
||
const filteredData = useFilteredFormData(); | ||
const localExpressionContext = useContext(LocalExpressionContext); | ||
const expressionLanguage = useService('expressionLanguage'); | ||
|
||
return useMemo(() => { | ||
const expressionContext = useMemo(() => { | ||
if (localExpressionContext) { | ||
wrapExpressionContext(filteredData, localExpressionContext); | ||
} | ||
return filteredData; | ||
}, [ filteredData, localExpressionContext ]); | ||
|
||
return useMemo(() => { | ||
if (expressionLanguage && expressionLanguage.isExpression(value)) { | ||
return expressionLanguage.evaluate(value, formData); | ||
return expressionLanguage.evaluate(value, expressionContext); | ||
} | ||
|
||
return value; | ||
|
||
}, [ expressionLanguage, formData, value ]); | ||
}, [ expressionLanguage, expressionContext, value ]); | ||
} |
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
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