@olaboot/esm-patient-registration-app 9.2.0

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 (250) hide show
  1. package/ADDRESS_CONFIGURATION.md +152 -0
  2. package/IDENTIFIER_CONFIGURATION.md +142 -0
  3. package/IMPLEMENTATION_SUMMARY.md +111 -0
  4. package/QUICK_START.md +95 -0
  5. package/README.md +7 -0
  6. package/address-required-fields-config.json +26 -0
  7. package/dist/126.js +1 -0
  8. package/dist/15.js +1 -0
  9. package/dist/1564.js +1 -0
  10. package/dist/1567.js +1 -0
  11. package/dist/1845.js +1 -0
  12. package/dist/1953.js +1 -0
  13. package/dist/200.js +1 -0
  14. package/dist/200.js.map +1 -0
  15. package/dist/215.js +1 -0
  16. package/dist/2178.js +1 -0
  17. package/dist/250.js +1 -0
  18. package/dist/250.js.map +1 -0
  19. package/dist/2523.js +1 -0
  20. package/dist/2523.js.map +1 -0
  21. package/dist/2566.js +1 -0
  22. package/dist/2586.js +1 -0
  23. package/dist/2586.js.map +1 -0
  24. package/dist/2716.js +1 -0
  25. package/dist/2716.js.map +1 -0
  26. package/dist/2759.js +1 -0
  27. package/dist/2821.js +6 -0
  28. package/dist/2821.js.map +1 -0
  29. package/dist/3089.js +1 -0
  30. package/dist/3089.js.map +1 -0
  31. package/dist/3230.js +1 -0
  32. package/dist/3441.js +1 -0
  33. package/dist/3565.js +1 -0
  34. package/dist/3571.js +1 -0
  35. package/dist/3571.js.map +1 -0
  36. package/dist/3746.js +1 -0
  37. package/dist/3925.js +1 -0
  38. package/dist/3946.js +1 -0
  39. package/dist/4024.js +1 -0
  40. package/dist/4024.js.map +1 -0
  41. package/dist/4744.js +1 -0
  42. package/dist/4744.js.map +1 -0
  43. package/dist/4809.js +1 -0
  44. package/dist/4894.js +1 -0
  45. package/dist/4970.js +1 -0
  46. package/dist/4970.js.map +1 -0
  47. package/dist/5130.js +1 -0
  48. package/dist/5187.js +1 -0
  49. package/dist/5491.js +1 -0
  50. package/dist/5491.js.map +1 -0
  51. package/dist/5595.js +1 -0
  52. package/dist/5961.js +1 -0
  53. package/dist/6133.js +1 -0
  54. package/dist/634.js +1 -0
  55. package/dist/634.js.map +1 -0
  56. package/dist/6456.js +1 -0
  57. package/dist/6466.js +1 -0
  58. package/dist/6613.js +1 -0
  59. package/dist/6783.js +1 -0
  60. package/dist/7073.js +38 -0
  61. package/dist/7073.js.map +1 -0
  62. package/dist/7154.js +1 -0
  63. package/dist/7154.js.map +1 -0
  64. package/dist/7348.js +1 -0
  65. package/dist/7439.js +1 -0
  66. package/dist/7439.js.map +1 -0
  67. package/dist/7543.js +1 -0
  68. package/dist/7607.js +1 -0
  69. package/dist/772.js +1 -0
  70. package/dist/7984.js +1 -0
  71. package/dist/7984.js.map +1 -0
  72. package/dist/8538.js +1 -0
  73. package/dist/8538.js.map +1 -0
  74. package/dist/8599.js +1 -0
  75. package/dist/8727.js +1 -0
  76. package/dist/8847.js +1 -0
  77. package/dist/9015.js +1 -0
  78. package/dist/906.js +1 -0
  79. package/dist/9065.js +1 -0
  80. package/dist/9182.js +1 -0
  81. package/dist/9339.js +1 -0
  82. package/dist/9453.js +1 -0
  83. package/dist/9833.js +1 -0
  84. package/dist/9833.js.map +1 -0
  85. package/dist/9856.js +1 -0
  86. package/dist/9856.js.map +1 -0
  87. package/dist/9920.js +1 -0
  88. package/dist/9938.js +1 -0
  89. package/dist/9943.js +1 -0
  90. package/dist/9943.js.map +1 -0
  91. package/dist/main.js +10 -0
  92. package/dist/main.js.map +1 -0
  93. package/dist/olaboot-esm-patient-registration-app.js +5 -0
  94. package/dist/olaboot-esm-patient-registration-app.js.buildmanifest.json +1627 -0
  95. package/dist/olaboot-esm-patient-registration-app.js.map +1 -0
  96. package/dist/routes.json +1 -0
  97. package/docs/images/patient-registration-hierarchy.png +0 -0
  98. package/example-config.json +14 -0
  99. package/jest.config.js +3 -0
  100. package/package.json +60 -0
  101. package/rspack.config.js +1 -0
  102. package/src/add-patient-link.extension.tsx +21 -0
  103. package/src/add-patient-link.scss +3 -0
  104. package/src/add-patient-link.test.tsx +16 -0
  105. package/src/config-schema.ts +507 -0
  106. package/src/constants.ts +14 -0
  107. package/src/declarations.d.ts +4 -0
  108. package/src/index.ts +59 -0
  109. package/src/nav-link.test.tsx +13 -0
  110. package/src/nav-link.tsx +10 -0
  111. package/src/offline.resources.ts +157 -0
  112. package/src/offline.ts +93 -0
  113. package/src/patient-photo.extension.tsx +11 -0
  114. package/src/patient-registration/before-save-prompt.component.tsx +72 -0
  115. package/src/patient-registration/field/__mocks__/field.resource.ts +60 -0
  116. package/src/patient-registration/field/address/address-field.component.tsx +186 -0
  117. package/src/patient-registration/field/address/address-hierarchy-levels.component.tsx +71 -0
  118. package/src/patient-registration/field/address/address-hierarchy.resource.tsx +157 -0
  119. package/src/patient-registration/field/address/address-hierarchy.test.tsx +296 -0
  120. package/src/patient-registration/field/address/address-search.component.tsx +87 -0
  121. package/src/patient-registration/field/address/address-search.scss +53 -0
  122. package/src/patient-registration/field/address/address-search.test.tsx +141 -0
  123. package/src/patient-registration/field/address/custom-address-field.component.tsx +32 -0
  124. package/src/patient-registration/field/cause-of-death/cause-of-death.component.tsx +98 -0
  125. package/src/patient-registration/field/custom-field.component.tsx +25 -0
  126. package/src/patient-registration/field/date-and-time-of-death/date-and-time-of-death.component.tsx +79 -0
  127. package/src/patient-registration/field/dob/dob.component.tsx +167 -0
  128. package/src/patient-registration/field/dob/dob.test.tsx +90 -0
  129. package/src/patient-registration/field/field.component.tsx +53 -0
  130. package/src/patient-registration/field/field.resource.ts +42 -0
  131. package/src/patient-registration/field/field.scss +171 -0
  132. package/src/patient-registration/field/field.test.tsx +330 -0
  133. package/src/patient-registration/field/gender/gender-field.component.tsx +54 -0
  134. package/src/patient-registration/field/gender/gender-field.test.tsx +99 -0
  135. package/src/patient-registration/field/id/id-field.component.tsx +136 -0
  136. package/src/patient-registration/field/id/id-field.test.tsx +121 -0
  137. package/src/patient-registration/field/id/identifier-selection-overlay.component.tsx +200 -0
  138. package/src/patient-registration/field/id/identifier-selection.scss +41 -0
  139. package/src/patient-registration/field/name/name-field.component.tsx +148 -0
  140. package/src/patient-registration/field/obs/obs-field.component.tsx +261 -0
  141. package/src/patient-registration/field/obs/obs-field.test.tsx +299 -0
  142. package/src/patient-registration/field/person-attributes/coded-person-attribute-field.component.tsx +120 -0
  143. package/src/patient-registration/field/person-attributes/coded-person-attribute-field.test.tsx +141 -0
  144. package/src/patient-registration/field/person-attributes/location-person-attribute-field.component.tsx +105 -0
  145. package/src/patient-registration/field/person-attributes/location-person-attribute-field.resource.tsx +48 -0
  146. package/src/patient-registration/field/person-attributes/person-attribute-field.component.tsx +100 -0
  147. package/src/patient-registration/field/person-attributes/person-attribute-field.test.tsx +193 -0
  148. package/src/patient-registration/field/person-attributes/person-attributes.resource.ts +20 -0
  149. package/src/patient-registration/field/person-attributes/text-person-attribute-field.component.tsx +58 -0
  150. package/src/patient-registration/field/person-attributes/text-person-attribute-field.test.tsx +90 -0
  151. package/src/patient-registration/field/phone/phone-field.component.tsx +17 -0
  152. package/src/patient-registration/form-manager.test.ts +91 -0
  153. package/src/patient-registration/form-manager.ts +443 -0
  154. package/src/patient-registration/input/basic-input/input/input.component.tsx +183 -0
  155. package/src/patient-registration/input/basic-input/input/input.test.tsx +72 -0
  156. package/src/patient-registration/input/basic-input/select/select-input.component.tsx +32 -0
  157. package/src/patient-registration/input/basic-input/select/select-input.test.tsx +49 -0
  158. package/src/patient-registration/input/combo-input/combo-input.component.tsx +130 -0
  159. package/src/patient-registration/input/combo-input/selection-tick.component.tsx +20 -0
  160. package/src/patient-registration/input/custom-input/autosuggest/autosuggest.component.tsx +187 -0
  161. package/src/patient-registration/input/custom-input/autosuggest/autosuggest.scss +62 -0
  162. package/src/patient-registration/input/custom-input/autosuggest/autosuggest.test.tsx +164 -0
  163. package/src/patient-registration/input/custom-input/identifier/identifier-input.component.tsx +193 -0
  164. package/src/patient-registration/input/custom-input/identifier/identifier-input.test.tsx +335 -0
  165. package/src/patient-registration/input/custom-input/identifier/utils.test.ts +81 -0
  166. package/src/patient-registration/input/custom-input/identifier/utils.ts +19 -0
  167. package/src/patient-registration/input/dummy-data/dummy-data-input.component.tsx +56 -0
  168. package/src/patient-registration/input/dummy-data/dummy-data-input.test.tsx +34 -0
  169. package/src/patient-registration/input/input.scss +122 -0
  170. package/src/patient-registration/patient-registration-context.ts +35 -0
  171. package/src/patient-registration/patient-registration-hooks.ts +376 -0
  172. package/src/patient-registration/patient-registration-utils.test.ts +33 -0
  173. package/src/patient-registration/patient-registration-utils.ts +214 -0
  174. package/src/patient-registration/patient-registration.component.tsx +266 -0
  175. package/src/patient-registration/patient-registration.resource.test.tsx +22 -0
  176. package/src/patient-registration/patient-registration.resource.ts +198 -0
  177. package/src/patient-registration/patient-registration.scss +103 -0
  178. package/src/patient-registration/patient-registration.test.tsx +580 -0
  179. package/src/patient-registration/patient-registration.types.ts +322 -0
  180. package/src/patient-registration/section/death-info/death-info-section.component.tsx +36 -0
  181. package/src/patient-registration/section/death-info/death-info-section.test.tsx +47 -0
  182. package/src/patient-registration/section/demographics/demographics-section.component.tsx +30 -0
  183. package/src/patient-registration/section/demographics/demographics-section.test.tsx +98 -0
  184. package/src/patient-registration/section/generic-section.component.tsx +17 -0
  185. package/src/patient-registration/section/patient-relationships/relationships-section.component.tsx +234 -0
  186. package/src/patient-registration/section/patient-relationships/relationships-section.test.tsx +113 -0
  187. package/src/patient-registration/section/patient-relationships/relationships.resource.tsx +78 -0
  188. package/src/patient-registration/section/patient-relationships/relationships.scss +35 -0
  189. package/src/patient-registration/section/section-wrapper.component.tsx +40 -0
  190. package/src/patient-registration/section/section.component.tsx +23 -0
  191. package/src/patient-registration/section/section.scss +21 -0
  192. package/src/patient-registration/ui-components/overlay/overlay.component.tsx +51 -0
  193. package/src/patient-registration/ui-components/overlay/overlay.scss +63 -0
  194. package/src/patient-registration/validation/patient-registration-validation.test.ts +205 -0
  195. package/src/patient-registration/validation/patient-registration-validation.ts +123 -0
  196. package/src/resource.ts +12 -0
  197. package/src/resources-context.ts +14 -0
  198. package/src/root.component.tsx +63 -0
  199. package/src/root.scss +7 -0
  200. package/src/routes.json +61 -0
  201. package/src/widgets/cancel-patient-edit.modal.tsx +33 -0
  202. package/src/widgets/cancel-patient-edit.test.tsx +22 -0
  203. package/src/widgets/delete-identifier-confirmation.modal.tsx +48 -0
  204. package/src/widgets/delete-identifier-confirmation.test.tsx +32 -0
  205. package/src/widgets/edit-patient-details-button.component.tsx +33 -0
  206. package/src/widgets/edit-patient-details-button.scss +3 -0
  207. package/src/widgets/edit-patient-details-button.test.tsx +35 -0
  208. package/translations/am.json +120 -0
  209. package/translations/ar.json +120 -0
  210. package/translations/ar_SY.json +120 -0
  211. package/translations/bn.json +120 -0
  212. package/translations/cs.json +120 -0
  213. package/translations/de.json +120 -0
  214. package/translations/en.json +120 -0
  215. package/translations/en_US.json +120 -0
  216. package/translations/es.json +120 -0
  217. package/translations/es_MX.json +120 -0
  218. package/translations/fr.json +120 -0
  219. package/translations/he.json +120 -0
  220. package/translations/hi.json +120 -0
  221. package/translations/hi_IN.json +120 -0
  222. package/translations/id.json +120 -0
  223. package/translations/it.json +120 -0
  224. package/translations/ka.json +120 -0
  225. package/translations/km.json +120 -0
  226. package/translations/ku.json +120 -0
  227. package/translations/ky.json +120 -0
  228. package/translations/lg.json +120 -0
  229. package/translations/ne.json +120 -0
  230. package/translations/pl.json +120 -0
  231. package/translations/pt.json +120 -0
  232. package/translations/pt_BR.json +120 -0
  233. package/translations/qu.json +120 -0
  234. package/translations/ro_RO.json +120 -0
  235. package/translations/ru_RU.json +120 -0
  236. package/translations/si.json +120 -0
  237. package/translations/sq.json +120 -0
  238. package/translations/sw.json +120 -0
  239. package/translations/sw_KE.json +120 -0
  240. package/translations/tr.json +120 -0
  241. package/translations/tr_TR.json +120 -0
  242. package/translations/uk.json +120 -0
  243. package/translations/uz.json +120 -0
  244. package/translations/uz@Latn.json +120 -0
  245. package/translations/uz_UZ.json +120 -0
  246. package/translations/vi.json +120 -0
  247. package/translations/zh.json +120 -0
  248. package/translations/zh_CN.json +120 -0
  249. package/translations/zh_TW.json +120 -0
  250. package/tsconfig.json +5 -0
@@ -0,0 +1,148 @@
1
+ import React, { useCallback } from 'react';
2
+ import { useTranslation } from 'react-i18next';
3
+ import { ContentSwitcher, Switch } from '@carbon/react';
4
+ import { useField } from 'formik';
5
+ import { ExtensionSlot, useConfig } from '@openmrs/esm-framework';
6
+ import { type RegistrationConfig } from '../../../config-schema';
7
+ import { usePatientRegistrationContext } from '../../patient-registration-context';
8
+ import { Input } from '../../input/basic-input/input/input.component';
9
+ import styles from '../field.scss';
10
+
11
+ export const unidentifiedPatientAttributeTypeUuid = '8b56eac7-5c76-4b9c-8c6f-1deab8d3fc47';
12
+ const containsNoNumbers = /^([^0-9]*)$/;
13
+
14
+ function checkNumber(value: string) {
15
+ if (!containsNoNumbers.test(value)) {
16
+ return 'numberInNameDubious';
17
+ }
18
+
19
+ return undefined;
20
+ }
21
+
22
+ export const NameField = () => {
23
+ const { t } = useTranslation();
24
+ const { setCapturePhotoProps, currentPhoto, setFieldValue, setFieldTouched } = usePatientRegistrationContext();
25
+
26
+ const {
27
+ fieldConfigurations: {
28
+ name: {
29
+ displayCapturePhoto,
30
+ allowUnidentifiedPatients,
31
+ defaultUnknownGivenName,
32
+ defaultUnknownFamilyName,
33
+ displayMiddleName,
34
+ displayReverseFieldOrder,
35
+ },
36
+ },
37
+ } = useConfig<RegistrationConfig>();
38
+
39
+ const [{ value: isPatientUnknownValue }, , { setValue: setUnknownPatient }] = useField<string>(
40
+ `attributes.${unidentifiedPatientAttributeTypeUuid}`,
41
+ );
42
+
43
+ const isPatientUnknown = isPatientUnknownValue === 'true';
44
+
45
+ const onCapturePhoto = useCallback(
46
+ (dataUri: string, photoDateTime: string) => {
47
+ if (setCapturePhotoProps) {
48
+ setCapturePhotoProps({
49
+ imageData: dataUri,
50
+ dateTime: photoDateTime,
51
+ });
52
+ setFieldTouched('photo', true, false);
53
+ }
54
+ },
55
+ [setCapturePhotoProps, setFieldTouched],
56
+ );
57
+
58
+ const toggleNameKnown = (e) => {
59
+ if (e.name === 'known') {
60
+ setFieldValue('givenName', '');
61
+ setFieldValue('familyName', '');
62
+ setUnknownPatient('false');
63
+ } else {
64
+ setFieldValue('givenName', defaultUnknownGivenName);
65
+ setFieldValue('familyName', defaultUnknownFamilyName);
66
+ setUnknownPatient('true');
67
+ }
68
+ setFieldTouched('givenName', true);
69
+ setFieldTouched('familyName', true);
70
+ setFieldTouched(`attributes.${unidentifiedPatientAttributeTypeUuid}`, true, false);
71
+ };
72
+
73
+ const firstNameField = (
74
+ <Input
75
+ id="givenName"
76
+ name="givenName"
77
+ labelText={t('givenNameLabelText', 'First Name')}
78
+ checkWarning={checkNumber}
79
+ required
80
+ />
81
+ );
82
+
83
+ const middleNameField = displayMiddleName && (
84
+ <Input
85
+ id="middleName"
86
+ name="middleName"
87
+ labelText={t('middleNameLabelText', 'Middle Name')}
88
+ checkWarning={checkNumber}
89
+ />
90
+ );
91
+
92
+ const familyNameField = (
93
+ <Input
94
+ id="familyName"
95
+ name="familyName"
96
+ labelText={t('familyNameLabelText', 'Family Name')}
97
+ checkWarning={checkNumber}
98
+ required
99
+ />
100
+ );
101
+
102
+ return (
103
+ <div>
104
+ <h4 className={styles.productiveHeading02Light}>{t('fullNameLabelText', 'Full Name')}</h4>
105
+ <div className={styles.grid}>
106
+ {displayCapturePhoto && (
107
+ <ExtensionSlot
108
+ className={styles.photoExtension}
109
+ name="capture-patient-photo-slot"
110
+ state={{ onCapturePhoto, initialState: currentPhoto }}
111
+ />
112
+ )}
113
+
114
+ <div className={styles.nameField}>
115
+ {(allowUnidentifiedPatients || isPatientUnknown) && (
116
+ <>
117
+ <div className={styles.dobContentSwitcherLabel}>
118
+ <span className={styles.label01}>{t('patientNameKnown', "Patient's Name is Known?")}</span>
119
+ </div>
120
+ <ContentSwitcher
121
+ className={styles.contentSwitcher}
122
+ size="md"
123
+ selectedIndex={isPatientUnknown ? 1 : 0}
124
+ onChange={toggleNameKnown}>
125
+ <Switch name="known" text={t('yes', 'Yes')} />
126
+ <Switch name="unknown" text={t('no', 'No')} />
127
+ </ContentSwitcher>
128
+ </>
129
+ )}
130
+ {!isPatientUnknown &&
131
+ (!displayReverseFieldOrder ? (
132
+ <>
133
+ {firstNameField}
134
+ {middleNameField}
135
+ {familyNameField}
136
+ </>
137
+ ) : (
138
+ <>
139
+ {familyNameField}
140
+ {middleNameField}
141
+ {firstNameField}
142
+ </>
143
+ ))}
144
+ </div>
145
+ </div>
146
+ </div>
147
+ );
148
+ };
@@ -0,0 +1,261 @@
1
+ import React, { useMemo } from 'react';
2
+ import classNames from 'classnames';
3
+ import { Field } from 'formik';
4
+ import { useTranslation } from 'react-i18next';
5
+ import { InlineNotification, Layer, Select, SelectItem } from '@carbon/react';
6
+ import { OpenmrsDatePicker, useConfig } from '@openmrs/esm-framework';
7
+ import { type ConceptResponse } from '../../patient-registration.types';
8
+ import { type FieldDefinition, type RegistrationConfig } from '../../../config-schema';
9
+ import { Input } from '../../input/basic-input/input/input.component';
10
+ import { useConcept, useConceptAnswers } from '../field.resource';
11
+ import { usePatientRegistrationContext } from '../../patient-registration-context';
12
+ import styles from './../field.scss';
13
+
14
+ export interface ObsFieldProps {
15
+ fieldDefinition: FieldDefinition;
16
+ }
17
+
18
+ export function ObsField({ fieldDefinition }: ObsFieldProps) {
19
+ const { t } = useTranslation();
20
+ const { data: concept, isLoading } = useConcept(fieldDefinition.uuid);
21
+ const config = useConfig<RegistrationConfig>();
22
+
23
+ if (!config.registrationObs.encounterTypeUuid) {
24
+ console.error(
25
+ 'The registration form has been configured to have obs fields, ' +
26
+ 'but no registration encounter type has been configured. Obs fields ' +
27
+ 'will not be displayed.',
28
+ );
29
+ return null;
30
+ }
31
+
32
+ if (isLoading) {
33
+ return null;
34
+ }
35
+
36
+ switch (concept.datatype.display) {
37
+ case 'Text':
38
+ return (
39
+ <TextObsField
40
+ concept={concept}
41
+ validationRegex={fieldDefinition.validation.matches}
42
+ label={fieldDefinition.label}
43
+ required={fieldDefinition.validation.required}
44
+ />
45
+ );
46
+ case 'Numeric':
47
+ return (
48
+ <NumericObsField
49
+ concept={concept}
50
+ label={fieldDefinition.label}
51
+ required={fieldDefinition.validation.required}
52
+ />
53
+ );
54
+ case 'Date':
55
+ return (
56
+ <DateObsField
57
+ concept={concept}
58
+ label={fieldDefinition.label}
59
+ required={fieldDefinition.validation.required}
60
+ allowPastDates={fieldDefinition.allowPastDates}
61
+ allowFutureDates={fieldDefinition.allowFutureDates}
62
+ />
63
+ );
64
+ case 'Coded':
65
+ return (
66
+ <CodedObsField
67
+ concept={concept}
68
+ answerConceptSetUuid={fieldDefinition.answerConceptSetUuid}
69
+ label={fieldDefinition.label}
70
+ required={fieldDefinition.validation.required}
71
+ customConceptAnswers={fieldDefinition.customConceptAnswers}
72
+ />
73
+ );
74
+ default:
75
+ return (
76
+ <InlineNotification kind="error" title="Error">
77
+ {t(
78
+ 'obsFieldUnknownDatatype',
79
+ `Concept for obs field '{{fieldDefinitionId}}' has unknown datatype '{{datatypeName}}'`,
80
+ { fieldDefinitionId: fieldDefinition.id, datatypeName: concept.datatype.display },
81
+ )}
82
+ </InlineNotification>
83
+ );
84
+ }
85
+ }
86
+
87
+ interface TextObsFieldProps {
88
+ concept: ConceptResponse;
89
+ validationRegex: string;
90
+ label: string;
91
+ required?: boolean;
92
+ }
93
+
94
+ function TextObsField({ concept, validationRegex, label, required }: TextObsFieldProps) {
95
+ const { t } = useTranslation();
96
+
97
+ const validateInput = (value: string) => {
98
+ if (!value || !validationRegex || validationRegex === '' || typeof validationRegex !== 'string' || value === '') {
99
+ return;
100
+ }
101
+ const regex = new RegExp(validationRegex);
102
+ if (regex.test(value)) {
103
+ return;
104
+ } else {
105
+ return t('invalidInput', 'Invalid Input');
106
+ }
107
+ };
108
+
109
+ const fieldName = `obs.${concept.uuid}`;
110
+ return (
111
+ <div className={classNames(styles.customField, styles.halfWidthInDesktopView)}>
112
+ <Field name={fieldName} validate={validateInput}>
113
+ {({ field, form: { touched, errors }, meta }) => {
114
+ return (
115
+ <Input
116
+ id={fieldName}
117
+ labelText={label ?? concept.display}
118
+ required={required}
119
+ invalid={errors[fieldName] && touched[fieldName]}
120
+ {...field}
121
+ />
122
+ );
123
+ }}
124
+ </Field>
125
+ </div>
126
+ );
127
+ }
128
+
129
+ interface NumericObsFieldProps {
130
+ concept: ConceptResponse;
131
+ label: string;
132
+ required?: boolean;
133
+ }
134
+
135
+ function NumericObsField({ concept, label, required }: NumericObsFieldProps) {
136
+ const fieldName = `obs.${concept.uuid}`;
137
+
138
+ return (
139
+ <div className={classNames(styles.customField, styles.halfWidthInDesktopView)}>
140
+ <Field name={fieldName}>
141
+ {({ field, form: { touched, errors }, meta }) => {
142
+ return (
143
+ <Input
144
+ id={fieldName}
145
+ labelText={label ?? concept.display}
146
+ required={required}
147
+ invalid={errors[fieldName] && touched[fieldName]}
148
+ type="number"
149
+ {...field}
150
+ />
151
+ );
152
+ }}
153
+ </Field>
154
+ </div>
155
+ );
156
+ }
157
+
158
+ interface DateObsFieldProps {
159
+ concept: ConceptResponse;
160
+ label: string;
161
+ required?: boolean;
162
+ allowPastDates?: boolean;
163
+ allowFutureDates?: boolean;
164
+ }
165
+
166
+ function DateObsField({ concept, label, required, allowPastDates, allowFutureDates }: DateObsFieldProps) {
167
+ const { t } = useTranslation();
168
+ const fieldName = `obs.${concept.uuid}`;
169
+ const { setFieldValue } = usePatientRegistrationContext();
170
+ const futureDatesAllowed = allowFutureDates ?? true;
171
+ const pastDatesAllowed = allowPastDates ?? true;
172
+
173
+ const onDateChange = (date: Date) => {
174
+ setFieldValue(fieldName, date);
175
+ };
176
+
177
+ return (
178
+ <Layer>
179
+ <div className={styles.dobField}>
180
+ <Field name={fieldName}>
181
+ {({ field, form: { touched, errors }, meta }) => {
182
+ return (
183
+ <>
184
+ <OpenmrsDatePicker
185
+ id={fieldName}
186
+ {...field}
187
+ isRequired={required}
188
+ onChange={onDateChange}
189
+ labelText={label ?? concept.display}
190
+ isInvalid={errors[fieldName] && touched[fieldName]}
191
+ invalidText={t(meta.error)}
192
+ value={field.value}
193
+ minDate={!pastDatesAllowed ? new Date() : undefined}
194
+ maxDate={!futureDatesAllowed ? new Date() : undefined}
195
+ />
196
+ </>
197
+ );
198
+ }}
199
+ </Field>
200
+ </div>
201
+ </Layer>
202
+ );
203
+ }
204
+
205
+ interface CodedObsFieldProps {
206
+ concept: ConceptResponse;
207
+ answerConceptSetUuid?: string;
208
+ label?: string;
209
+ required?: boolean;
210
+ customConceptAnswers: Array<{ uuid: string; label?: string }>;
211
+ }
212
+
213
+ function CodedObsField({ concept, answerConceptSetUuid, label, required, customConceptAnswers }: CodedObsFieldProps) {
214
+ const { t } = useTranslation();
215
+ const fieldName = `obs.${concept.uuid}`;
216
+
217
+ const { data: conceptAnswers, isLoading: isLoadingConceptAnswers } = useConceptAnswers(
218
+ customConceptAnswers.length ? '' : answerConceptSetUuid ?? concept.uuid,
219
+ );
220
+
221
+ const answers = useMemo(
222
+ () =>
223
+ customConceptAnswers.length
224
+ ? customConceptAnswers
225
+ : isLoadingConceptAnswers
226
+ ? []
227
+ : conceptAnswers.map((answer) => ({ ...answer, label: answer.display })),
228
+ [customConceptAnswers, conceptAnswers, isLoadingConceptAnswers],
229
+ );
230
+
231
+ return (
232
+ <div className={classNames(styles.customField, styles.halfWidthInDesktopView)}>
233
+ {!isLoadingConceptAnswers ? (
234
+ <Field name={fieldName}>
235
+ {({ field, form: { touched, errors }, meta }) => {
236
+ return (
237
+ <Layer>
238
+ <Select
239
+ id={fieldName}
240
+ name={fieldName}
241
+ labelText={label ?? concept?.display}
242
+ required={required}
243
+ invalid={errors[fieldName] && touched[fieldName]}
244
+ {...field}>
245
+ <SelectItem
246
+ key={`no-answer-select-item-${fieldName}`}
247
+ value={''}
248
+ text={t('selectAnOption', 'Select an option')}
249
+ />
250
+ {answers.map((answer) => (
251
+ <SelectItem key={answer.uuid} value={answer.uuid} text={answer.label} />
252
+ ))}
253
+ </Select>
254
+ </Layer>
255
+ );
256
+ }}
257
+ </Field>
258
+ ) : null}
259
+ </div>
260
+ );
261
+ }
@@ -0,0 +1,299 @@
1
+ import React from 'react';
2
+ import userEvent from '@testing-library/user-event';
3
+ import { render, screen } from '@testing-library/react';
4
+ import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework';
5
+ import { esmPatientRegistrationSchema, type FieldDefinition, type RegistrationConfig } from '../../../config-schema';
6
+ import { useConcept, useConceptAnswers } from '../field.resource';
7
+ import { ObsField } from './obs-field.component';
8
+ import {
9
+ PatientRegistrationContextProvider,
10
+ type PatientRegistrationContextProps,
11
+ } from '../../patient-registration-context';
12
+ import { mockOpenmrsId, mockPatient } from '__mocks__';
13
+
14
+ const mockUseConcept = jest.mocked(useConcept);
15
+ const mockUseConceptAnswers = jest.mocked(useConceptAnswers);
16
+ const mockUseConfig = jest.mocked(useConfig<RegistrationConfig>);
17
+
18
+ jest.mock('../field.resource');
19
+
20
+ const useConceptMockImpl = (uuid: string) => {
21
+ let data;
22
+ if (uuid == 'weight-uuid') {
23
+ data = {
24
+ uuid: 'weight-uuid',
25
+ display: 'Weight (kg)',
26
+ datatype: { display: 'Numeric', uuid: 'num' },
27
+ answers: [],
28
+ setMembers: [],
29
+ };
30
+ } else if (uuid == 'chief-complaint-uuid') {
31
+ data = {
32
+ uuid: 'chief-complaint-uuid',
33
+ display: 'Chief Complaint',
34
+ datatype: { display: 'Text', uuid: 'txt' },
35
+ answers: [],
36
+ setMembers: [],
37
+ };
38
+ } else if (uuid == 'nationality-uuid') {
39
+ data = {
40
+ uuid: 'nationality-uuid',
41
+ display: 'Nationality',
42
+ datatype: { display: 'Coded', uuid: 'cdd' },
43
+ answers: [
44
+ { display: 'USA', uuid: 'usa' },
45
+ { display: 'Mexico', uuid: 'mex' },
46
+ ],
47
+ setMembers: [],
48
+ };
49
+ } else if (uuid == 'vaccination-date-uuid') {
50
+ data = {
51
+ uuid: 'vaccination-date-uuid',
52
+ display: 'Vaccination Date',
53
+ datatype: { display: 'Date', uuid: 'date' },
54
+ answers: [],
55
+ setMembers: [],
56
+ };
57
+ } else {
58
+ throw Error(`Programming error, you probably didn't mean to do this: unknown concept uuid '${uuid}'`);
59
+ }
60
+ return {
61
+ data: data ?? null,
62
+ isLoading: !data,
63
+ };
64
+ };
65
+
66
+ const useConceptAnswersMockImpl = (uuid: string) => {
67
+ if (uuid == 'nationality-uuid') {
68
+ return {
69
+ data: [
70
+ { display: 'USA', uuid: 'usa' },
71
+ { display: 'Mexico', uuid: 'mex' },
72
+ ],
73
+ isLoading: false,
74
+ error: null,
75
+ };
76
+ } else if (uuid == 'other-countries-uuid') {
77
+ return {
78
+ data: [
79
+ { display: 'Kenya', uuid: 'ke' },
80
+ { display: 'Uganda', uuid: 'ug' },
81
+ ],
82
+ isLoading: false,
83
+ error: null,
84
+ };
85
+ } else if (uuid == '') {
86
+ return {
87
+ data: [],
88
+ isLoading: false,
89
+ error: null,
90
+ };
91
+ } else {
92
+ throw Error(`Programming error, you probably didn't mean to do this: unknown concept answer set uuid '${uuid}'`);
93
+ }
94
+ };
95
+
96
+ type FieldProps = {
97
+ children: ({ field, form: { touched, errors }, meta }) => React.ReactNode;
98
+ };
99
+
100
+ jest.mock('formik', () => ({
101
+ ...(jest.requireActual('formik') as object),
102
+ Field: jest.fn(({ children }: FieldProps) => (
103
+ <>{children({ field: {}, form: { touched: {}, errors: {} }, meta: { error: undefined } })}</>
104
+ )),
105
+ useField: jest.fn(() => [{ value: null }, {}]),
106
+ }));
107
+
108
+ const textFieldDef: FieldDefinition = {
109
+ id: 'chief-complaint',
110
+ type: 'obs',
111
+ label: 'Chief complaint',
112
+ placeholder: '',
113
+ showHeading: false,
114
+ uuid: 'chief-complaint-uuid',
115
+ validation: {
116
+ required: false,
117
+ matches: null,
118
+ },
119
+ answerConceptSetUuid: null,
120
+ customConceptAnswers: [],
121
+ };
122
+
123
+ const numberFieldDef: FieldDefinition = {
124
+ id: 'weight',
125
+ type: 'obs',
126
+ label: 'Weight',
127
+ placeholder: '',
128
+ showHeading: false,
129
+ uuid: 'weight-uuid',
130
+ validation: {
131
+ required: false,
132
+ matches: null,
133
+ },
134
+ answerConceptSetUuid: null,
135
+ customConceptAnswers: [],
136
+ };
137
+
138
+ const dateFieldDef: FieldDefinition = {
139
+ id: 'vac_date',
140
+ type: 'obs',
141
+ label: 'Vaccination date',
142
+ placeholder: '',
143
+ showHeading: false,
144
+ uuid: 'vaccination-date-uuid',
145
+ validation: {
146
+ required: false,
147
+ matches: null,
148
+ },
149
+ answerConceptSetUuid: null,
150
+ customConceptAnswers: [],
151
+ };
152
+
153
+ const codedFieldDef: FieldDefinition = {
154
+ id: 'nationality',
155
+ type: 'obs',
156
+ label: 'Nationality',
157
+ placeholder: '',
158
+ showHeading: false,
159
+ uuid: 'nationality-uuid',
160
+ validation: {
161
+ required: false,
162
+ matches: null,
163
+ },
164
+ answerConceptSetUuid: null,
165
+ customConceptAnswers: [],
166
+ };
167
+
168
+ const mockInitialFormValues = {
169
+ additionalFamilyName: '',
170
+ additionalGivenName: '',
171
+ additionalMiddleName: '',
172
+ addNameInLocalLanguage: false,
173
+ address: {},
174
+ birthdate: null,
175
+ birthdateEstimated: false,
176
+ deathCause: '',
177
+ deathDate: '',
178
+ familyName: 'Doe',
179
+ gender: 'male',
180
+ givenName: 'John',
181
+ identifiers: mockOpenmrsId,
182
+ isDead: false,
183
+ middleName: 'Test',
184
+ monthsEstimated: 0,
185
+ patientUuid: mockPatient.uuid,
186
+ relationships: [],
187
+ telephoneNumber: '',
188
+ yearsEstimated: 0,
189
+ deathTime: '',
190
+ deathTimeFormat: 'AM' as const,
191
+ nonCodedCauseOfDeath: '',
192
+ };
193
+
194
+ const initialContextValues: PatientRegistrationContextProps = {
195
+ currentPhoto: null,
196
+ inEditMode: false,
197
+ identifierTypes: [],
198
+ initialFormValues: mockInitialFormValues,
199
+ isOffline: false,
200
+ setCapturePhotoProps: jest.fn(),
201
+ setFieldValue: jest.fn(),
202
+ setInitialFormValues: jest.fn(),
203
+ validationSchema: null,
204
+ values: mockInitialFormValues,
205
+ setFieldTouched: jest.fn(),
206
+ };
207
+
208
+ describe('ObsField', () => {
209
+ beforeEach(() => {
210
+ mockUseConfig.mockReturnValue({
211
+ ...getDefaultsFromConfigSchema(esmPatientRegistrationSchema),
212
+ registrationObs: { encounterTypeUuid: 'reg-enc-uuid' },
213
+ } as RegistrationConfig);
214
+ mockUseConcept.mockImplementation(useConceptMockImpl);
215
+ mockUseConceptAnswers.mockImplementation(useConceptAnswersMockImpl);
216
+ });
217
+
218
+ it("logs an error and doesn't render if no registration encounter type is provided", () => {
219
+ mockUseConfig.mockReturnValue({
220
+ ...getDefaultsFromConfigSchema(esmPatientRegistrationSchema),
221
+ registrationObs: { encounterTypeUuid: null },
222
+ } as RegistrationConfig);
223
+
224
+ console.error = jest.fn();
225
+ render(<ObsField fieldDefinition={textFieldDef} />);
226
+ expect(console.error).toHaveBeenCalledWith(
227
+ expect.stringMatching(/no registration encounter type has been configured/i),
228
+ );
229
+ expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
230
+ });
231
+
232
+ it('renders a text box for text concept', () => {
233
+ render(<ObsField fieldDefinition={textFieldDef} />);
234
+
235
+ expect(screen.getByRole('textbox', { name: 'Chief complaint (optional)' })).toBeInTheDocument();
236
+ });
237
+
238
+ it('renders a number box for number concept', () => {
239
+ render(<ObsField fieldDefinition={numberFieldDef} />);
240
+
241
+ expect(screen.getByRole('spinbutton', { name: 'Weight (optional)' })).toBeInTheDocument();
242
+ });
243
+
244
+ it('renders a datepicker for date concept', async () => {
245
+ const user = userEvent.setup();
246
+ render(
247
+ <PatientRegistrationContextProvider value={initialContextValues}>
248
+ <ObsField fieldDefinition={dateFieldDef} />
249
+ </PatientRegistrationContextProvider>,
250
+ );
251
+
252
+ expect(screen.getByText(/vaccination date/i)).toBeInTheDocument();
253
+
254
+ const dateInput = screen.getByLabelText(/vaccination date/i);
255
+ expect(dateInput).toBeInTheDocument();
256
+ await user.clear(dateInput);
257
+ await user.type(dateInput, '28/05/2024');
258
+ // FIXME: Make the date input work
259
+ // expect(dateInput).toHaveValue('28/05/2024');
260
+ });
261
+
262
+ it('renders a select for a coded concept', () => {
263
+ render(<ObsField fieldDefinition={codedFieldDef} />);
264
+
265
+ expect(screen.getByRole('combobox', { name: 'Nationality' })).toBeInTheDocument();
266
+ expect(screen.getByRole('option', { name: 'USA' })).toBeInTheDocument();
267
+ expect(screen.getByRole('option', { name: 'Mexico' })).toBeInTheDocument();
268
+ });
269
+
270
+ it('select uses answerConcept for answers when it is provided', async () => {
271
+ render(<ObsField fieldDefinition={{ ...codedFieldDef, answerConceptSetUuid: 'other-countries-uuid' }} />);
272
+
273
+ expect(screen.getByRole('combobox', { name: 'Nationality' })).toBeInTheDocument();
274
+ expect(screen.getByRole('option', { name: 'Kenya' })).toBeInTheDocument();
275
+ expect(screen.getByRole('option', { name: 'Uganda' })).toBeInTheDocument();
276
+ expect(screen.queryByRole('option', { name: 'USA' })).not.toBeInTheDocument();
277
+ expect(screen.queryByRole('option', { name: 'Mexico' })).not.toBeInTheDocument();
278
+ });
279
+
280
+ it('select uses customConceptAnswers for answers when provided', async () => {
281
+ render(
282
+ <ObsField
283
+ fieldDefinition={{
284
+ ...codedFieldDef,
285
+ customConceptAnswers: [
286
+ {
287
+ uuid: 'mozambique-uuid',
288
+ label: 'Mozambique',
289
+ },
290
+ ],
291
+ }}
292
+ />,
293
+ );
294
+
295
+ expect(screen.getByRole('combobox', { name: 'Nationality' })).toBeInTheDocument();
296
+ expect(screen.getByRole('option', { name: 'Mozambique' })).toBeInTheDocument();
297
+ expect(screen.queryByRole('option', { name: 'Uganda' })).not.toBeInTheDocument();
298
+ });
299
+ });