@openmrs/esm-form-engine-lib 3.1.5-pre.2075 → 3.1.5-pre.2082

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-form-engine-lib",
3
- "version": "3.1.5-pre.2075",
3
+ "version": "3.1.5-pre.2082",
4
4
  "description": "React Form Engine for O3",
5
5
  "browser": "dist/openmrs-esm-form-engine-lib.js",
6
6
  "main": "src/index.ts",
@@ -5,8 +5,21 @@ describe('shouldRenderField', () => {
5
5
  const sessionMode = 'embedded-view';
6
6
  const isTransient = true;
7
7
  const isEmpty = true;
8
+ const hideUnansweredQuestionsInReadonlyForms = false;
9
+
10
+ const result = shouldRenderField(sessionMode, isTransient, isEmpty, hideUnansweredQuestionsInReadonlyForms);
11
+
12
+ expect(result).toBe(false);
13
+ });
14
+
15
+ it('should return false for non-transient empty fields when hideUnansweredQuestionsInReadonlyForms is true in embedded-view mode', () => {
16
+ const sessionMode = 'embedded-view';
17
+ const isTransient = false;
18
+ const isEmpty = true;
19
+ const hideUnansweredQuestionsInReadonlyForms = true;
20
+
21
+ const result = shouldRenderField(sessionMode, isTransient, isEmpty, hideUnansweredQuestionsInReadonlyForms);
8
22
 
9
- const result = shouldRenderField(sessionMode, isTransient, isEmpty);
10
23
  expect(result).toBe(false);
11
24
  });
12
25
 
@@ -14,17 +27,32 @@ describe('shouldRenderField', () => {
14
27
  const sessionMode = 'embedded-view';
15
28
  const isTransient = true;
16
29
  const isEmpty = false;
30
+ const hideUnansweredQuestionsInReadonlyForms = true;
31
+
32
+ const result = shouldRenderField(sessionMode, isTransient, isEmpty, hideUnansweredQuestionsInReadonlyForms);
17
33
 
18
- const result = shouldRenderField(sessionMode, isTransient, isEmpty);
19
34
  expect(result).toBe(true);
20
35
  });
21
36
 
22
- it('should return true for non-transient fields in embedded-view mode', () => {
37
+ it('should return true for non-transient empty fields when hideUnansweredQuestionsInReadonlyForms is false in embedded-view mode', () => {
23
38
  const sessionMode = 'embedded-view';
24
39
  const isTransient = false;
25
40
  const isEmpty = true;
41
+ const hideUnansweredQuestionsInReadonlyForms = false;
42
+
43
+ const result = shouldRenderField(sessionMode, isTransient, isEmpty, hideUnansweredQuestionsInReadonlyForms);
44
+
45
+ expect(result).toBe(true);
46
+ });
47
+
48
+ it('should return true for non-empty fields in embedded-view mode regardless of flags', () => {
49
+ const sessionMode = 'embedded-view';
50
+ const isTransient = false;
51
+ const isEmpty = false;
52
+ const hideUnansweredQuestionsInReadonlyForms = true;
53
+
54
+ const result = shouldRenderField(sessionMode, isTransient, isEmpty, hideUnansweredQuestionsInReadonlyForms);
26
55
 
27
- const result = shouldRenderField(sessionMode, isTransient, isEmpty);
28
56
  expect(result).toBe(true);
29
57
  });
30
58
 
@@ -32,8 +60,10 @@ describe('shouldRenderField', () => {
32
60
  const sessionMode = 'edit';
33
61
  const isTransient = true;
34
62
  const isEmpty = true;
63
+ const hideUnansweredQuestionsInReadonlyForms = true;
64
+
65
+ const result = shouldRenderField(sessionMode, isTransient, isEmpty, hideUnansweredQuestionsInReadonlyForms);
35
66
 
36
- const result = shouldRenderField(sessionMode, isTransient, isEmpty);
37
67
  expect(result).toBe(true);
38
68
  });
39
69
  });
@@ -2,9 +2,15 @@ import { type SessionMode } from '../../../types';
2
2
 
3
3
  /**
4
4
  * @name shouldRenderField
5
- * @description Determines if a field should be rendered based on the session mode, whether it is transient, and if it is empty.
6
- * - A field will not be rendered in 'embedded-view' mode if it is transient and has no value.
5
+ * @description Returns true if a field should be rendered.
6
+ * A field is hidden in 'embedded-view' mode when it is empty
7
+ * and either transient or `hideUnansweredQuestionsInReadonlyForms` is enabled.
7
8
  */
8
- export function shouldRenderField(sessionMode: SessionMode, isTransient: boolean, isEmpty: boolean): boolean {
9
- return !(sessionMode === 'embedded-view' && isTransient && isEmpty);
9
+ export function shouldRenderField(
10
+ sessionMode: SessionMode,
11
+ isTransient: boolean,
12
+ isEmptyValue: boolean,
13
+ hideUnansweredQuestionsInReadonlyForms: boolean,
14
+ ): boolean {
15
+ return !(sessionMode === 'embedded-view' && isEmptyValue && (isTransient || hideUnansweredQuestionsInReadonlyForms));
10
16
  }
@@ -3,6 +3,7 @@ import { ToastNotification } from '@carbon/react';
3
3
  import { Controller, useWatch } from 'react-hook-form';
4
4
  import { useTranslation } from 'react-i18next';
5
5
  import { ErrorBoundary } from 'react-error-boundary';
6
+ import { useConfig } from '@openmrs/esm-framework';
6
7
  import {
7
8
  type FormField,
8
9
  type FormFieldInputProps,
@@ -38,6 +39,9 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
38
39
  const [warnings, setWarnings] = useState<ValidationResult[]>([]);
39
40
  const [historicalValue, setHistoricalValue] = useState<ValueAndDisplay>(null);
40
41
  const context = useFormProviderContext();
42
+ const { hideUnansweredQuestionsInReadonlyForms } = useConfig({
43
+ externalModuleName: '@openmrs/esm-form-engine-app',
44
+ });
41
45
 
42
46
  const {
43
47
  methods: { control, getValues, getFieldState },
@@ -174,9 +178,16 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
174
178
  );
175
179
  }
176
180
 
177
- // If the field is transient and has no value, we do not render it in embedded view mode.
178
- // This is to prevent transient fields from being displayed in the form when they are not necessarily needed.
179
- if (!shouldRenderField(sessionMode, !!field.questionOptions.isTransient, isEmpty(fieldValue))) {
181
+ // In 'embedded-view' mode, empty fields are hidden if they are transient
182
+ // or if the config flag `hideUnansweredQuestionsInReadonlyForms` is enabled.
183
+ if (
184
+ !shouldRenderField(
185
+ sessionMode,
186
+ !!field.questionOptions.isTransient,
187
+ isEmpty(fieldValue),
188
+ hideUnansweredQuestionsInReadonlyForms,
189
+ )
190
+ ) {
180
191
  return null;
181
192
  }
182
193
 
@@ -1076,9 +1076,12 @@ describe('Form engine component', () => {
1076
1076
  await user.click(addButton);
1077
1077
 
1078
1078
  expect(screen.getByRole('button', { name: /Remove/i })).toBeInTheDocument();
1079
- expect(screen.getAllByRole('radio', { name: /^male$/i }).length).toEqual(2);
1080
- expect(screen.getAllByRole('radio', { name: /^female$/i }).length).toEqual(2);
1081
- expect(screen.getAllByRole('textbox', { name: /date of birth/i }).length).toEqual(2);
1079
+
1080
+ await waitFor(() => {
1081
+ expect(screen.getAllByRole('radio', { name: /^male$/i })).toHaveLength(2);
1082
+ expect(screen.getAllByRole('radio', { name: /female/i })).toHaveLength(2);
1083
+ expect(screen.getAllByRole('textbox', { name: /date of birth/i })).toHaveLength(2);
1084
+ });
1082
1085
  });
1083
1086
 
1084
1087
  it('should test deletion of a group', async () => {