@openmrs/esm-form-engine-lib 4.3.1-pre.2701 → 4.3.1-pre.2711
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/inputs/multi-select/multi-select.component.js +5 -1
- package/dist/src/components/inputs/multi-select/multi-select.component.d.ts.map +1 -1
- package/dist/src/utils/post-submission-action-helper.d.ts +13 -0
- package/dist/src/utils/post-submission-action-helper.d.ts.map +1 -1
- package/dist/utils/post-submission-action-helper.js +27 -50
- package/package.json +1 -1
- package/src/components/inputs/multi-select/multi-select.component.tsx +7 -1
- package/src/utils/post-submission-action-helper.ts +26 -60
|
@@ -106,10 +106,14 @@ const MultiSelect = ({ field, value, errors, warnings, setFieldValue })=>{
|
|
|
106
106
|
warn: warnings.length > 0,
|
|
107
107
|
warnText: warnings[0]?.message
|
|
108
108
|
}) : /*#__PURE__*/ React.createElement(CheckboxGroup, {
|
|
109
|
+
invalid: errors.length > 0,
|
|
110
|
+
invalidText: errors[0]?.message,
|
|
109
111
|
legendText: /*#__PURE__*/ React.createElement(FieldLabel, {
|
|
110
112
|
field: field
|
|
111
113
|
}),
|
|
112
|
-
readOnly: isTrue(field.readonly)
|
|
114
|
+
readOnly: isTrue(field.readonly),
|
|
115
|
+
warn: warnings.length > 0,
|
|
116
|
+
warnText: warnings[0]?.message
|
|
113
117
|
}, selectOptions?.map((value, index)=>{
|
|
114
118
|
return /*#__PURE__*/ React.createElement(Checkbox, {
|
|
115
119
|
className: styles.checkbox,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multi-select.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/inputs/multi-select/multi-select.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+C,MAAM,OAAO,CAAC;AAKpE,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAO1D,QAAA,MAAM,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,
|
|
1
|
+
{"version":3,"file":"multi-select.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/inputs/multi-select/multi-select.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+C,MAAM,OAAO,CAAC;AAKpE,OAAO,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAO1D,QAAA,MAAM,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CA+I9C,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
|
@@ -1,2 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evaluates a post-submission action's `enabled` expression against the obs of the first encounter. Each
|
|
3
|
+
* variable in the expression is a field id, resolved to the value of the obs whose `formFieldPath` contains
|
|
4
|
+
* it (or the `uuid` for coded values). Field ids that aren't valid identifiers (e.g. `1stVisit`) can be read
|
|
5
|
+
* with `useFieldValue('1stVisit')`.
|
|
6
|
+
*
|
|
7
|
+
* Fields without an obs resolve to `null`, and `undefined` is also treated as `null`, so `field === null` and
|
|
8
|
+
* `field === undefined` both test whether the field has a value.
|
|
9
|
+
*
|
|
10
|
+
* The result is coerced to a boolean, so non-boolean expressions are treated as truthy or falsy.
|
|
11
|
+
*
|
|
12
|
+
* @throws If the expression cannot be parsed or evaluated
|
|
13
|
+
*/
|
|
1
14
|
export declare function evaluatePostSubmissionExpression(expression: string, encounters: any[]): boolean;
|
|
2
15
|
//# sourceMappingURL=post-submission-action-helper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"post-submission-action-helper.d.ts","sourceRoot":"","sources":["../../../src/utils/post-submission-action-helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"post-submission-action-helper.d.ts","sourceRoot":"","sources":["../../../src/utils/post-submission-action-helper.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,wBAAgB,gCAAgC,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,GAAG,OAAO,CAgB/F"}
|
|
@@ -1,57 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
import { compile, evaluate, extractVariableNames } from "@openmrs/esm-framework";
|
|
2
|
+
/**
|
|
3
|
+
* Evaluates a post-submission action's `enabled` expression against the obs of the first encounter. Each
|
|
4
|
+
* variable in the expression is a field id, resolved to the value of the obs whose `formFieldPath` contains
|
|
5
|
+
* it (or the `uuid` for coded values). Field ids that aren't valid identifiers (e.g. `1stVisit`) can be read
|
|
6
|
+
* with `useFieldValue('1stVisit')`.
|
|
7
|
+
*
|
|
8
|
+
* Fields without an obs resolve to `null`, and `undefined` is also treated as `null`, so `field === null` and
|
|
9
|
+
* `field === undefined` both test whether the field has a value.
|
|
10
|
+
*
|
|
11
|
+
* The result is coerced to a boolean, so non-boolean expressions are treated as truthy or falsy.
|
|
12
|
+
*
|
|
13
|
+
* @throws If the expression cannot be parsed or evaluated
|
|
14
|
+
*/ export function evaluatePostSubmissionExpression(expression, encounters) {
|
|
2
15
|
const encounter = encounters[0];
|
|
3
|
-
const regx = /(?:\w+|'(?:\\'|[^'\n])*')/g;
|
|
4
|
-
let match;
|
|
5
|
-
const fieldIds = new Set();
|
|
6
16
|
try {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const isFloat = /^-?\d+\.\d+$/.test(value);
|
|
12
|
-
if (!(value.startsWith("'") && value.endsWith("'")) && typeof value === 'string' && !isBoolean && !isNumber && !isFloat) {
|
|
13
|
-
fieldIds.add(value);
|
|
14
|
-
}
|
|
17
|
+
const ast = compile(expression);
|
|
18
|
+
const variables = {};
|
|
19
|
+
for (const name of extractVariableNames(ast)){
|
|
20
|
+
variables[name] = getFieldValue(name, encounter);
|
|
15
21
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
fieldToValueMap = getFieldValues(fieldIds, encounter);
|
|
20
|
-
}
|
|
21
|
-
if (Object.keys(fieldToValueMap).length) {
|
|
22
|
-
replacedExpression = expression.replace(/(\w+)/g, (match)=>{
|
|
23
|
-
return Object.prototype.hasOwnProperty.call(fieldToValueMap, match) ? fieldToValueMap[match] : match;
|
|
24
|
-
});
|
|
25
|
-
} else {
|
|
26
|
-
replacedExpression = expression;
|
|
27
|
-
}
|
|
28
|
-
return eval(replacedExpression);
|
|
22
|
+
variables.useFieldValue = (fieldId)=>getFieldValue(fieldId, encounter);
|
|
23
|
+
variables.undefined = null;
|
|
24
|
+
return Boolean(evaluate(ast, variables));
|
|
29
25
|
} catch (error) {
|
|
30
|
-
throw new Error('Error evaluating expression'
|
|
26
|
+
throw new Error('Error evaluating expression', {
|
|
27
|
+
cause: error
|
|
28
|
+
});
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
function
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
let value = encounter.obs?.find((item)=>item.formFieldPath.includes(fieldId))?.value;
|
|
37
|
-
if (typeof value === 'object') {
|
|
38
|
-
value = value.uuid;
|
|
39
|
-
}
|
|
40
|
-
if (value) {
|
|
41
|
-
value = formatValue(value);
|
|
42
|
-
}
|
|
43
|
-
result[fieldId] = value;
|
|
44
|
-
});
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
//This function wraps string values in single quotes which Javascript will evaluate
|
|
48
|
-
function formatValue(value) {
|
|
49
|
-
if (typeof value === 'string') {
|
|
50
|
-
if (value.length >= 2 && value[0] === "'" && value[value.length - 1] === "'") {
|
|
51
|
-
return value;
|
|
52
|
-
} else {
|
|
53
|
-
return `'${value}'`;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return value;
|
|
31
|
+
function getFieldValue(fieldId, encounter) {
|
|
32
|
+
const value = encounter.obs?.find((item)=>item.formFieldPath.includes(fieldId))?.value;
|
|
33
|
+
return (typeof value === 'object' ? value?.uuid : value) ?? null;
|
|
57
34
|
}
|
package/package.json
CHANGED
|
@@ -109,7 +109,13 @@ const MultiSelect: React.FC<FormFieldInputProps> = ({ field, value, errors, warn
|
|
|
109
109
|
warnText={warnings[0]?.message}
|
|
110
110
|
/>
|
|
111
111
|
) : (
|
|
112
|
-
<CheckboxGroup
|
|
112
|
+
<CheckboxGroup
|
|
113
|
+
invalid={errors.length > 0}
|
|
114
|
+
invalidText={errors[0]?.message}
|
|
115
|
+
legendText={<FieldLabel field={field} />}
|
|
116
|
+
readOnly={isTrue(field.readonly)}
|
|
117
|
+
warn={warnings.length > 0}
|
|
118
|
+
warnText={warnings[0]?.message}>
|
|
113
119
|
{selectOptions?.map((value, index) => {
|
|
114
120
|
return (
|
|
115
121
|
<Checkbox
|
|
@@ -1,71 +1,37 @@
|
|
|
1
|
+
import { compile, evaluate, extractVariableNames, type VariablesMap } from '@openmrs/esm-framework';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Evaluates a post-submission action's `enabled` expression against the obs of the first encounter. Each
|
|
5
|
+
* variable in the expression is a field id, resolved to the value of the obs whose `formFieldPath` contains
|
|
6
|
+
* it (or the `uuid` for coded values). Field ids that aren't valid identifiers (e.g. `1stVisit`) can be read
|
|
7
|
+
* with `useFieldValue('1stVisit')`.
|
|
8
|
+
*
|
|
9
|
+
* Fields without an obs resolve to `null`, and `undefined` is also treated as `null`, so `field === null` and
|
|
10
|
+
* `field === undefined` both test whether the field has a value.
|
|
11
|
+
*
|
|
12
|
+
* The result is coerced to a boolean, so non-boolean expressions are treated as truthy or falsy.
|
|
13
|
+
*
|
|
14
|
+
* @throws If the expression cannot be parsed or evaluated
|
|
15
|
+
*/
|
|
1
16
|
export function evaluatePostSubmissionExpression(expression: string, encounters: any[]): boolean {
|
|
2
17
|
const encounter = encounters[0];
|
|
3
|
-
const regx = /(?:\w+|'(?:\\'|[^'\n])*')/g;
|
|
4
|
-
let match;
|
|
5
|
-
const fieldIds = new Set<string>();
|
|
6
18
|
try {
|
|
7
|
-
|
|
8
|
-
const value = match[0].replace(/\\'/g, "'"); // Replace escaped single quotes
|
|
19
|
+
const ast = compile(expression);
|
|
9
20
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (
|
|
15
|
-
!(value.startsWith("'") && value.endsWith("'")) &&
|
|
16
|
-
typeof value === 'string' &&
|
|
17
|
-
!isBoolean &&
|
|
18
|
-
!isNumber &&
|
|
19
|
-
!isFloat
|
|
20
|
-
) {
|
|
21
|
-
fieldIds.add(value);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
let fieldToValueMap = {};
|
|
26
|
-
let replacedExpression;
|
|
27
|
-
if (fieldIds.size) {
|
|
28
|
-
fieldToValueMap = getFieldValues(fieldIds, encounter);
|
|
21
|
+
const variables: VariablesMap = {};
|
|
22
|
+
for (const name of extractVariableNames(ast)) {
|
|
23
|
+
variables[name] = getFieldValue(name, encounter);
|
|
29
24
|
}
|
|
25
|
+
variables.useFieldValue = (fieldId: string) => getFieldValue(fieldId, encounter);
|
|
26
|
+
variables.undefined = null;
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
replacedExpression = expression.replace(/(\w+)/g, (match) => {
|
|
33
|
-
return Object.prototype.hasOwnProperty.call(fieldToValueMap, match) ? fieldToValueMap[match] : match;
|
|
34
|
-
});
|
|
35
|
-
} else {
|
|
36
|
-
replacedExpression = expression;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return eval(replacedExpression);
|
|
28
|
+
return Boolean(evaluate(ast, variables));
|
|
40
29
|
} catch (error) {
|
|
41
|
-
throw new Error('Error evaluating expression');
|
|
30
|
+
throw new Error('Error evaluating expression', { cause: error });
|
|
42
31
|
}
|
|
43
32
|
}
|
|
44
33
|
|
|
45
|
-
function
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
let value = encounter.obs?.find((item) => item.formFieldPath.includes(fieldId))?.value;
|
|
49
|
-
if (typeof value === 'object') {
|
|
50
|
-
value = value.uuid;
|
|
51
|
-
}
|
|
52
|
-
if (value) {
|
|
53
|
-
value = formatValue(value);
|
|
54
|
-
}
|
|
55
|
-
result[fieldId] = value;
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
//This function wraps string values in single quotes which Javascript will evaluate
|
|
62
|
-
function formatValue(value: any): any {
|
|
63
|
-
if (typeof value === 'string') {
|
|
64
|
-
if (value.length >= 2 && value[0] === "'" && value[value.length - 1] === "'") {
|
|
65
|
-
return value;
|
|
66
|
-
} else {
|
|
67
|
-
return `'${value}'`;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return value;
|
|
34
|
+
function getFieldValue(fieldId: string, encounter: any) {
|
|
35
|
+
const value = encounter.obs?.find((item) => item.formFieldPath.includes(fieldId))?.value;
|
|
36
|
+
return (typeof value === 'object' ? value?.uuid : value) ?? null;
|
|
71
37
|
}
|