@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.
Files changed (272) hide show
  1. package/.editorconfig +12 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc +58 -0
  4. package/.husky/pre-commit +6 -0
  5. package/.husky/pre-push +6 -0
  6. package/.prettierignore +4 -0
  7. package/LICENSE.txt +401 -0
  8. package/README.md +136 -0
  9. package/__mocks__/concepts.mock.json +140 -0
  10. package/__mocks__/forms/afe-forms/component_art.json +38 -0
  11. package/__mocks__/forms/afe-forms/component_preclinic-review.json +38 -0
  12. package/__mocks__/forms/afe-forms/demo_hts-form.json +62 -0
  13. package/__mocks__/forms/afe-forms/form-component.json +38 -0
  14. package/__mocks__/forms/afe-forms/mini-form.json +31 -0
  15. package/__mocks__/forms/afe-forms/nested-form1.json +38 -0
  16. package/__mocks__/forms/afe-forms/nested-form2.json +38 -0
  17. package/__mocks__/forms/afe-forms/test-orders.json +72 -0
  18. package/__mocks__/forms/afe-forms/test-schema-transformer-form.json +88 -0
  19. package/__mocks__/forms/rfe-forms/age-validation-form.json +58 -0
  20. package/__mocks__/forms/rfe-forms/bmi-test-form.json +69 -0
  21. package/__mocks__/forms/rfe-forms/bsa-test-form.json +69 -0
  22. package/__mocks__/forms/rfe-forms/component_art.json +1705 -0
  23. package/__mocks__/forms/rfe-forms/component_preclinic-review.json +480 -0
  24. package/__mocks__/forms/rfe-forms/conditional-answered-form.json +97 -0
  25. package/__mocks__/forms/rfe-forms/conditional-required-form.json +281 -0
  26. package/__mocks__/forms/rfe-forms/demo_hts-form.json +346 -0
  27. package/__mocks__/forms/rfe-forms/edd-test-form.json +88 -0
  28. package/__mocks__/forms/rfe-forms/external_data_source_form.json +43 -0
  29. package/__mocks__/forms/rfe-forms/filter-answer-options-test-form.json +87 -0
  30. package/__mocks__/forms/rfe-forms/form-component.json +43 -0
  31. package/__mocks__/forms/rfe-forms/forms-loader.test.schema.ts +209 -0
  32. package/__mocks__/forms/rfe-forms/historical-expressions-form.json +170 -0
  33. package/__mocks__/forms/rfe-forms/labour_and_delivery_test_form.json +374 -0
  34. package/__mocks__/forms/rfe-forms/mini-form.json +29 -0
  35. package/__mocks__/forms/rfe-forms/mockHistoricalvisitsEncounter.json +89 -0
  36. package/__mocks__/forms/rfe-forms/months-on-art-form.json +90 -0
  37. package/__mocks__/forms/rfe-forms/multi-select-form.json +86 -0
  38. package/__mocks__/forms/rfe-forms/nested-form1.json +43 -0
  39. package/__mocks__/forms/rfe-forms/nested-form2.json +43 -0
  40. package/__mocks__/forms/rfe-forms/next-visit-test-form.json +78 -0
  41. package/__mocks__/forms/rfe-forms/obs-group-test_form.json +137 -0
  42. package/__mocks__/forms/rfe-forms/obs-list-data.ts +37 -0
  43. package/__mocks__/forms/rfe-forms/post-submission-test-form.json +116 -0
  44. package/__mocks__/forms/rfe-forms/reference-by-mapping-form.json +54 -0
  45. package/__mocks__/forms/rfe-forms/required-form.json +50 -0
  46. package/__mocks__/forms/rfe-forms/sample_fields.json +36 -0
  47. package/__mocks__/forms/rfe-forms/test-enrolment-form.json +241 -0
  48. package/__mocks__/forms/rfe-forms/treatment-end-date-test-form.json +121 -0
  49. package/__mocks__/forms/rfe-forms/viral-load-status-form.json +75 -0
  50. package/__mocks__/forms/rfe-forms/zscore-bmi-for-age-form.json +79 -0
  51. package/__mocks__/forms/rfe-forms/zscore-height-for-age-form.json +79 -0
  52. package/__mocks__/forms/rfe-forms/zscore-weight-height-form.json +77 -0
  53. package/__mocks__/packages/hiv/forms/hts_poc/1.0.json +8 -0
  54. package/__mocks__/packages/hiv/forms/hts_poc/1.1.json +91 -0
  55. package/__mocks__/packages/test-forms-registry.ts +12 -0
  56. package/__mocks__/patient.mock.ts +173 -0
  57. package/__mocks__/react-i18next.js +49 -0
  58. package/__mocks__/react-markdown.tsx +5 -0
  59. package/__mocks__/session.mock.ts +117 -0
  60. package/__mocks__/single-spa-react.js +11 -0
  61. package/__mocks__/use-initial-values/encounter.mock.json +963 -0
  62. package/__mocks__/use-initial-values/patient.mock.json +73 -0
  63. package/__mocks__/visit.mock.ts +19 -0
  64. package/dist/openmrs-esm-form-engine-lib.js +1 -0
  65. package/jest.config.js +30 -0
  66. package/package.json +104 -0
  67. package/prettier.config.js +8 -0
  68. package/readme/form-engine.jpeg +0 -0
  69. package/src/adapters/control-adapter.ts +29 -0
  70. package/src/adapters/encounter-datetime-adapter.ts +38 -0
  71. package/src/adapters/encounter-location-adapter.ts +39 -0
  72. package/src/adapters/encounter-provider-adapter.ts +48 -0
  73. package/src/adapters/encounter-role-adapter.ts +54 -0
  74. package/src/adapters/inline-date-adapter.ts +58 -0
  75. package/src/adapters/obs-adapter.ts +280 -0
  76. package/src/adapters/obs-comment-adapter.ts +60 -0
  77. package/src/adapters/orders-adapter.ts +75 -0
  78. package/src/adapters/patient-identifier-adapter.ts +40 -0
  79. package/src/adapters/program-state-adapter.ts +52 -0
  80. package/src/api/index.ts +178 -0
  81. package/src/components/error/error-modal.component.tsx +37 -0
  82. package/src/components/error/error.scss +4 -0
  83. package/src/components/extension/extension-parcel.component.tsx +32 -0
  84. package/src/components/field-label/field-label.component.tsx +32 -0
  85. package/src/components/field-label/field-label.scss +11 -0
  86. package/src/components/group/obs-group.component.tsx +29 -0
  87. package/src/components/group/obs-group.scss +12 -0
  88. package/src/components/inputs/content-switcher/content-switcher.component.tsx +71 -0
  89. package/src/components/inputs/content-switcher/content-switcher.scss +55 -0
  90. package/src/components/inputs/date/date.component.tsx +149 -0
  91. package/src/components/inputs/date/date.scss +36 -0
  92. package/src/components/inputs/file/camera/camera.component.tsx +34 -0
  93. package/src/components/inputs/file/camera/camera.scss +3 -0
  94. package/src/components/inputs/file/file.component.tsx +159 -0
  95. package/src/components/inputs/file/file.scss +101 -0
  96. package/src/components/inputs/fixed-value/fixed-value.component.tsx +19 -0
  97. package/src/components/inputs/markdown/markdown-wrapper.component.tsx +14 -0
  98. package/src/components/inputs/markdown/markdown.component.tsx +8 -0
  99. package/src/components/inputs/multi-select/multi-select.component.tsx +151 -0
  100. package/src/components/inputs/multi-select/multi-select.scss +25 -0
  101. package/src/components/inputs/multi-select/multi-select.test.tsx +90 -0
  102. package/src/components/inputs/number/number.component.tsx +69 -0
  103. package/src/components/inputs/number/number.scss +15 -0
  104. package/src/components/inputs/radio/radio.component.tsx +79 -0
  105. package/src/components/inputs/radio/radio.scss +36 -0
  106. package/src/components/inputs/select/dropdown.component.tsx +73 -0
  107. package/src/components/inputs/select/dropdown.scss +11 -0
  108. package/src/components/inputs/select/dropdown.test.tsx +120 -0
  109. package/src/components/inputs/text/text.component.tsx +65 -0
  110. package/src/components/inputs/text/text.scss +15 -0
  111. package/src/components/inputs/text/text.test.tsx +104 -0
  112. package/src/components/inputs/text-area/text-area.component.tsx +63 -0
  113. package/src/components/inputs/text-area/text-area.scss +11 -0
  114. package/src/components/inputs/toggle/toggle.component.tsx +66 -0
  115. package/src/components/inputs/toggle/toggle.scss +12 -0
  116. package/src/components/inputs/tooltip/tooltip.component.tsx +23 -0
  117. package/src/components/inputs/tooltip/tooltip.scss +8 -0
  118. package/src/components/inputs/ui-select-extended/ui-select-extended.component.tsx +187 -0
  119. package/src/components/inputs/ui-select-extended/ui-select-extended.scss +15 -0
  120. package/src/components/inputs/ui-select-extended/ui-select-extended.test.tsx +211 -0
  121. package/src/components/inputs/unspecified/unspecified.component.tsx +74 -0
  122. package/src/components/inputs/unspecified/unspecified.scss +7 -0
  123. package/src/components/inputs/unspecified/unspecified.test.tsx +95 -0
  124. package/src/components/inputs/workspace-launcher/workspace-launcher.component.tsx +35 -0
  125. package/src/components/inputs/workspace-launcher/workspace-launcher.scss +15 -0
  126. package/src/components/label/label.component.tsx +20 -0
  127. package/src/components/label/label.scss +11 -0
  128. package/src/components/loaders/loader.component.tsx +16 -0
  129. package/src/components/loaders/loader.scss +20 -0
  130. package/src/components/patient-banner/patient-banner.component.tsx +20 -0
  131. package/src/components/patient-banner/patient-banner.scss +12 -0
  132. package/src/components/previous-value-review/previous-value-review.component.tsx +49 -0
  133. package/src/components/previous-value-review/previous-value-review.scss +36 -0
  134. package/src/components/processor-factory/form-processor-factory.component.tsx +127 -0
  135. package/src/components/renderer/custom-hooks-renderer.component.tsx +30 -0
  136. package/src/components/renderer/field/fieldLogic.ts +214 -0
  137. package/src/components/renderer/field/form-field-renderer.component.tsx +281 -0
  138. package/src/components/renderer/field/form-field-renderer.scss +5 -0
  139. package/src/components/renderer/form/form-renderer.component.tsx +89 -0
  140. package/src/components/renderer/form/state.ts +54 -0
  141. package/src/components/renderer/page/page.renderer.component.tsx +50 -0
  142. package/src/components/renderer/page/page.renderer.scss +36 -0
  143. package/src/components/renderer/section/section-renderer.component.tsx +21 -0
  144. package/src/components/renderer/section/section-renderer.scss +19 -0
  145. package/src/components/repeat/helpers.test.ts +29 -0
  146. package/src/components/repeat/helpers.ts +68 -0
  147. package/src/components/repeat/repeat-controls.component.tsx +38 -0
  148. package/src/components/repeat/repeat-controls.scss +7 -0
  149. package/src/components/repeat/repeat.component.tsx +201 -0
  150. package/src/components/repeat/repeat.scss +30 -0
  151. package/src/components/repeat/repeat.test.ts +29 -0
  152. package/src/components/sidebar/sidebar.component.tsx +134 -0
  153. package/src/components/sidebar/sidebar.scss +121 -0
  154. package/src/components/value/value.component.tsx +27 -0
  155. package/src/components/value/value.scss +17 -0
  156. package/src/components/value/view/field-value-view.component.tsx +33 -0
  157. package/src/components/value/view/field-value-view.scss +31 -0
  158. package/src/constants.ts +12 -0
  159. package/src/datasources/concept-data-source.ts +42 -0
  160. package/src/datasources/data-source.ts +23 -0
  161. package/src/datasources/encounter-role-datasource.ts +15 -0
  162. package/src/datasources/historical-data-source.ts +11 -0
  163. package/src/datasources/location-data-source.ts +27 -0
  164. package/src/datasources/provider-datasource.ts +15 -0
  165. package/src/datasources/select-concept-answers-datasource.ts +15 -0
  166. package/src/declarations.d.ts +4 -0
  167. package/src/external-function-context.tsx +8 -0
  168. package/src/form-context.tsx +42 -0
  169. package/src/form-engine.component.tsx +178 -0
  170. package/src/form-engine.scss +140 -0
  171. package/src/form-engine.test.tsx +817 -0
  172. package/src/globals.ts +1 -0
  173. package/src/hooks/useClobData.tsx +21 -0
  174. package/src/hooks/useConcepts.tsx +55 -0
  175. package/src/hooks/useDatasourceDependentValue.ts +16 -0
  176. package/src/hooks/useEncounter.tsx +32 -0
  177. package/src/hooks/useEncounterRole.tsx +15 -0
  178. package/src/hooks/useEvaluateFormFieldExpressions.ts +138 -0
  179. package/src/hooks/useFieldValidationResults.ts +18 -0
  180. package/src/hooks/useFormCollapse.tsx +36 -0
  181. package/src/hooks/useFormFieldValidators.ts +22 -0
  182. package/src/hooks/useFormFieldValueAdapters.ts +24 -0
  183. package/src/hooks/useFormFields.ts +37 -0
  184. package/src/hooks/useFormFieldsMeta.ts +48 -0
  185. package/src/hooks/useFormJson.test.tsx +173 -0
  186. package/src/hooks/useFormJson.tsx +237 -0
  187. package/src/hooks/useFormStateHelpers.ts +50 -0
  188. package/src/hooks/useFormsConfig.tsx +27 -0
  189. package/src/hooks/useInitialValues.ts +38 -0
  190. package/src/hooks/usePatientData.tsx +32 -0
  191. package/src/hooks/usePatientPrograms.ts +32 -0
  192. package/src/hooks/usePostSubmissionActions.test.tsx +42 -0
  193. package/src/hooks/usePostSubmissionActions.ts +31 -0
  194. package/src/hooks/useProcessorDependencies.ts +30 -0
  195. package/src/hooks/useRestMaxResultsCount.ts +5 -0
  196. package/src/hooks/useSystemSetting.ts +36 -0
  197. package/src/hooks/useWorkspaceLayout.ts +29 -0
  198. package/src/index.ts +12 -0
  199. package/src/lifecycle.ts +33 -0
  200. package/src/post-submission-actions/program-enrollment-action.ts +138 -0
  201. package/src/processors/encounter/encounter-form-processor.ts +337 -0
  202. package/src/processors/encounter/encounter-processor-helper.ts +320 -0
  203. package/src/processors/form-processor.ts +41 -0
  204. package/src/provider/form-factory-helper.ts +100 -0
  205. package/src/provider/form-factory-provider.tsx +169 -0
  206. package/src/provider/form-provider.tsx +37 -0
  207. package/src/registry/inbuilt-components/InbuiltPostSubmissionActions.ts +9 -0
  208. package/src/registry/inbuilt-components/control-templates.ts +57 -0
  209. package/src/registry/inbuilt-components/inbuiltControls.ts +99 -0
  210. package/src/registry/inbuilt-components/inbuiltDataSources.ts +41 -0
  211. package/src/registry/inbuilt-components/inbuiltFieldValueAdapters.ts +64 -0
  212. package/src/registry/inbuilt-components/inbuiltTransformers.ts +10 -0
  213. package/src/registry/inbuilt-components/inbuiltValidators.ts +33 -0
  214. package/src/registry/inbuilt-components/template-component-map.ts +28 -0
  215. package/src/registry/registry.test.ts +20 -0
  216. package/src/registry/registry.ts +261 -0
  217. package/src/routes.json +1 -0
  218. package/src/setupI18n.ts +16 -0
  219. package/src/setupTests.ts +5 -0
  220. package/src/transformers/default-schema-transformer.test.ts +155 -0
  221. package/src/transformers/default-schema-transformer.ts +239 -0
  222. package/src/types/domain.ts +129 -0
  223. package/src/types/index.ts +130 -0
  224. package/src/types/schema.ts +238 -0
  225. package/src/typings.d.ts +9 -0
  226. package/src/utils/boolean-utils.ts +25 -0
  227. package/src/utils/common-expression-helpers.ts +503 -0
  228. package/src/utils/common-utils.test.ts +136 -0
  229. package/src/utils/common-utils.ts +55 -0
  230. package/src/utils/error-utils.ts +37 -0
  231. package/src/utils/expression-parser.test.ts +308 -0
  232. package/src/utils/expression-parser.ts +158 -0
  233. package/src/utils/expression-runner.test.ts +387 -0
  234. package/src/utils/expression-runner.ts +219 -0
  235. package/src/utils/form-helper.test.ts +482 -0
  236. package/src/utils/form-helper.ts +210 -0
  237. package/src/utils/form-page-utils.ts +13 -0
  238. package/src/utils/forms-loader.test.ts +323 -0
  239. package/src/utils/forms-loader.ts +306 -0
  240. package/src/utils/post-submission-action-helper.ts +71 -0
  241. package/src/utils/test-utils.ts +54 -0
  242. package/src/utils/zscore-service.ts +59 -0
  243. package/src/validators/conditional-answered-validator.test.ts +61 -0
  244. package/src/validators/conditional-answered-validator.ts +17 -0
  245. package/src/validators/date-validator.test.ts +46 -0
  246. package/src/validators/date-validator.ts +19 -0
  247. package/src/validators/default-value-validator.test.ts +90 -0
  248. package/src/validators/default-value-validator.ts +36 -0
  249. package/src/validators/form-validator.test.ts +188 -0
  250. package/src/validators/form-validator.ts +95 -0
  251. package/src/validators/js-expression-validator.test.ts +118 -0
  252. package/src/validators/js-expression-validator.ts +44 -0
  253. package/src/validators/schema.ts +34 -0
  254. package/src/zscore/bfa_boys_5_above.json +2522 -0
  255. package/src/zscore/bfa_girls_5_above.json +2522 -0
  256. package/src/zscore/hfa_boys_5_above.json +2186 -0
  257. package/src/zscore/hfa_boys_below5.json +22286 -0
  258. package/src/zscore/hfa_girls_5_above.json +2186 -0
  259. package/src/zscore/hfa_girls_below5.json +22286 -0
  260. package/src/zscore/wfl_boys_below5.json +7814 -0
  261. package/src/zscore/wfl_girls_below5.json +7814 -0
  262. package/src/zscore-tests/bmi-age.test.tsx +88 -0
  263. package/src/zscore-tests/height-age.test.tsx +96 -0
  264. package/src/zscore-tests/weight-height.test.tsx +87 -0
  265. package/tools/i18next-parser.config.js +93 -0
  266. package/translations/en.json +47 -0
  267. package/translations/es.json +38 -0
  268. package/translations/fr.json +38 -0
  269. package/translations/km.json +38 -0
  270. package/tsconfig.json +19 -0
  271. package/turbo.json +15 -0
  272. package/webpack.config.js +1 -0
@@ -0,0 +1,42 @@
1
+ import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import { BaseOpenMRSDataSource } from './data-source';
3
+ import { isEmpty } from '../validators/form-validator';
4
+
5
+ export class ConceptDataSource extends BaseOpenMRSDataSource {
6
+ constructor() {
7
+ super(`${restBaseUrl}/concept?name=&searchType=fuzzy&v=custom:(uuid,display,conceptClass:(uuid,display))`);
8
+ }
9
+
10
+ fetchData(searchTerm: string, config?: Record<string, any>, uuid?: string): Promise<any[]> {
11
+ if (isEmpty(config?.class) && isEmpty(config?.concept) && !config?.useSetMembersByConcept && isEmpty(searchTerm)) {
12
+ return Promise.resolve([]);
13
+ }
14
+
15
+ let apiUrl = this.url;
16
+ if (config?.class) {
17
+ if (typeof config.class == 'string') {
18
+ const urlParts = apiUrl.split('searchType=fuzzy');
19
+ apiUrl = `${urlParts[0]}searchType=fuzzy&class=${config.class}&${urlParts[1]}`;
20
+ } else {
21
+ return openmrsFetch(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl).then(({ data }) => {
22
+ return data.results.filter(
23
+ (concept) => concept.conceptClass && config.class.includes(concept.conceptClass.uuid),
24
+ );
25
+ });
26
+ }
27
+ }
28
+
29
+ if (config?.concept && config?.useSetMembersByConcept) {
30
+ let urlParts = apiUrl.split('?name=&searchType=fuzzy&v=');
31
+ apiUrl = `${urlParts[0]}/${config.concept}?v=custom:(uuid,setMembers:(uuid,display))`;
32
+ return openmrsFetch(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl).then(({ data }) => {
33
+ // return the setMembers from the retrieved concept object
34
+ return data['setMembers'];
35
+ });
36
+ }
37
+
38
+ return openmrsFetch(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl).then(({ data }) => {
39
+ return data.results;
40
+ });
41
+ }
42
+ }
@@ -0,0 +1,23 @@
1
+ import { openmrsFetch, type OpenmrsResource } from '@openmrs/esm-framework';
2
+ import { type DataSource } from '../types';
3
+
4
+ export class BaseOpenMRSDataSource implements DataSource<OpenmrsResource> {
5
+ url: string;
6
+
7
+ constructor(url: string) {
8
+ this.url = url;
9
+ }
10
+
11
+ fetchData(searchTerm: string): Promise<any[]> {
12
+ return openmrsFetch(searchTerm ? `${this.url}&q=${searchTerm}` : this.url).then(({ data }) => {
13
+ return data.results;
14
+ });
15
+ }
16
+
17
+ toUuidAndDisplay(data: OpenmrsResource): OpenmrsResource {
18
+ if (typeof data.uuid === 'undefined' || typeof data.display === 'undefined') {
19
+ throw new Error("'uuid' or 'display' not found in the OpenMRS object.");
20
+ }
21
+ return data;
22
+ }
23
+ }
@@ -0,0 +1,15 @@
1
+ import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import { BaseOpenMRSDataSource } from './data-source';
3
+
4
+ export class EncounterRoleDataSource extends BaseOpenMRSDataSource {
5
+ constructor() {
6
+ super(null);
7
+ }
8
+
9
+ async fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> {
10
+ const rep = 'v=custom:(uuid,display,name)';
11
+ const url = `${restBaseUrl}/encounterrole?${rep}`;
12
+ const { data } = await openmrsFetch(searchTerm ? `${url}&q=${searchTerm}` : url);
13
+ return data?.results;
14
+ }
15
+ }
@@ -0,0 +1,11 @@
1
+ export class HistoricalDataSourceService {
2
+ dataSourceMap: Record<string, any> = {};
3
+
4
+ putObject(key: string, value: any) {
5
+ this.dataSourceMap[key] = value;
6
+ }
7
+
8
+ getObject(key: string) {
9
+ return this.dataSourceMap[key];
10
+ }
11
+ }
@@ -0,0 +1,27 @@
1
+ import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import { BaseOpenMRSDataSource } from './data-source';
3
+
4
+ export class LocationDataSource extends BaseOpenMRSDataSource {
5
+ constructor() {
6
+ super(`${restBaseUrl}/location?v=custom:(uuid,display)`);
7
+ }
8
+
9
+ fetchData(searchTerm: string, config?: Record<string, any>, uuid?: string): Promise<any[]> {
10
+ let apiUrl = this.url;
11
+ const urlParts = apiUrl.split('?');
12
+ if (config?.tag) {
13
+ apiUrl = `${urlParts[0]}?tag=${config.tag}&${urlParts[1]}`;
14
+ }
15
+ //overwrite url if there's a uuid value, meaning we are in edit mode
16
+ if (uuid) {
17
+ apiUrl = `${urlParts[0]}/${uuid}?${urlParts[1]}`;
18
+ }
19
+
20
+ return openmrsFetch(searchTerm ? `${apiUrl}&q=${searchTerm}` : apiUrl).then(({ data }) => {
21
+ if (data.results) {
22
+ return data.results;
23
+ }
24
+ return data;
25
+ });
26
+ }
27
+ }
@@ -0,0 +1,15 @@
1
+ import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import { BaseOpenMRSDataSource } from './data-source';
3
+
4
+ export class ProviderDataSource extends BaseOpenMRSDataSource {
5
+ constructor() {
6
+ super(null);
7
+ }
8
+
9
+ async fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> {
10
+ const rep = 'v=custom:(uuid,display)';
11
+ const url = `${restBaseUrl}/provider?${rep}`;
12
+ const { data } = await openmrsFetch(searchTerm ? `${url}&q=${searchTerm}` : url);
13
+ return data?.results;
14
+ }
15
+ }
@@ -0,0 +1,15 @@
1
+ import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import { BaseOpenMRSDataSource } from './data-source';
3
+
4
+ export class SelectConceptAnswersDatasource extends BaseOpenMRSDataSource {
5
+ constructor() {
6
+ super(`${restBaseUrl}/concept/conceptUuid?v=custom:(uuid,setMembers:(uuid,display),answers:(uuid,display))`);
7
+ }
8
+
9
+ fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> {
10
+ const apiUrl = this.url.replace('conceptUuid', config.referencedValue || config.concept);
11
+ return openmrsFetch(apiUrl).then(({ data }) => {
12
+ return data['setMembers'].length ? data['setMembers'] : data['answers'];
13
+ });
14
+ }
15
+ }
@@ -0,0 +1,4 @@
1
+ declare module '@carbon/react';
2
+ declare module '*.css';
3
+ declare module '*.scss';
4
+ declare type SideNavProps = {};
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import type { FormField } from './types';
3
+
4
+ export type ExternalFunctionContextProps = {
5
+ handleConfirmQuestionDeletion: (question: Readonly<FormField>) => Promise<void>;
6
+ };
7
+
8
+ export const ExternalFunctionContext = React.createContext<ExternalFunctionContextProps | undefined>(undefined);
@@ -0,0 +1,42 @@
1
+ import React from 'react';
2
+ import { type LayoutType } from '@openmrs/esm-framework';
3
+ import {
4
+ type PatientProgram,
5
+ type FormField,
6
+ type OpenmrsEncounter,
7
+ type PatientIdentifier,
8
+ type SessionMode,
9
+ } from './types';
10
+
11
+ type FormContextProps = {
12
+ values: Record<string, any>;
13
+ setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void;
14
+ setEncounterLocation: (value: any) => void;
15
+ encounterContext: EncounterContext;
16
+ fields: FormField[];
17
+ isFieldInitializationComplete: boolean;
18
+ isSubmitting: boolean;
19
+ layoutType?: LayoutType;
20
+ workspaceLayout?: 'minimized' | 'maximized';
21
+ };
22
+
23
+ export interface EncounterContext {
24
+ patient: fhir.Patient;
25
+ encounter: OpenmrsEncounter;
26
+ previousEncounter?: OpenmrsEncounter;
27
+ location: any;
28
+ sessionMode: SessionMode;
29
+ encounterDate: Date;
30
+ setEncounterDate(value: Date): void;
31
+ encounterProvider: string;
32
+ setEncounterProvider(value: string): void;
33
+ setEncounterLocation(value: any): void;
34
+ encounterRole: string;
35
+ setEncounterRole(value: string): void;
36
+ initValues?: Record<string, any>;
37
+ patientIdentifier?: PatientIdentifier;
38
+ patientPrograms?: Array<PatientProgram>;
39
+ getFormField?: (id: string) => FormField;
40
+ }
41
+
42
+ export const FormContext = React.createContext<FormContextProps | undefined>(undefined);
@@ -0,0 +1,178 @@
1
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import type { FormField, SessionMode, FormSchema } from './types';
3
+ import { useSession, type Visit } from '@openmrs/esm-framework';
4
+ import { useFormJson } from '.';
5
+ import FormProcessorFactory from './components/processor-factory/form-processor-factory.component';
6
+ import { Form } from '@carbon/react';
7
+ import Loader from './components/loaders/loader.component';
8
+ import { usePatientData } from './hooks/usePatientData';
9
+ import { useWorkspaceLayout } from './hooks/useWorkspaceLayout';
10
+ import { FormFactoryProvider } from './provider/form-factory-provider';
11
+ import classNames from 'classnames';
12
+ import styles from './form-engine.scss';
13
+ import { ButtonSet, Button, InlineLoading } from '@carbon/react';
14
+ import { I18nextProvider, useTranslation } from 'react-i18next';
15
+ import PatientBanner from './components/patient-banner/patient-banner.component';
16
+ import MarkdownWrapper from './components/inputs/markdown/markdown-wrapper.component';
17
+ import { init, teardown } from './lifecycle';
18
+ import { reportError } from './utils/error-utils';
19
+ import { moduleName } from './globals';
20
+
21
+ interface FormEngineProps {
22
+ patientUUID: string;
23
+ formUUID?: string;
24
+ formJson?: FormSchema;
25
+ encounterUUID?: string;
26
+ visit?: Visit;
27
+ formSessionIntent?: string;
28
+ mode?: SessionMode;
29
+ onSubmit?: () => void;
30
+ onCancel?: () => void;
31
+ handleClose?: () => void;
32
+ handleConfirmQuestionDeletion?: (question: Readonly<FormField>) => Promise<void>;
33
+ }
34
+
35
+ // TODOs:
36
+ // - Implement sidebar
37
+ // - Conditionally render the button set
38
+ // - Patient banner
39
+ const FormEngine = ({
40
+ formJson,
41
+ patientUUID,
42
+ formUUID,
43
+ encounterUUID,
44
+ visit,
45
+ formSessionIntent,
46
+ mode,
47
+ onSubmit,
48
+ onCancel,
49
+ handleClose,
50
+ handleConfirmQuestionDeletion,
51
+ }: FormEngineProps) => {
52
+ const { t } = useTranslation();
53
+ const session = useSession();
54
+ const ref = useRef(null);
55
+ const sessionDate = useMemo(() => {
56
+ return new Date();
57
+ }, []);
58
+ const workspaceLayout = useWorkspaceLayout(ref);
59
+ const { patient, isLoadingPatient } = usePatientData(patientUUID);
60
+ const [isLoadingDependencies, setIsLoadingDependencies] = useState(false);
61
+ const [showSidebar, setShowSidebar] = useState(false);
62
+ const [isSubmitting, setIsSubmitting] = useState(false);
63
+ // TODO: Updating this prop triggers a rerender of the entire form. This means whenever we scroll into a new page, the form is rerendered.
64
+ // Figure out a way to avoid this. Maybe use a ref with an observer instead of a state?
65
+ const [currentPage, setCurrentPage] = useState('');
66
+ const [showPatientBanner, setShowPatientBanner] = useState(false);
67
+ const {
68
+ formJson: refinedFormJson,
69
+ isLoading: isLoadingFormJson,
70
+ formError,
71
+ } = useFormJson(formUUID, formJson, encounterUUID, formSessionIntent);
72
+
73
+ const showButtonSet = useMemo(() => {
74
+ // if (mode === 'embedded-view') {
75
+ // return false;
76
+ // }
77
+ // return workspaceLayout === 'minimized' || (workspaceLayout === 'maximized' && scrollablePages.size <= 1);
78
+ return true;
79
+ }, [mode, workspaceLayout]);
80
+
81
+ useEffect(() => {
82
+ reportError(formError, t('errorLoadingFormSchema', 'Error loading form schema'));
83
+ }, [formError]);
84
+
85
+ useEffect(() => {
86
+ init();
87
+ return () => {
88
+ teardown();
89
+ };
90
+ }, []);
91
+
92
+ const handleSubmit = useCallback((e: React.FormEvent<HTMLFormElement>) => {
93
+ e.preventDefault();
94
+ setIsSubmitting(true);
95
+ }, []);
96
+
97
+ return (
98
+ <Form noValidate ref={ref} className={classNames('cds--form', styles.form)} onSubmit={handleSubmit}>
99
+ {isLoadingPatient || isLoadingFormJson ? (
100
+ <Loader />
101
+ ) : (
102
+ <FormFactoryProvider
103
+ patient={patient}
104
+ sessionMode={mode}
105
+ sessionDate={sessionDate}
106
+ formJson={refinedFormJson}
107
+ workspaceLayout={workspaceLayout}
108
+ location={session?.sessionLocation}
109
+ provider={session?.currentProvider}
110
+ visit={visit}
111
+ handleConfirmQuestionDeletion={handleConfirmQuestionDeletion}
112
+ formSubmissionProps={{
113
+ isSubmitting,
114
+ setIsSubmitting,
115
+ onSubmit,
116
+ onError: () => {},
117
+ handleClose: () => {},
118
+ }}
119
+ setCurrentPage={setCurrentPage}>
120
+ <div className={styles.formContainer}>
121
+ {isLoadingDependencies && (
122
+ <div className={styles.linearActivity}>
123
+ <div className={styles.indeterminate}></div>
124
+ </div>
125
+ )}
126
+ <div className={styles.formContent}>
127
+ {showSidebar && <div>{/* Side bar goes here */}</div>}
128
+ <div className={styles.formContentInner}>
129
+ {showPatientBanner && <PatientBanner patient={patient} hideActionsOverflow />}
130
+ {refinedFormJson.markdown && (
131
+ <div className={styles.markdownContainer}>
132
+ <MarkdownWrapper markdown={refinedFormJson.markdown} />
133
+ </div>
134
+ )}
135
+ <div className={styles.formBody}>
136
+ <FormProcessorFactory
137
+ formJson={refinedFormJson}
138
+ setIsLoadingFormDependencies={setIsLoadingDependencies}
139
+ />
140
+ </div>
141
+ {showButtonSet && (
142
+ <ButtonSet className={styles.minifiedButtons}>
143
+ <Button
144
+ kind="secondary"
145
+ onClick={() => {
146
+ onCancel && onCancel();
147
+ handleClose && handleClose();
148
+ // TODO: hideFormCollapseToggle();
149
+ }}>
150
+ {mode === 'view' ? t('close', 'Close') : t('cancel', 'Cancel')}
151
+ </Button>
152
+ <Button type="submit" disabled={isLoadingDependencies || mode === 'view'}>
153
+ {isSubmitting ? (
154
+ <InlineLoading description={t('submitting', 'Submitting') + '...'} />
155
+ ) : (
156
+ <span>{`${t('save', 'Save')}`}</span>
157
+ )}
158
+ </Button>
159
+ </ButtonSet>
160
+ )}
161
+ </div>
162
+ </div>
163
+ </div>
164
+ </FormFactoryProvider>
165
+ )}
166
+ </Form>
167
+ );
168
+ };
169
+
170
+ function I18FormEngine(props: FormEngineProps) {
171
+ return (
172
+ <I18nextProvider i18n={window.i18next} defaultNS={moduleName}>
173
+ <FormEngine {...props} />
174
+ </I18nextProvider>
175
+ );
176
+ }
177
+
178
+ export default I18FormEngine;
@@ -0,0 +1,140 @@
1
+ // replaces .formEngine
2
+ .form {
3
+ height: 100%;
4
+ overflow: hidden;
5
+ flex-grow: 1;
6
+ }
7
+
8
+ // replaces .container
9
+ .formContainer {
10
+ display: flex;
11
+ height: 100%;
12
+ overflow-y: hidden;
13
+ flex-direction: column;
14
+ }
15
+
16
+ // replaces .body
17
+ .formContent {
18
+ display: flex;
19
+ flex-direction: row;
20
+ height: 100%;
21
+ }
22
+
23
+ // replaces .content
24
+ .formContentInner {
25
+ height: var(--desktop-workspace-window-height);
26
+ margin: 0;
27
+ flex-basis: 65%;
28
+ flex-grow: 1;
29
+ flex-shrink: 1;
30
+ position: relative;
31
+ overflow-y: hidden;
32
+ display: flex;
33
+ flex-direction: column;
34
+ justify-content: space-between;
35
+ }
36
+
37
+ // replaces .contentBody
38
+ .formBody {
39
+ overflow-y: auto;
40
+ width: inherit;
41
+ position: relative;
42
+ flex-grow: 1;
43
+ display: flex;
44
+ flex-direction: column;
45
+ justify-content: flex-start;
46
+ }
47
+
48
+ .minifiedButtons {
49
+ button {
50
+ height: 4rem;
51
+ display: flex;
52
+ align-content: flex-start;
53
+ align-items: baseline;
54
+ min-width: 50%;
55
+ }
56
+ }
57
+
58
+ .markdownContainer {
59
+ padding: 1rem;
60
+ }
61
+
62
+ .loader {
63
+ @extend .formContent;
64
+ height: 1rem;
65
+ }
66
+
67
+ :global(.omrs-breakpoint-lt-desktop) {
68
+ .formContentInner {
69
+ height: var(--tablet-workspace-window-height);
70
+ }
71
+
72
+ .minifiedButtons {
73
+ border-top: 1px solid #ededed;
74
+ padding: 1.5rem 1rem;
75
+ background-color: white;
76
+ }
77
+ }
78
+
79
+ .linearActivity {
80
+ overflow: hidden;
81
+ width: 100%;
82
+ height: 4px;
83
+ background-color: var(--brand-03);
84
+ margin-top: 0px;
85
+ }
86
+
87
+ .determinate {
88
+ position: relative;
89
+ max-width: 100%;
90
+ height: 100%;
91
+ -webkit-transition: width 500ms ease-out 1s;
92
+ -moz-transition: width 500ms ease-out 1s;
93
+ -o-transition: width 500ms ease-out 1s;
94
+ transition: width 500ms ease-out 1s;
95
+ background-color: var(--brand-02);
96
+ }
97
+
98
+ .indeterminate {
99
+ position: relative;
100
+ width: 100%;
101
+ height: 100%;
102
+ }
103
+
104
+ .indeterminate:before {
105
+ content: '';
106
+ position: absolute;
107
+ height: 100%;
108
+ background-color: var(--brand-02);
109
+ animation: indeterminate_first 2.5s infinite ease-out;
110
+ }
111
+
112
+ .indeterminate:after {
113
+ content: '';
114
+ position: absolute;
115
+ height: 100%;
116
+ background-color: var(--brand-01);
117
+ animation: indeterminate_second 2.5s infinite ease-in;
118
+ }
119
+
120
+ @keyframes indeterminate_first {
121
+ 0% {
122
+ left: -100%;
123
+ width: 100%;
124
+ }
125
+ 100% {
126
+ left: 100%;
127
+ width: 10%;
128
+ }
129
+ }
130
+
131
+ @keyframes indeterminate_second {
132
+ 0% {
133
+ left: -150%;
134
+ width: 100%;
135
+ }
136
+ 100% {
137
+ left: 100%;
138
+ width: 10%;
139
+ }
140
+ }