@openmrs/esm-form-engine-lib 3.0.0-pre.1636 → 3.0.0-pre.1638

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.
@@ -1,137 +1,201 @@
1
1
  {
2
- "name": "ObsGroup Test Form",
3
- "version": "1",
4
- "published": true,
5
- "retired": false,
6
- "pages": [
7
- {
8
- "label": "Introduction",
9
- "sections": [
10
- {
11
- "label": "",
12
- "isExpanded": "true",
13
- "questions": [
14
- {
15
- "type": "markdown",
16
- "questionOptions": {
17
- "rendering": "markdown"
18
- },
19
- "id": "fooMarkdown",
20
- "value": [
21
- "**Use this form to:** Test Obs Group behaviour"
22
- ]
23
- }
24
- ]
25
- }
26
- ]
27
- },
28
- {
29
- "label": "Obs Group Page",
30
- "sections": [
31
- {
32
- "label": "Group Section",
33
- "isExpanded": "true",
34
- "questions": [
35
- {
36
- "id": "myGroup",
37
- "label": "My Group",
38
- "type": "obsGroup",
39
- "questionOptions": {
40
- "rendering": "repeating",
41
- "concept": "1c70c490-cafa-4c95-9fdd-a30b62bb78b8"
2
+ "name": "ObsGroup Test Form",
3
+ "version": "1",
4
+ "published": true,
5
+ "retired": false,
6
+ "pages": [
7
+ {
8
+ "label": "Obs Group Page",
9
+ "sections": [
10
+ {
11
+ "label": "Group Section",
12
+ "isExpanded": "true",
13
+ "questions": [
14
+ {
15
+ "id": "myGroup",
16
+ "label": "My Group",
17
+ "type": "obsGroup",
18
+ "questionOptions": {
19
+ "rendering": "repeating",
20
+ "concept": "1c70c490-cafa-4c95-9fdd-a30b62bb78b8"
21
+ },
22
+ "behaviours": [
23
+ {
24
+ "intent": "*",
25
+ "required": "false",
26
+ "unspecified": "false",
27
+ "hide": {
28
+ "hideWhenExpression": ""
29
+ },
30
+ "validators": []
31
+ }
32
+ ],
33
+ "questions": [
34
+ {
35
+ "label": "Sex",
36
+ "type": "obs",
37
+ "required": "true",
38
+ "questionOptions": {
39
+ "rendering": "radio",
40
+ "concept": "1587AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
41
+ "answers": [
42
+ {
43
+ "concept": "1535AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
44
+ "label": "Female"
45
+ },
46
+ {
47
+ "concept": "1534AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
48
+ "label": "Male"
49
+ }
50
+ ]
51
+ },
52
+ "id": "childSex",
53
+ "behaviours": [
54
+ {
55
+ "intent": "*",
56
+ "required": "true",
57
+ "unspecified": "true",
58
+ "hide": {
59
+ "hideWhenExpression": "false"
60
+ },
61
+ "validators": []
62
+ }
63
+ ]
42
64
  },
43
- "behaviours":[
44
- {
45
- "intent":"*",
46
- "required":"false",
47
- "unspecified":"false",
48
- "hide":{
49
- "hideWhenExpression":""
50
- },
51
- "validators":[]
52
- }
53
- ],
54
- "questions": [
55
- {
56
- "label": "Sex",
57
- "type":"obs",
58
- "required":"true",
59
- "questionOptions":{
60
- "rendering":"radio",
61
- "concept":"1587AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
62
- "answers":[
65
+ {
66
+ "label": "Date of Birth",
67
+ "type": "obs",
68
+ "questionOptions": {
69
+ "rendering": "date",
70
+ "concept": "164802AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
71
+ "weeksList": ""
72
+ },
73
+ "id": "birthDate",
74
+ "behaviours": [
75
+ {
76
+ "intent": "*",
77
+ "required": "true",
78
+ "unspecified": "true",
79
+ "hide": {
80
+ "hideWhenExpression": "false"
81
+ },
82
+ "validators": [
63
83
  {
64
- "concept":"1535AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
65
- "label":"Female"
84
+ "type": "date",
85
+ "allowFutureDates": "false"
66
86
  },
67
87
  {
68
- "concept":"1534AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
69
- "label":"Male"
88
+ "type": "js_expression",
89
+ "failsWhenExpression": "!isDateEqualTo(myValue, useFieldValue('visit_date'))",
90
+ "message": "Child birth date should be the same as the visit date!"
70
91
  }
71
92
  ]
72
- },
73
- "id":"childSex",
74
- "behaviours":[
75
- {
76
- "intent":"*",
77
- "required":"true",
78
- "unspecified":"true",
79
- "hide":{
80
- "hideWhenExpression":"false"
81
- },
82
- "validators":[]
83
- }
84
- ]
93
+ }
94
+ ]
95
+ }
96
+ ]
97
+ }
98
+ ]
99
+ },
100
+ {
101
+ "label": "Screening Section",
102
+ "questions": [
103
+ {
104
+ "label": "Do you have dependents?",
105
+ "id": "hasDependents",
106
+ "type": "obs",
107
+ "questionOptions": {
108
+ "rendering": "radio",
109
+ "concept": "a89e3f94-1350-11df-a1f1-0026b9348837",
110
+ "answers": [
111
+ {
112
+ "concept": "a89ce50e-1350-11df-a1f1-0026b9348839",
113
+ "label": "Yes"
85
114
  },
86
115
  {
87
- "label": "Date of Birth",
88
- "type": "obs",
89
- "questionOptions": {
90
- "rendering": "date",
91
- "concept": "164802AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
92
- "weeksList": ""
93
- },
94
- "id": "birthDate",
95
- "behaviours": [
116
+ "concept": "a89ce50e-1350-11df-a1f1-0026b9348838",
117
+ "label": "No"
118
+ }
119
+ ]
120
+ }
121
+ }
122
+ ]
123
+ },
124
+ {
125
+ "label": "Groups with Hide logic",
126
+ "questions": [
127
+ {
128
+ "type": "obsGroup",
129
+ "label": "Dependents Group",
130
+ "id": "dependentGroup",
131
+ "questionOptions": {
132
+ "concept": "3665d0ef-3718-47b2-9091-8b685bda412d",
133
+ "rendering": "group"
134
+ },
135
+ "questions": [
136
+ {
137
+ "label": "Dependent Type",
138
+ "id": "dependentType",
139
+ "type": "obs",
140
+ "questionOptions": {
141
+ "rendering": "radio",
142
+ "concept": "a89e3f94-1350-11df-a1f1-0026b9348838",
143
+ "answers": [
96
144
  {
97
- "intent": "*",
98
- "required": "true",
99
- "unspecified": "true",
100
- "hide": {
101
- "hideWhenExpression": "false"
102
- },
103
- "validators": [
104
- {
105
- "type": "date",
106
- "allowFutureDates": "false"
107
- },
108
- {
109
- "type": "js_expression",
110
- "failsWhenExpression": "!isDateEqualTo(myValue, useFieldValue('visit_date'))",
111
- "message": "Child birth date should be the same as the visit date!"
112
- }
113
- ]
145
+ "concept": "6daff4ce-bce7-41f5-9141-17e694155180",
146
+ "label": "Child"
147
+ },
148
+ {
149
+ "concept": "a89ce50e-1350-11df-a1f1-0026b9348838",
150
+ "label": "Spouse"
114
151
  }
115
152
  ]
153
+ },
154
+ "hide": {
155
+ "hideWhenExpression": "hasDependents !== 'a89ce50e-1350-11df-a1f1-0026b9348839'"
116
156
  }
117
- ]
118
- }
119
- ]
120
- }
121
- ]
122
- }
123
- ],
124
- "availableIntents": [
125
- {
126
- "intent": "*",
127
- "display": "ObsGroup Test Form"
128
- }
129
- ],
130
- "processor": "EncounterFormProcessor",
131
- "uuid": "8f713e0e-94a0-3c57-9024-69520933802a",
132
- "referencedForms": [],
133
- "encounterType": "7e54cd64-f9c3-11eb-8e6a-57478ce139b0",
134
- "encounter": "Obs Group Test",
135
- "allowUnspecifiedAll": true
136
- }
137
-
157
+ },
158
+ {
159
+ "label": "Dependent Name",
160
+ "id": "dependentName",
161
+ "type": "obs",
162
+ "questionOptions": {
163
+ "rendering": "text",
164
+ "concept": "a8a06fc6-1350-11df-a1f1-0026b9348838"
165
+ },
166
+ "hide": {
167
+ "hideWhenExpression": "isEmpty(dependentType)"
168
+ }
169
+ },
170
+ {
171
+ "label": "Dependent Age",
172
+ "id": "dependentAge",
173
+ "type": "obs",
174
+ "questionOptions": {
175
+ "rendering": "number",
176
+ "concept": "a8a06fc6-1350-11df-a1f1-0026b9348839"
177
+ },
178
+ "hide": {
179
+ "hideWhenExpression": "dependentType !== '6daff4ce-bce7-41f5-9141-17e694155180'"
180
+ }
181
+ }
182
+ ]
183
+ }
184
+ ]
185
+ }
186
+ ]
187
+ }
188
+ ],
189
+ "availableIntents": [
190
+ {
191
+ "intent": "*",
192
+ "display": "ObsGroup Test Form"
193
+ }
194
+ ],
195
+ "processor": "EncounterFormProcessor",
196
+ "uuid": "8f713e0e-94a0-3c57-9024-69520933802a",
197
+ "referencedForms": [],
198
+ "encounterType": "7e54cd64-f9c3-11eb-8e6a-57478ce139b0",
199
+ "encounter": "Obs Group Test",
200
+ "allowUnspecifiedAll": true
201
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-form-engine-lib",
3
- "version": "3.0.0-pre.1636",
3
+ "version": "3.0.0-pre.1638",
4
4
  "description": "React Form Engine for O3",
5
5
  "browser": "dist/openmrs-esm-form-engine-lib.js",
6
6
  "main": "src/index.ts",
@@ -9,13 +9,13 @@ import { useTranslation } from 'react-i18next';
9
9
 
10
10
  export const ObsGroup: React.FC<FormFieldInputProps> = ({ field, ...restProps }) => {
11
11
  const { t } = useTranslation();
12
- const { formFieldAdapters } = useFormProviderContext();
13
- const showLabel = useMemo(() => field.questions?.length > 1, [field]);
12
+ const { formFieldAdapters, formFields } = useFormProviderContext();
14
13
 
15
14
  const content = useMemo(
16
15
  () =>
17
16
  field.questions
18
- ?.filter((child) => !child.isHidden)
17
+ .map((child) => formFields.find((field) => field.id === child.id))
18
+ .filter((child) => !child.isHidden)
19
19
  .map((child, index) => {
20
20
  const key = `${child.id}_${index}`;
21
21
 
@@ -35,12 +35,12 @@ export const ObsGroup: React.FC<FormFieldInputProps> = ({ field, ...restProps })
35
35
  );
36
36
  }
37
37
  }),
38
- [field],
38
+ [field, formFields],
39
39
  );
40
40
 
41
41
  return (
42
42
  <div className={styles.groupContainer}>
43
- {showLabel ? (
43
+ {content.length > 1 ? (
44
44
  <FormGroup legendText={t(field.label)} className={styles.boldLegend}>
45
45
  {content}
46
46
  </FormGroup>
@@ -1,21 +1,21 @@
1
1
  import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
2
  import { debounce } from 'lodash-es';
3
3
  import { ComboBox, DropdownSkeleton, Layer, InlineLoading } from '@carbon/react';
4
- import { isTrue } from '../../../utils/boolean-utils';
5
4
  import { useTranslation } from 'react-i18next';
6
- import { getRegisteredDataSource } from '../../../registry/registry';
5
+ import { useWatch } from 'react-hook-form';
6
+ import { type OpenmrsResource } from '@openmrs/esm-framework';
7
7
  import { getControlTemplate } from '../../../registry/inbuilt-components/control-templates';
8
- import { type DataSource, type FormFieldInputProps } from '../../../types';
8
+ import { getRegisteredDataSource } from '../../../registry/registry';
9
9
  import { isEmpty } from '../../../validators/form-validator';
10
+ import { isTrue } from '../../../utils/boolean-utils';
11
+ import { isViewMode } from '../../../utils/common-utils';
10
12
  import { shouldUseInlineLayout } from '../../../utils/form-helper';
11
- import FieldValueView from '../../value/view/field-value-view.component';
12
- import styles from './ui-select-extended.scss';
13
+ import { type DataSource, type FormFieldInputProps } from '../../../types';
13
14
  import { useFormProviderContext } from '../../../provider/form-provider';
14
- import FieldLabel from '../../field-label/field-label.component';
15
- import { useWatch } from 'react-hook-form';
16
15
  import useDataSourceDependentValue from '../../../hooks/useDataSourceDependentValue';
17
- import { isViewMode } from '../../../utils/common-utils';
18
- import { type OpenmrsResource } from '@openmrs/esm-framework';
16
+ import FieldLabel from '../../field-label/field-label.component';
17
+ import FieldValueView from '../../value/view/field-value-view.component';
18
+ import styles from './ui-select-extended.scss';
19
19
 
20
20
  const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnings, setFieldValue }) => {
21
21
  const { t } = useTranslation();
@@ -49,6 +49,7 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
49
49
 
50
50
  const debouncedSearch = debounce((searchTerm: string, dataSource: DataSource<OpenmrsResource>) => {
51
51
  setIsSearching(true);
52
+
52
53
  dataSource
53
54
  .fetchData(searchTerm, config)
54
55
  .then((dataItems) => {
@@ -86,22 +87,33 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
86
87
  }, [field.questionOptions?.datasource]);
87
88
 
88
89
  useEffect(() => {
90
+ let ignore = false;
91
+
89
92
  // If not searchable, preload the items
90
93
  if (dataSource && !isTrue(field.questionOptions.isSearchable)) {
91
94
  setItems([]);
92
95
  setIsLoading(true);
96
+
93
97
  dataSource
94
98
  .fetchData(null, { ...config, referencedValue: dataSourceDependentValue })
95
99
  .then((dataItems) => {
96
- setItems(dataItems.map(dataSource.toUuidAndDisplay));
97
- setIsLoading(false);
100
+ if (!ignore) {
101
+ setItems(dataItems.map(dataSource.toUuidAndDisplay));
102
+ setIsLoading(false);
103
+ }
98
104
  })
99
105
  .catch((err) => {
100
- console.error(err);
101
- setIsLoading(false);
102
- setItems([]);
106
+ if (!ignore) {
107
+ console.error(err);
108
+ setIsLoading(false);
109
+ setItems([]);
110
+ }
103
111
  });
104
112
  }
113
+
114
+ return () => {
115
+ ignore = true;
116
+ };
105
117
  }, [dataSource, config, dataSourceDependentValue]);
106
118
 
107
119
  useEffect(() => {
@@ -111,19 +123,28 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
111
123
  }, [dataSource, searchTerm, config]);
112
124
 
113
125
  useEffect(() => {
126
+ let ignore = false;
114
127
  if (value && !isDirty && dataSource && isSearchable && sessionMode !== 'enter' && !items.length) {
115
128
  // While in edit mode, search-based instances should fetch the initial item (previously selected value) to resolve its display property
116
129
  setIsLoading(true);
117
130
  try {
118
131
  dataSource.fetchSingleItem(value).then((item) => {
119
- setItems([dataSource.toUuidAndDisplay(item)]);
120
- setIsLoading(false);
132
+ if (!ignore) {
133
+ setItems([dataSource.toUuidAndDisplay(item)]);
134
+ setIsLoading(false);
135
+ }
121
136
  });
122
137
  } catch (error) {
123
- console.error(error);
124
- setIsLoading(false);
138
+ if (!ignore) {
139
+ console.error(error);
140
+ setIsLoading(false);
141
+ }
125
142
  }
126
143
  }
144
+
145
+ return () => {
146
+ ignore = true;
147
+ };
127
148
  }, [value, isDirty, sessionMode, dataSource, isSearchable, items]);
128
149
 
129
150
  if (isLoading) {
@@ -876,6 +876,50 @@ describe('Form engine component', () => {
876
876
  });
877
877
 
878
878
  describe('Obs group', () => {
879
+ it('should not render empty obs group', async () => {
880
+ await act(async () => {
881
+ renderForm(null, obsGroupTestForm);
882
+ });
883
+
884
+ // Check that only one obs group is initially rendered
885
+ const initialGroups = screen.getAllByRole('group', { name: /My Group|Dependents Group/i });
886
+ expect(initialGroups.length).toBe(1);
887
+ const dependentTypeRadios = screen.queryAllByRole('radio', { name: /child|spouse/i });
888
+ expect(dependentTypeRadios.length).toBe(0);
889
+
890
+ // Select "Yes" for having dependents
891
+ const yesRadio = screen.getByRole('radio', { name: /yes/i });
892
+ await user.click(yesRadio);
893
+
894
+ // Now the dependent type radios should be visible
895
+ const visibleDependentTypeRadios = screen.getAllByRole('radio', { name: /child|spouse/i });
896
+ expect(visibleDependentTypeRadios.length).toBe(2);
897
+
898
+ // Check that the group label is still hidden since it only has one visible field
899
+ const dependentsGroupResults = screen.queryAllByRole('group', { name: /Dependents Group/i });
900
+ expect(dependentsGroupResults.length).toBe(0);
901
+
902
+ // Check that dependent name and age are still hidden
903
+ const hiddenDependentNameInput = screen.queryByRole('textbox', { name: /dependent name/i });
904
+ const hiddenDependentAgeInput = screen.queryByRole('spinbutton', { name: /dependent age/i });
905
+ expect(hiddenDependentNameInput).toBeNull();
906
+ expect(hiddenDependentAgeInput).toBeNull();
907
+
908
+ // Select "Child" as dependent type
909
+ await user.click(visibleDependentTypeRadios[0]);
910
+
911
+ // Check the visibility of the group label
912
+ const dependentsGroup = screen.getAllByRole('group', { name: /Dependents Group/i })[0];
913
+ expect(dependentsGroup).toBeInTheDocument();
914
+
915
+ // Check that dependent name and age are now visible
916
+ const dependentNameInput = screen.getByRole('textbox', { name: /dependent name/i });
917
+ const dependentAgeInput = screen.getByRole('spinbutton', { name: /dependent age/i });
918
+
919
+ expect(dependentNameInput).toBeInTheDocument();
920
+ expect(dependentAgeInput).toBeInTheDocument();
921
+ });
922
+
879
923
  it('should save obs group on form submission', async () => {
880
924
  const saveEncounterMock = jest.spyOn(api, 'saveEncounter');
881
925
  await act(async () => {
@@ -11,8 +11,12 @@ export function useEncounter(formJson: FormSchema) {
11
11
  const [error, setError] = useState(null);
12
12
 
13
13
  useEffect(() => {
14
+ const abortController = new AbortController();
15
+
14
16
  if (!isEmpty(formJson.encounter) && isString(formJson.encounter)) {
15
- openmrsFetch(`${restBaseUrl}/encounter/${formJson.encounter}?v=${encounterRepresentation}`)
17
+ openmrsFetch(`${restBaseUrl}/encounter/${formJson.encounter}?v=${encounterRepresentation}`, {
18
+ signal: abortController.signal,
19
+ })
16
20
  .then((response) => {
17
21
  setEncounter(response.data);
18
22
  setIsLoading(false);
@@ -26,6 +30,10 @@ export function useEncounter(formJson: FormSchema) {
26
30
  } else {
27
31
  setIsLoading(false);
28
32
  }
33
+
34
+ return () => {
35
+ abortController.abort();
36
+ };
29
37
  }, [formJson.encounter]);
30
38
 
31
39
  return { encounter: encounter, error, isLoading };
@@ -11,6 +11,8 @@ export function useFormJson(formUuid: string, rawFormJson: any, encounterUuid: s
11
11
  const [error, setError] = useState(validateFormsArgs(formUuid, rawFormJson));
12
12
 
13
13
  useEffect(() => {
14
+ const abortController = new AbortController();
15
+
14
16
  const setFormJsonWithTranslations = (formJson: FormSchema) => {
15
17
  if (formJson?.translations) {
16
18
  const language = window.i18next.language;
@@ -24,9 +26,15 @@ export function useFormJson(formUuid: string, rawFormJson: any, encounterUuid: s
24
26
  setFormJsonWithTranslations({ ...formJson, encounter: encounterUuid });
25
27
  })
26
28
  .catch((error) => {
27
- console.error(error);
28
- setError(new Error('Error loading form JSON: ' + error.message));
29
+ if (error.name !== 'AbortError') {
30
+ console.error(error);
31
+ setError(new Error('Error loading form JSON: ' + error.message));
32
+ }
29
33
  });
34
+
35
+ return () => {
36
+ abortController.abort();
37
+ };
30
38
  }, [formSessionIntent, formUuid, rawFormJson, encounterUuid]);
31
39
 
32
40
  return {
@@ -9,19 +9,29 @@ export const usePatientPrograms = (patientUuid: string, formJson: FormSchema) =>
9
9
  const [error, setError] = useState(null);
10
10
 
11
11
  useEffect(() => {
12
+ const abortController = new AbortController();
13
+
12
14
  if (formJson.meta?.programs?.hasProgramFields) {
13
- openmrsFetch(`${restBaseUrl}/programenrollment?patient=${patientUuid}&v=${customRepresentation}`)
15
+ openmrsFetch(`${restBaseUrl}/programenrollment?patient=${patientUuid}&v=${customRepresentation}`, {
16
+ signal: abortController.signal,
17
+ })
14
18
  .then((response) => {
15
19
  setPatientPrograms(response.data.results.filter((enrollment) => enrollment.dateCompleted === null));
16
20
  setIsLoading(false);
17
21
  })
18
22
  .catch((error) => {
19
- setError(error);
20
- setIsLoading(false);
23
+ if (error.name !== 'AbortError') {
24
+ setError(error);
25
+ setIsLoading(false);
26
+ }
21
27
  });
22
28
  } else {
23
29
  setIsLoading(false);
24
30
  }
31
+
32
+ return () => {
33
+ abortController.abort();
34
+ };
25
35
  }, [formJson]);
26
36
 
27
37
  return {
@@ -13,17 +13,27 @@ const useProcessorDependencies = (
13
13
  const { loadDependencies } = formProcessor;
14
14
 
15
15
  useEffect(() => {
16
+ let ignore = false;
17
+
16
18
  if (loadDependencies) {
17
19
  setIsLoading(true);
18
20
  loadDependencies(context, setContext)
19
- .then((results) => {
20
- setIsLoading(false);
21
+ .then(() => {
22
+ if (!ignore) {
23
+ setIsLoading(false);
24
+ }
21
25
  })
22
26
  .catch((error) => {
23
- setError(error);
24
- reportError(error, 'Encountered error while loading dependencies');
27
+ if (!ignore) {
28
+ setError(error);
29
+ reportError(error, 'Encountered error while loading dependencies');
30
+ }
25
31
  });
26
32
  }
33
+
34
+ return () => {
35
+ ignore = true;
36
+ };
27
37
  }, [loadDependencies]);
28
38
 
29
39
  return { isLoading, error };