@openmrs/esm-form-engine-lib 2.1.0-pre.1362
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/.editorconfig +12 -0
- package/.eslintignore +2 -0
- package/.eslintrc +58 -0
- package/.husky/pre-commit +6 -0
- package/.husky/pre-push +6 -0
- package/.prettierignore +4 -0
- package/LICENSE.txt +401 -0
- package/README.md +136 -0
- package/__mocks__/concepts.mock.json +140 -0
- package/__mocks__/forms/afe-forms/component_art.json +38 -0
- package/__mocks__/forms/afe-forms/component_preclinic-review.json +38 -0
- package/__mocks__/forms/afe-forms/demo_hts-form.json +62 -0
- package/__mocks__/forms/afe-forms/form-component.json +38 -0
- package/__mocks__/forms/afe-forms/mini-form.json +31 -0
- package/__mocks__/forms/afe-forms/nested-form1.json +38 -0
- package/__mocks__/forms/afe-forms/nested-form2.json +38 -0
- package/__mocks__/forms/afe-forms/test-orders.json +72 -0
- package/__mocks__/forms/afe-forms/test-schema-transformer-form.json +88 -0
- package/__mocks__/forms/rfe-forms/age-validation-form.json +58 -0
- package/__mocks__/forms/rfe-forms/bmi-test-form.json +69 -0
- package/__mocks__/forms/rfe-forms/bsa-test-form.json +69 -0
- package/__mocks__/forms/rfe-forms/component_art.json +1705 -0
- package/__mocks__/forms/rfe-forms/component_preclinic-review.json +480 -0
- package/__mocks__/forms/rfe-forms/conditional-answered-form.json +97 -0
- package/__mocks__/forms/rfe-forms/conditional-required-form.json +281 -0
- package/__mocks__/forms/rfe-forms/demo_hts-form.json +346 -0
- package/__mocks__/forms/rfe-forms/edd-test-form.json +88 -0
- package/__mocks__/forms/rfe-forms/external_data_source_form.json +43 -0
- package/__mocks__/forms/rfe-forms/filter-answer-options-test-form.json +87 -0
- package/__mocks__/forms/rfe-forms/form-component.json +43 -0
- package/__mocks__/forms/rfe-forms/forms-loader.test.schema.ts +209 -0
- package/__mocks__/forms/rfe-forms/historical-expressions-form.json +170 -0
- package/__mocks__/forms/rfe-forms/labour_and_delivery_test_form.json +374 -0
- package/__mocks__/forms/rfe-forms/mini-form.json +29 -0
- package/__mocks__/forms/rfe-forms/mockHistoricalvisitsEncounter.json +89 -0
- package/__mocks__/forms/rfe-forms/months-on-art-form.json +90 -0
- package/__mocks__/forms/rfe-forms/multi-select-form.json +86 -0
- package/__mocks__/forms/rfe-forms/nested-form1.json +43 -0
- package/__mocks__/forms/rfe-forms/nested-form2.json +43 -0
- package/__mocks__/forms/rfe-forms/next-visit-test-form.json +78 -0
- package/__mocks__/forms/rfe-forms/obs-group-test_form.json +137 -0
- package/__mocks__/forms/rfe-forms/obs-list-data.ts +37 -0
- package/__mocks__/forms/rfe-forms/post-submission-test-form.json +116 -0
- package/__mocks__/forms/rfe-forms/reference-by-mapping-form.json +54 -0
- package/__mocks__/forms/rfe-forms/required-form.json +50 -0
- package/__mocks__/forms/rfe-forms/sample_fields.json +36 -0
- package/__mocks__/forms/rfe-forms/test-enrolment-form.json +241 -0
- package/__mocks__/forms/rfe-forms/treatment-end-date-test-form.json +121 -0
- package/__mocks__/forms/rfe-forms/viral-load-status-form.json +75 -0
- package/__mocks__/forms/rfe-forms/zscore-bmi-for-age-form.json +79 -0
- package/__mocks__/forms/rfe-forms/zscore-height-for-age-form.json +79 -0
- package/__mocks__/forms/rfe-forms/zscore-weight-height-form.json +77 -0
- package/__mocks__/packages/hiv/forms/hts_poc/1.0.json +8 -0
- package/__mocks__/packages/hiv/forms/hts_poc/1.1.json +91 -0
- package/__mocks__/packages/test-forms-registry.ts +12 -0
- package/__mocks__/patient.mock.ts +173 -0
- package/__mocks__/react-i18next.js +49 -0
- package/__mocks__/react-markdown.tsx +5 -0
- package/__mocks__/session.mock.ts +117 -0
- package/__mocks__/single-spa-react.js +11 -0
- package/__mocks__/use-initial-values/encounter.mock.json +963 -0
- package/__mocks__/use-initial-values/patient.mock.json +73 -0
- package/__mocks__/visit.mock.ts +19 -0
- package/dist/openmrs-esm-form-engine-lib.js +1 -0
- package/jest.config.js +30 -0
- package/package.json +104 -0
- package/prettier.config.js +8 -0
- package/readme/form-engine.jpeg +0 -0
- package/src/adapters/control-adapter.ts +29 -0
- package/src/adapters/encounter-datetime-adapter.ts +38 -0
- package/src/adapters/encounter-location-adapter.ts +39 -0
- package/src/adapters/encounter-provider-adapter.ts +48 -0
- package/src/adapters/encounter-role-adapter.ts +54 -0
- package/src/adapters/inline-date-adapter.ts +58 -0
- package/src/adapters/obs-adapter.ts +280 -0
- package/src/adapters/obs-comment-adapter.ts +60 -0
- package/src/adapters/orders-adapter.ts +75 -0
- package/src/adapters/patient-identifier-adapter.ts +40 -0
- package/src/adapters/program-state-adapter.ts +52 -0
- package/src/api/index.ts +178 -0
- package/src/components/error/error-modal.component.tsx +37 -0
- package/src/components/error/error.scss +4 -0
- package/src/components/extension/extension-parcel.component.tsx +32 -0
- package/src/components/field-label/field-label.component.tsx +32 -0
- package/src/components/field-label/field-label.scss +11 -0
- package/src/components/group/obs-group.component.tsx +29 -0
- package/src/components/group/obs-group.scss +12 -0
- package/src/components/inputs/content-switcher/content-switcher.component.tsx +71 -0
- package/src/components/inputs/content-switcher/content-switcher.scss +55 -0
- package/src/components/inputs/date/date.component.tsx +149 -0
- package/src/components/inputs/date/date.scss +36 -0
- package/src/components/inputs/file/camera/camera.component.tsx +34 -0
- package/src/components/inputs/file/camera/camera.scss +3 -0
- package/src/components/inputs/file/file.component.tsx +159 -0
- package/src/components/inputs/file/file.scss +101 -0
- package/src/components/inputs/fixed-value/fixed-value.component.tsx +19 -0
- package/src/components/inputs/markdown/markdown-wrapper.component.tsx +14 -0
- package/src/components/inputs/markdown/markdown.component.tsx +8 -0
- package/src/components/inputs/multi-select/multi-select.component.tsx +151 -0
- package/src/components/inputs/multi-select/multi-select.scss +25 -0
- package/src/components/inputs/multi-select/multi-select.test.tsx +90 -0
- package/src/components/inputs/number/number.component.tsx +69 -0
- package/src/components/inputs/number/number.scss +15 -0
- package/src/components/inputs/radio/radio.component.tsx +79 -0
- package/src/components/inputs/radio/radio.scss +36 -0
- package/src/components/inputs/select/dropdown.component.tsx +73 -0
- package/src/components/inputs/select/dropdown.scss +11 -0
- package/src/components/inputs/select/dropdown.test.tsx +120 -0
- package/src/components/inputs/text/text.component.tsx +65 -0
- package/src/components/inputs/text/text.scss +15 -0
- package/src/components/inputs/text/text.test.tsx +104 -0
- package/src/components/inputs/text-area/text-area.component.tsx +63 -0
- package/src/components/inputs/text-area/text-area.scss +11 -0
- package/src/components/inputs/toggle/toggle.component.tsx +66 -0
- package/src/components/inputs/toggle/toggle.scss +12 -0
- package/src/components/inputs/tooltip/tooltip.component.tsx +23 -0
- package/src/components/inputs/tooltip/tooltip.scss +8 -0
- package/src/components/inputs/ui-select-extended/ui-select-extended.component.tsx +187 -0
- package/src/components/inputs/ui-select-extended/ui-select-extended.scss +15 -0
- package/src/components/inputs/ui-select-extended/ui-select-extended.test.tsx +211 -0
- package/src/components/inputs/unspecified/unspecified.component.tsx +74 -0
- package/src/components/inputs/unspecified/unspecified.scss +7 -0
- package/src/components/inputs/unspecified/unspecified.test.tsx +95 -0
- package/src/components/inputs/workspace-launcher/workspace-launcher.component.tsx +35 -0
- package/src/components/inputs/workspace-launcher/workspace-launcher.scss +15 -0
- package/src/components/label/label.component.tsx +20 -0
- package/src/components/label/label.scss +11 -0
- package/src/components/loaders/loader.component.tsx +16 -0
- package/src/components/loaders/loader.scss +20 -0
- package/src/components/patient-banner/patient-banner.component.tsx +20 -0
- package/src/components/patient-banner/patient-banner.scss +12 -0
- package/src/components/previous-value-review/previous-value-review.component.tsx +49 -0
- package/src/components/previous-value-review/previous-value-review.scss +36 -0
- package/src/components/processor-factory/form-processor-factory.component.tsx +127 -0
- package/src/components/renderer/custom-hooks-renderer.component.tsx +30 -0
- package/src/components/renderer/field/fieldLogic.ts +214 -0
- package/src/components/renderer/field/form-field-renderer.component.tsx +281 -0
- package/src/components/renderer/field/form-field-renderer.scss +5 -0
- package/src/components/renderer/form/form-renderer.component.tsx +89 -0
- package/src/components/renderer/form/state.ts +54 -0
- package/src/components/renderer/page/page.renderer.component.tsx +50 -0
- package/src/components/renderer/page/page.renderer.scss +36 -0
- package/src/components/renderer/section/section-renderer.component.tsx +21 -0
- package/src/components/renderer/section/section-renderer.scss +19 -0
- package/src/components/repeat/helpers.test.ts +29 -0
- package/src/components/repeat/helpers.ts +68 -0
- package/src/components/repeat/repeat-controls.component.tsx +38 -0
- package/src/components/repeat/repeat-controls.scss +7 -0
- package/src/components/repeat/repeat.component.tsx +201 -0
- package/src/components/repeat/repeat.scss +30 -0
- package/src/components/repeat/repeat.test.ts +29 -0
- package/src/components/sidebar/sidebar.component.tsx +134 -0
- package/src/components/sidebar/sidebar.scss +121 -0
- package/src/components/value/value.component.tsx +27 -0
- package/src/components/value/value.scss +17 -0
- package/src/components/value/view/field-value-view.component.tsx +33 -0
- package/src/components/value/view/field-value-view.scss +31 -0
- package/src/constants.ts +12 -0
- package/src/datasources/concept-data-source.ts +42 -0
- package/src/datasources/data-source.ts +23 -0
- package/src/datasources/encounter-role-datasource.ts +15 -0
- package/src/datasources/historical-data-source.ts +11 -0
- package/src/datasources/location-data-source.ts +27 -0
- package/src/datasources/provider-datasource.ts +15 -0
- package/src/datasources/select-concept-answers-datasource.ts +15 -0
- package/src/declarations.d.ts +4 -0
- package/src/external-function-context.tsx +8 -0
- package/src/form-context.tsx +42 -0
- package/src/form-engine.component.tsx +178 -0
- package/src/form-engine.scss +140 -0
- package/src/form-engine.test.tsx +817 -0
- package/src/globals.ts +1 -0
- package/src/hooks/useClobData.tsx +21 -0
- package/src/hooks/useConcepts.tsx +55 -0
- package/src/hooks/useDatasourceDependentValue.ts +16 -0
- package/src/hooks/useEncounter.tsx +32 -0
- package/src/hooks/useEncounterRole.tsx +15 -0
- package/src/hooks/useEvaluateFormFieldExpressions.ts +138 -0
- package/src/hooks/useFieldValidationResults.ts +18 -0
- package/src/hooks/useFormCollapse.tsx +36 -0
- package/src/hooks/useFormFieldValidators.ts +22 -0
- package/src/hooks/useFormFieldValueAdapters.ts +24 -0
- package/src/hooks/useFormFields.ts +37 -0
- package/src/hooks/useFormFieldsMeta.ts +48 -0
- package/src/hooks/useFormJson.test.tsx +173 -0
- package/src/hooks/useFormJson.tsx +237 -0
- package/src/hooks/useFormStateHelpers.ts +50 -0
- package/src/hooks/useFormsConfig.tsx +27 -0
- package/src/hooks/useInitialValues.ts +38 -0
- package/src/hooks/usePatientData.tsx +32 -0
- package/src/hooks/usePatientPrograms.ts +32 -0
- package/src/hooks/usePostSubmissionActions.test.tsx +42 -0
- package/src/hooks/usePostSubmissionActions.ts +31 -0
- package/src/hooks/useProcessorDependencies.ts +30 -0
- package/src/hooks/useRestMaxResultsCount.ts +5 -0
- package/src/hooks/useSystemSetting.ts +36 -0
- package/src/hooks/useWorkspaceLayout.ts +29 -0
- package/src/index.ts +12 -0
- package/src/lifecycle.ts +33 -0
- package/src/post-submission-actions/program-enrollment-action.ts +138 -0
- package/src/processors/encounter/encounter-form-processor.ts +337 -0
- package/src/processors/encounter/encounter-processor-helper.ts +320 -0
- package/src/processors/form-processor.ts +41 -0
- package/src/provider/form-factory-helper.ts +100 -0
- package/src/provider/form-factory-provider.tsx +169 -0
- package/src/provider/form-provider.tsx +37 -0
- package/src/registry/inbuilt-components/InbuiltPostSubmissionActions.ts +9 -0
- package/src/registry/inbuilt-components/control-templates.ts +57 -0
- package/src/registry/inbuilt-components/inbuiltControls.ts +99 -0
- package/src/registry/inbuilt-components/inbuiltDataSources.ts +41 -0
- package/src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts +64 -0
- package/src/registry/inbuilt-components/inbuiltTransformers.ts +10 -0
- package/src/registry/inbuilt-components/inbuiltValidators.ts +33 -0
- package/src/registry/inbuilt-components/template-component-map.ts +28 -0
- package/src/registry/registry.test.ts +20 -0
- package/src/registry/registry.ts +261 -0
- package/src/routes.json +1 -0
- package/src/setupI18n.ts +16 -0
- package/src/setupTests.ts +5 -0
- package/src/transformers/default-schema-transformer.test.ts +155 -0
- package/src/transformers/default-schema-transformer.ts +239 -0
- package/src/types/domain.ts +129 -0
- package/src/types/index.ts +130 -0
- package/src/types/schema.ts +238 -0
- package/src/typings.d.ts +9 -0
- package/src/utils/boolean-utils.ts +25 -0
- package/src/utils/common-expression-helpers.ts +503 -0
- package/src/utils/common-utils.test.ts +136 -0
- package/src/utils/common-utils.ts +55 -0
- package/src/utils/error-utils.ts +37 -0
- package/src/utils/expression-parser.test.ts +308 -0
- package/src/utils/expression-parser.ts +158 -0
- package/src/utils/expression-runner.test.ts +387 -0
- package/src/utils/expression-runner.ts +219 -0
- package/src/utils/form-helper.test.ts +482 -0
- package/src/utils/form-helper.ts +210 -0
- package/src/utils/form-page-utils.ts +13 -0
- package/src/utils/forms-loader.test.ts +323 -0
- package/src/utils/forms-loader.ts +306 -0
- package/src/utils/post-submission-action-helper.ts +71 -0
- package/src/utils/test-utils.ts +54 -0
- package/src/utils/zscore-service.ts +59 -0
- package/src/validators/conditional-answered-validator.test.ts +61 -0
- package/src/validators/conditional-answered-validator.ts +17 -0
- package/src/validators/date-validator.test.ts +46 -0
- package/src/validators/date-validator.ts +19 -0
- package/src/validators/default-value-validator.test.ts +90 -0
- package/src/validators/default-value-validator.ts +36 -0
- package/src/validators/form-validator.test.ts +188 -0
- package/src/validators/form-validator.ts +95 -0
- package/src/validators/js-expression-validator.test.ts +118 -0
- package/src/validators/js-expression-validator.ts +44 -0
- package/src/validators/schema.ts +34 -0
- package/src/zscore/bfa_boys_5_above.json +2522 -0
- package/src/zscore/bfa_girls_5_above.json +2522 -0
- package/src/zscore/hfa_boys_5_above.json +2186 -0
- package/src/zscore/hfa_boys_below5.json +22286 -0
- package/src/zscore/hfa_girls_5_above.json +2186 -0
- package/src/zscore/hfa_girls_below5.json +22286 -0
- package/src/zscore/wfl_boys_below5.json +7814 -0
- package/src/zscore/wfl_girls_below5.json +7814 -0
- package/src/zscore-tests/bmi-age.test.tsx +88 -0
- package/src/zscore-tests/height-age.test.tsx +96 -0
- package/src/zscore-tests/weight-height.test.tsx +87 -0
- package/tools/i18next-parser.config.js +93 -0
- package/translations/en.json +47 -0
- package/translations/es.json +38 -0
- package/translations/fr.json +38 -0
- package/translations/km.json +38 -0
- package/tsconfig.json +19 -0
- package/turbo.json +15 -0
- package/webpack.config.js +1 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
.loaderContainer {
|
2
|
+
height: var(--desktop-workspace-window-height);
|
3
|
+
display: flex;
|
4
|
+
justify-content: center;
|
5
|
+
align-items: center;
|
6
|
+
}
|
7
|
+
|
8
|
+
// tablet
|
9
|
+
:global(.omrs-breakpoint-lt-desktop) {
|
10
|
+
.loaderContainer {
|
11
|
+
height: var(--tablet-workspace-window-height);
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
.loader {
|
16
|
+
display: flex;
|
17
|
+
justify-content: center;
|
18
|
+
align-items: center;
|
19
|
+
min-height: 50%;
|
20
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { ExtensionSlot } from '@openmrs/esm-framework';
|
3
|
+
import styles from './patient-banner.scss';
|
4
|
+
|
5
|
+
const PatientBanner: React.FC<{ patient: any; hideActionsOverflow?: any }> = ({ patient, hideActionsOverflow }) => {
|
6
|
+
return (
|
7
|
+
<div className={styles.patientBannerContainer}>
|
8
|
+
<ExtensionSlot
|
9
|
+
name="patient-header-slot"
|
10
|
+
state={{
|
11
|
+
patient,
|
12
|
+
patientUuid: patient.id,
|
13
|
+
hideActionsOverflow,
|
14
|
+
}}
|
15
|
+
/>
|
16
|
+
</div>
|
17
|
+
);
|
18
|
+
};
|
19
|
+
|
20
|
+
export default PatientBanner;
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { useTranslation } from 'react-i18next';
|
3
|
+
import { ValueDisplay } from '../value/value.component';
|
4
|
+
import styles from './previous-value-review.scss';
|
5
|
+
import { type FormField } from '../../types';
|
6
|
+
import { useFormProviderContext } from '../../provider/form-provider';
|
7
|
+
|
8
|
+
type PreviousValueReviewProps = {
|
9
|
+
previousValue: any;
|
10
|
+
displayText: string;
|
11
|
+
field: FormField;
|
12
|
+
hideHeader?: boolean;
|
13
|
+
onAfterChange: (value) => void;
|
14
|
+
};
|
15
|
+
|
16
|
+
const PreviousValueReview: React.FC<PreviousValueReviewProps> = ({
|
17
|
+
previousValue,
|
18
|
+
displayText,
|
19
|
+
field,
|
20
|
+
hideHeader,
|
21
|
+
onAfterChange,
|
22
|
+
}) => {
|
23
|
+
const { t } = useTranslation();
|
24
|
+
const {
|
25
|
+
methods: { setValue },
|
26
|
+
} = useFormProviderContext();
|
27
|
+
|
28
|
+
const onReuseValue = (e) => {
|
29
|
+
e.preventDefault();
|
30
|
+
setValue(field.id, previousValue);
|
31
|
+
onAfterChange(previousValue);
|
32
|
+
};
|
33
|
+
|
34
|
+
return (
|
35
|
+
<div className={styles.formField}>
|
36
|
+
<div className={styles.row}>
|
37
|
+
{!hideHeader && <div>{t('previousValue', 'Previous value:')}</div>}
|
38
|
+
<div className={styles.value}>
|
39
|
+
<ValueDisplay value={displayText} />
|
40
|
+
</div>
|
41
|
+
</div>
|
42
|
+
<div className={styles.reuseButton} role="button" tabIndex={0} onClick={onReuseValue}>
|
43
|
+
{t('reuseValue', 'Reuse value')}
|
44
|
+
</div>
|
45
|
+
</div>
|
46
|
+
);
|
47
|
+
};
|
48
|
+
|
49
|
+
export default PreviousValueReview;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
@use '@carbon/colors';
|
2
|
+
|
3
|
+
.formField {
|
4
|
+
display: flex;
|
5
|
+
margin-top: 0.25rem;
|
6
|
+
margin-bottom: 0.25rem;
|
7
|
+
align-items: center;
|
8
|
+
|
9
|
+
> div > div > label {
|
10
|
+
color: colors.$gray-70;
|
11
|
+
}
|
12
|
+
}
|
13
|
+
|
14
|
+
.row {
|
15
|
+
display: flex;
|
16
|
+
justify-content: flex-start;
|
17
|
+
align-items: center;
|
18
|
+
color: colors.$gray-60;
|
19
|
+
font-size: 0.8rem;
|
20
|
+
}
|
21
|
+
|
22
|
+
.value {
|
23
|
+
display: flex;
|
24
|
+
align-items: baseline;
|
25
|
+
padding-left: 0.5rem;
|
26
|
+
}
|
27
|
+
|
28
|
+
.reuseButton {
|
29
|
+
font-size: 0.8rem;
|
30
|
+
margin: 0 1rem;
|
31
|
+
color: colors.$blue-60;
|
32
|
+
}
|
33
|
+
|
34
|
+
.reuseButton:hover {
|
35
|
+
cursor: pointer;
|
36
|
+
}
|
@@ -0,0 +1,127 @@
|
|
1
|
+
import React, { useEffect, useMemo, useState } from 'react';
|
2
|
+
import useProcessorDependencies from '../../hooks/useProcessorDependencies';
|
3
|
+
import useInitialValues from '../../hooks/useInitialValues';
|
4
|
+
import { FormRenderer } from '../renderer/form/form-renderer.component';
|
5
|
+
import { type FormSchema, type FormProcessorContextProps } from '../../types';
|
6
|
+
import { CustomHooksRenderer } from '../renderer/custom-hooks-renderer.component';
|
7
|
+
import { useFormFields } from '../../hooks/useFormFields';
|
8
|
+
import { useConcepts } from '../../hooks/useConcepts';
|
9
|
+
import { useFormFieldValidators } from '../../hooks/useFormFieldValidators';
|
10
|
+
import { useFormFieldsMeta } from '../../hooks/useFormFieldsMeta';
|
11
|
+
import { useFormFactory } from '../../provider/form-factory-provider';
|
12
|
+
import { useFormFieldValueAdapters } from '../../hooks/useFormFieldValueAdapters';
|
13
|
+
import { EncounterFormProcessor } from '../../processors/encounter/encounter-form-processor';
|
14
|
+
import { reportError } from '../../utils/error-utils';
|
15
|
+
import { useTranslation } from 'react-i18next';
|
16
|
+
import Loader from '../loaders/loader.component';
|
17
|
+
import { registerFormFieldAdaptersForCleanUp } from '../../lifecycle';
|
18
|
+
|
19
|
+
interface FormProcessorFactoryProps {
|
20
|
+
formJson: FormSchema;
|
21
|
+
isSubForm?: boolean;
|
22
|
+
setIsLoadingFormDependencies: (isLoading: boolean) => void;
|
23
|
+
}
|
24
|
+
|
25
|
+
const FormProcessorFactory = ({
|
26
|
+
formJson,
|
27
|
+
isSubForm = false,
|
28
|
+
setIsLoadingFormDependencies,
|
29
|
+
}: FormProcessorFactoryProps) => {
|
30
|
+
const { patient, sessionMode, formProcessors, layoutType, location, provider, sessionDate, visit } = useFormFactory();
|
31
|
+
|
32
|
+
const processor = useMemo(() => {
|
33
|
+
const ProcessorClass = formProcessors[formJson.processor];
|
34
|
+
if (processor) {
|
35
|
+
return processor;
|
36
|
+
}
|
37
|
+
if (ProcessorClass) {
|
38
|
+
return new ProcessorClass(formJson);
|
39
|
+
}
|
40
|
+
console.error(`Form processor ${formJson.processor} not found, defaulting to EncounterFormProcessor`);
|
41
|
+
return new EncounterFormProcessor(formJson);
|
42
|
+
}, [formProcessors, formJson.processor]);
|
43
|
+
|
44
|
+
const [processorContext, setProcessorContext] = useState<FormProcessorContextProps>({
|
45
|
+
patient,
|
46
|
+
formJson,
|
47
|
+
sessionMode,
|
48
|
+
layoutType,
|
49
|
+
location,
|
50
|
+
currentProvider: provider,
|
51
|
+
processor,
|
52
|
+
sessionDate,
|
53
|
+
visit,
|
54
|
+
formFields: [],
|
55
|
+
formFieldAdapters: {},
|
56
|
+
formFieldValidators: {},
|
57
|
+
});
|
58
|
+
const { t } = useTranslation();
|
59
|
+
const { formFields: rawFormFields, conceptReferences } = useFormFields(formJson);
|
60
|
+
const { concepts: formFieldsConcepts, isLoading: isLoadingConcepts } = useConcepts(conceptReferences);
|
61
|
+
const formFieldsWithMeta = useFormFieldsMeta(rawFormFields, formFieldsConcepts);
|
62
|
+
const formFieldAdapters = useFormFieldValueAdapters(rawFormFields);
|
63
|
+
const formFieldValidators = useFormFieldValidators(rawFormFields);
|
64
|
+
const { isLoading: isLoadingCustomDeps } = useProcessorDependencies(processor, processorContext, setProcessorContext);
|
65
|
+
const useCustomHooks = processor.getCustomHooks().useCustomHooks;
|
66
|
+
const [isLoadingCustomHooks, setIsLoadingCustomHooks] = useState(!!useCustomHooks);
|
67
|
+
const [isLoadingProcessorDependencies, setIsLoadingProcessorDependencies] = useState(true);
|
68
|
+
|
69
|
+
const {
|
70
|
+
isLoadingInitialValues,
|
71
|
+
initialValues,
|
72
|
+
error: initialValuesError,
|
73
|
+
} = useInitialValues(processor, isLoadingCustomDeps || isLoadingCustomHooks || isLoadingConcepts, processorContext);
|
74
|
+
|
75
|
+
useEffect(() => {
|
76
|
+
const isLoading = isLoadingCustomDeps || isLoadingCustomHooks || isLoadingConcepts || isLoadingInitialValues;
|
77
|
+
setIsLoadingFormDependencies(isLoading);
|
78
|
+
setIsLoadingProcessorDependencies(isLoading);
|
79
|
+
}, [isLoadingCustomDeps, isLoadingCustomHooks, isLoadingConcepts, isLoadingInitialValues]);
|
80
|
+
|
81
|
+
useEffect(() => {
|
82
|
+
setProcessorContext((prev) => ({
|
83
|
+
...prev,
|
84
|
+
...(formFieldAdapters && { formFieldAdapters }),
|
85
|
+
...(formFieldValidators && { formFieldValidators }),
|
86
|
+
...(formFieldsWithMeta?.length
|
87
|
+
? { formFields: formFieldsWithMeta }
|
88
|
+
: rawFormFields?.length
|
89
|
+
? { formFields: rawFormFields }
|
90
|
+
: {}),
|
91
|
+
}));
|
92
|
+
}, [formFieldAdapters, formFieldValidators, rawFormFields, formFieldsWithMeta]);
|
93
|
+
|
94
|
+
useEffect(() => {
|
95
|
+
reportError(initialValuesError, t('errorLoadingInitialValues', 'Error loading initial values'));
|
96
|
+
}, [initialValuesError]);
|
97
|
+
|
98
|
+
useEffect(() => {
|
99
|
+
if (formFieldAdapters) {
|
100
|
+
registerFormFieldAdaptersForCleanUp(formFieldAdapters);
|
101
|
+
}
|
102
|
+
}, [formFieldAdapters]);
|
103
|
+
|
104
|
+
return (
|
105
|
+
<>
|
106
|
+
{useCustomHooks && (
|
107
|
+
<CustomHooksRenderer
|
108
|
+
context={processorContext}
|
109
|
+
setContext={setProcessorContext}
|
110
|
+
useCustomHooks={useCustomHooks}
|
111
|
+
setIsLoadingCustomHooks={setIsLoadingCustomHooks}
|
112
|
+
/>
|
113
|
+
)}
|
114
|
+
{isLoadingProcessorDependencies && !isSubForm ? (
|
115
|
+
<Loader />
|
116
|
+
) : (
|
117
|
+
<FormRenderer
|
118
|
+
processorContext={processorContext}
|
119
|
+
initialValues={initialValues}
|
120
|
+
setIsLoadingFormDependencies={setIsLoadingFormDependencies}
|
121
|
+
/>
|
122
|
+
)}
|
123
|
+
</>
|
124
|
+
);
|
125
|
+
};
|
126
|
+
|
127
|
+
export default FormProcessorFactory;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { useEffect } from 'react';
|
2
|
+
import { type FormProcessorContextProps } from '../../types';
|
3
|
+
|
4
|
+
export const CustomHooksRenderer = ({
|
5
|
+
context,
|
6
|
+
setContext,
|
7
|
+
useCustomHooks,
|
8
|
+
setIsLoadingCustomHooks,
|
9
|
+
}: {
|
10
|
+
context: FormProcessorContextProps;
|
11
|
+
setContext: (context: FormProcessorContextProps) => void;
|
12
|
+
useCustomHooks: (context: FormProcessorContextProps) => {
|
13
|
+
data: any;
|
14
|
+
isLoading: boolean;
|
15
|
+
error: any;
|
16
|
+
updateContext: (setContext: (context: FormProcessorContextProps) => void) => void;
|
17
|
+
};
|
18
|
+
setIsLoadingCustomHooks: (isLoading: boolean) => void;
|
19
|
+
}) => {
|
20
|
+
const { isLoading = false, error = null, data, updateContext } = useCustomHooks(context);
|
21
|
+
|
22
|
+
useEffect(() => {
|
23
|
+
if (!isLoading && updateContext) {
|
24
|
+
updateContext(setContext);
|
25
|
+
setIsLoadingCustomHooks(false);
|
26
|
+
}
|
27
|
+
}, [isLoading]);
|
28
|
+
|
29
|
+
return null;
|
30
|
+
};
|
@@ -0,0 +1,214 @@
|
|
1
|
+
import { codedTypes } from '../../../constants';
|
2
|
+
import { type FormContextProps } from '../../../provider/form-provider';
|
3
|
+
import { type FormField } from '../../../types';
|
4
|
+
import { isTrue } from '../../../utils/boolean-utils';
|
5
|
+
import { hasRendering } from '../../../utils/common-utils';
|
6
|
+
import { evaluateAsyncExpression, evaluateExpression } from '../../../utils/expression-runner';
|
7
|
+
import { evalConditionalRequired, evaluateDisabled, evaluateHide } from '../../../utils/form-helper';
|
8
|
+
import { isEmpty } from '../../../validators/form-validator';
|
9
|
+
|
10
|
+
export function handleFieldLogic(field: FormField, context: FormContextProps) {
|
11
|
+
const {
|
12
|
+
methods: { getValues },
|
13
|
+
} = context;
|
14
|
+
const values = getValues();
|
15
|
+
if (codedTypes.includes(field.questionOptions.rendering)) {
|
16
|
+
evaluateFieldAnswerDisabled(field, values, context);
|
17
|
+
}
|
18
|
+
evaluateFieldDependents(field, values, context);
|
19
|
+
}
|
20
|
+
|
21
|
+
function evaluateFieldAnswerDisabled(field: FormField, values: Record<string, any>, context: FormContextProps) {
|
22
|
+
const { sessionMode, formFields, patient } = context;
|
23
|
+
field.questionOptions.answers.forEach((answer) => {
|
24
|
+
const disableExpression = answer.disable?.disableWhenExpression;
|
25
|
+
if (disableExpression && disableExpression.includes('myValue')) {
|
26
|
+
answer.disable.isDisabled = evaluateExpression(
|
27
|
+
answer.disable?.disableWhenExpression,
|
28
|
+
{ value: field, type: 'field' },
|
29
|
+
formFields,
|
30
|
+
values,
|
31
|
+
{
|
32
|
+
mode: sessionMode,
|
33
|
+
patient,
|
34
|
+
},
|
35
|
+
);
|
36
|
+
}
|
37
|
+
});
|
38
|
+
}
|
39
|
+
|
40
|
+
function evaluateFieldDependents(field: FormField, values: any, context: FormContextProps) {
|
41
|
+
const {
|
42
|
+
sessionMode,
|
43
|
+
formFields,
|
44
|
+
patient,
|
45
|
+
formFieldValidators,
|
46
|
+
formJson,
|
47
|
+
methods: { setValue },
|
48
|
+
updateFormField,
|
49
|
+
setForm,
|
50
|
+
} = context;
|
51
|
+
// handle fields
|
52
|
+
if (field.fieldDependents) {
|
53
|
+
field.fieldDependents.forEach((dep) => {
|
54
|
+
const dependent = formFields.find((f) => f.id == dep);
|
55
|
+
// evaluate calculated value
|
56
|
+
if (dependent.questionOptions.calculate?.calculateExpression) {
|
57
|
+
evaluateAsyncExpression(
|
58
|
+
dependent.questionOptions.calculate.calculateExpression,
|
59
|
+
{ value: dependent, type: 'field' },
|
60
|
+
formFields,
|
61
|
+
values,
|
62
|
+
{
|
63
|
+
mode: sessionMode,
|
64
|
+
patient,
|
65
|
+
},
|
66
|
+
).then((result) => {
|
67
|
+
setValue(dependent.id, result);
|
68
|
+
});
|
69
|
+
}
|
70
|
+
// evaluate hide
|
71
|
+
if (dependent.hide) {
|
72
|
+
evaluateHide({ value: dependent, type: 'field' }, formFields, values, sessionMode, patient, evaluateExpression);
|
73
|
+
}
|
74
|
+
// evaluate disabled
|
75
|
+
if (typeof dependent.disabled === 'object' && dependent.disabled.disableWhenExpression) {
|
76
|
+
dependent.isDisabled = evaluateDisabled(
|
77
|
+
{ value: dependent, type: 'field' },
|
78
|
+
formFields,
|
79
|
+
values,
|
80
|
+
sessionMode,
|
81
|
+
patient,
|
82
|
+
evaluateExpression,
|
83
|
+
);
|
84
|
+
}
|
85
|
+
// evaluate conditional required
|
86
|
+
if (typeof dependent.required === 'object' && dependent.required?.type === 'conditionalRequired') {
|
87
|
+
dependent.isRequired = evalConditionalRequired(dependent, formFields, values);
|
88
|
+
}
|
89
|
+
// evaluate conditional answered
|
90
|
+
if (dependent.validators?.some((validator) => validator.type === 'conditionalAnswered')) {
|
91
|
+
const fieldValidatorConfig = dependent.validators?.find(
|
92
|
+
(validator) => validator.type === 'conditionalAnswered',
|
93
|
+
);
|
94
|
+
|
95
|
+
const validationResults = formFieldValidators['conditionalAnswered'].validate(
|
96
|
+
dependent,
|
97
|
+
dependent.meta.submission?.newValue,
|
98
|
+
{
|
99
|
+
...fieldValidatorConfig,
|
100
|
+
expressionContext: { patient, mode: sessionMode },
|
101
|
+
values,
|
102
|
+
formFields,
|
103
|
+
},
|
104
|
+
);
|
105
|
+
dependent.meta.submission = { ...dependent.meta.submission, errors: validationResults };
|
106
|
+
}
|
107
|
+
// evaluate hide for answers
|
108
|
+
dependent?.questionOptions.answers
|
109
|
+
?.filter((answer) => !isEmpty(answer.hide?.hideWhenExpression))
|
110
|
+
.forEach((answer) => {
|
111
|
+
answer.isHidden = evaluateExpression(
|
112
|
+
answer.hide?.hideWhenExpression,
|
113
|
+
{ value: dependent, type: 'field' },
|
114
|
+
formFields,
|
115
|
+
values,
|
116
|
+
{
|
117
|
+
mode: sessionMode,
|
118
|
+
patient,
|
119
|
+
},
|
120
|
+
);
|
121
|
+
});
|
122
|
+
// evaluate disabled
|
123
|
+
dependent?.questionOptions.answers
|
124
|
+
?.filter((answer) => !isEmpty(answer.disable?.isDisabled))
|
125
|
+
.forEach((answer) => {
|
126
|
+
answer.disable.isDisabled = evaluateExpression(
|
127
|
+
answer.disable?.disableWhenExpression,
|
128
|
+
{ value: dependent, type: 'field' },
|
129
|
+
formFields,
|
130
|
+
values,
|
131
|
+
{
|
132
|
+
mode: sessionMode,
|
133
|
+
patient,
|
134
|
+
},
|
135
|
+
);
|
136
|
+
});
|
137
|
+
// evaluate readonly
|
138
|
+
if (!dependent.isHidden && dependent.meta.readonlyExpression) {
|
139
|
+
dependent.readonly = evaluateExpression(
|
140
|
+
dependent.meta.readonlyExpression,
|
141
|
+
{ value: dependent, type: 'field' },
|
142
|
+
formFields,
|
143
|
+
values,
|
144
|
+
{
|
145
|
+
mode: sessionMode,
|
146
|
+
patient,
|
147
|
+
},
|
148
|
+
);
|
149
|
+
}
|
150
|
+
// evaluate repeat limit
|
151
|
+
if (hasRendering(dependent, 'repeating') && !isEmpty(dependent.questionOptions.repeatOptions?.limitExpression)) {
|
152
|
+
dependent.questionOptions.repeatOptions.limit = evaluateExpression(
|
153
|
+
dependent.questionOptions.repeatOptions?.limitExpression,
|
154
|
+
{ value: dependent, type: 'field' },
|
155
|
+
formFields,
|
156
|
+
values,
|
157
|
+
{
|
158
|
+
mode: sessionMode,
|
159
|
+
patient,
|
160
|
+
},
|
161
|
+
);
|
162
|
+
}
|
163
|
+
updateFormField(dependent);
|
164
|
+
});
|
165
|
+
}
|
166
|
+
|
167
|
+
let shouldUpdateForm = false;
|
168
|
+
|
169
|
+
// handle sections
|
170
|
+
if (field.sectionDependents) {
|
171
|
+
field.sectionDependents.forEach((sectionId) => {
|
172
|
+
for (let i = 0; i < formJson.pages.length; i++) {
|
173
|
+
const section = formJson.pages[i].sections.find((section) => section.label == sectionId);
|
174
|
+
if (section) {
|
175
|
+
evaluateHide(
|
176
|
+
{ value: section, type: 'section' },
|
177
|
+
formFields,
|
178
|
+
values,
|
179
|
+
sessionMode,
|
180
|
+
patient,
|
181
|
+
evaluateExpression,
|
182
|
+
);
|
183
|
+
if (isTrue(section.isHidden)) {
|
184
|
+
section.questions.forEach((field) => {
|
185
|
+
field.isParentHidden = true;
|
186
|
+
});
|
187
|
+
}
|
188
|
+
shouldUpdateForm = true;
|
189
|
+
break;
|
190
|
+
}
|
191
|
+
}
|
192
|
+
});
|
193
|
+
}
|
194
|
+
|
195
|
+
// handle pages
|
196
|
+
if (field.pageDependents) {
|
197
|
+
field.pageDependents?.forEach((dep) => {
|
198
|
+
const dependent = formJson.pages.find((f) => f.label == dep);
|
199
|
+
evaluateHide({ value: dependent, type: 'page' }, formFields, values, sessionMode, patient, evaluateExpression);
|
200
|
+
if (isTrue(dependent.isHidden)) {
|
201
|
+
dependent.sections.forEach((section) => {
|
202
|
+
section.questions.forEach((field) => {
|
203
|
+
field.isParentHidden = true;
|
204
|
+
});
|
205
|
+
});
|
206
|
+
}
|
207
|
+
shouldUpdateForm = true;
|
208
|
+
});
|
209
|
+
|
210
|
+
if (shouldUpdateForm) {
|
211
|
+
setForm({ ...formJson });
|
212
|
+
}
|
213
|
+
}
|
214
|
+
}
|