@openmrs/esm-form-engine-lib 4.2.2-pre.2610 → 4.2.2-pre.2615

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.
@@ -25,7 +25,7 @@ const UiSelectExtended = ({ field, errors, warnings, setFieldValue })=>{
25
25
  const [config, setConfig] = useState({});
26
26
  const dataSourceDependentValue = useDataSourceDependentValue(field);
27
27
  const isSearchable = isTrue(field.questionOptions.isSearchable);
28
- const { layoutType, sessionMode, workspaceLayout, methods: { control, getFieldState } } = useFormProviderContext();
28
+ const { layoutType, sessionMode, workspaceLayout, methods: { control, getFieldState, getValues } } = useFormProviderContext();
29
29
  const value = useWatch({
30
30
  control,
31
31
  name: field.id,
@@ -48,23 +48,38 @@ const UiSelectExtended = ({ field, errors, warnings, setFieldValue })=>{
48
48
  items,
49
49
  value
50
50
  ]);
51
- const debouncedSearch = debounce((searchTerm, dataSource)=>{
52
- setIsSearching(true);
53
- dataSource.fetchData(searchTerm, config).then((dataItems)=>{
54
- if (dataItems.length) {
55
- const currentSelectedItem = items.find((item)=>item.uuid == value);
56
- const newItems = dataItems.map((item)=>dataSource.toUuidAndDisplay(item, config));
57
- if (currentSelectedItem && !newItems.some((item)=>item.uuid == currentSelectedItem.uuid)) {
58
- newItems.unshift(currentSelectedItem);
51
+ const searchGeneration = useRef(0);
52
+ const debouncedSearch = useMemo(()=>debounce((searchTerm, dataSource)=>{
53
+ const generation = searchGeneration.current;
54
+ setIsSearching(true);
55
+ dataSource.fetchData(searchTerm, config).then((dataItems)=>{
56
+ if (generation !== searchGeneration.current) {
57
+ return;
59
58
  }
60
- setItems(newItems);
61
- }
62
- setIsSearching(false);
63
- }).catch((err)=>{
64
- console.error(err);
65
- setIsSearching(false);
66
- });
67
- }, 300);
59
+ if (dataItems.length) {
60
+ setItems((currentItems)=>{
61
+ const currentValue = getValues(field.id);
62
+ const currentSelectedItem = currentItems.find((item)=>item.uuid == currentValue);
63
+ const newItems = dataItems.map((item)=>dataSource.toUuidAndDisplay(item, config));
64
+ if (currentSelectedItem && !newItems.some((item)=>item.uuid == currentSelectedItem.uuid)) {
65
+ newItems.unshift(currentSelectedItem);
66
+ }
67
+ return newItems;
68
+ });
69
+ }
70
+ setIsSearching(false);
71
+ }).catch((err)=>{
72
+ if (generation !== searchGeneration.current) {
73
+ return;
74
+ }
75
+ console.error(err);
76
+ setIsSearching(false);
77
+ });
78
+ }, 300), [
79
+ config,
80
+ field.id,
81
+ getValues
82
+ ]);
68
83
  const searchTermHasMatchingItem = useCallback((searchTerm)=>{
69
84
  return items.some((item)=>item.display?.toLowerCase().includes(searchTerm.toLowerCase()));
70
85
  }, [
@@ -110,11 +125,18 @@ const UiSelectExtended = ({ field, errors, warnings, setFieldValue })=>{
110
125
  useEffect(()=>{
111
126
  if (dataSource && isSearchable && !isEmpty(searchTerm) && !searchTermHasMatchingItem(searchTerm)) {
112
127
  debouncedSearch(searchTerm, dataSource);
128
+ } else {
129
+ setIsSearching(false);
113
130
  }
131
+ return ()=>{
132
+ debouncedSearch.cancel();
133
+ searchGeneration.current++;
134
+ };
114
135
  }, [
115
136
  dataSource,
116
137
  searchTerm,
117
- config
138
+ isSearchable,
139
+ debouncedSearch
118
140
  ]);
119
141
  useEffect(()=>{
120
142
  let ignore = false;
@@ -173,6 +195,9 @@ const UiSelectExtended = ({ field, errors, warnings, setFieldValue })=>{
173
195
  selectedItem: selectedItem,
174
196
  placeholder: isSearchable ? t('search', 'Search') + '...' : null,
175
197
  onChange: ({ selectedItem })=>{
198
+ debouncedSearch.cancel();
199
+ searchGeneration.current++;
200
+ setIsSearching(false);
176
201
  isProcessingSelection.current = true;
177
202
  setFieldValue(selectedItem?.uuid);
178
203
  },
@@ -22,12 +22,13 @@ const UnspecifiedField = ({ field, fieldValue, setFieldValue, onAfterChange })=>
22
22
  }, []);
23
23
  useEffect(()=>{
24
24
  if (field.meta.submission?.unspecified && (field.meta.submission.newValue || !isEmpty(fieldValue))) {
25
- setIsUnspecified(false);
26
25
  field.meta.submission.unspecified = false;
27
26
  updateFormField(field);
28
27
  }
28
+ setIsUnspecified(!!field.meta.submission?.unspecified);
29
29
  }, [
30
30
  field.meta?.submission,
31
+ field.meta.submission?.unspecified,
31
32
  fieldValue
32
33
  ]);
33
34
  const handleOnChange = useCallback((value)=>{
@@ -1,4 +1,5 @@
1
- import React, { useEffect, useMemo, useState } from "react";
1
+ import React, { useEffect, useMemo, useRef, useState } from "react";
2
+ import { isEqual } from "lodash-es";
2
3
  import { ToastNotification } from "@carbon/react";
3
4
  import { Controller, useWatch } from "react-hook-form";
4
5
  import { useTranslation } from "react-i18next";
@@ -32,6 +33,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions })=>{
32
33
  console.warn('Failed to load @openmrs/esm-form engine-app config - using hideUnansweredQuestionsInReadonlyForms=false (empty fields will be visible in readonly mode): ', error);
33
34
  }
34
35
  const { methods: { control, getValues, getFieldState }, patient, sessionMode, formFields, formFieldValidators, addInvalidField, removeInvalidField, updateFormField } = context;
36
+ const pendingChange = useRef(null);
35
37
  const fieldValue = useWatch({
36
38
  control,
37
39
  name: fieldId,
@@ -77,7 +79,10 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions })=>{
77
79
  if (!isEmpty(fieldValue) && !submission?.newValue && !isDirty && !unspecified && (calculate?.calculateExpression || defaultValue)) {
78
80
  valueAdapter.transformFieldValue(field, fieldValue, context);
79
81
  }
80
- if (isDirty || isTouched) {
82
+ // Controls already process changes synchronously; the watcher also handles external updates.
83
+ const processedChange = pendingChange.current;
84
+ pendingChange.current = null;
85
+ if ((isDirty || isTouched) && (!processedChange || !isEqual(processedChange.value, fieldValue))) {
81
86
  onAfterChange(fieldValue);
82
87
  }
83
88
  }, [
@@ -98,6 +103,9 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions })=>{
98
103
  field.meta.submission
99
104
  ]);
100
105
  const onAfterChange = (value)=>{
106
+ if (field.meta.submission?.unspecified && !isEmpty(value)) {
107
+ field.meta.submission.unspecified = false;
108
+ }
101
109
  const { errors: validationErrors, warnings: validationWarnings } = validateFieldValue(field, value, formFieldValidators, {
102
110
  formFields: formFields,
103
111
  values: getValues(),
@@ -136,6 +144,12 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions })=>{
136
144
  }
137
145
  }
138
146
  };
147
+ const handleAfterChange = (value)=>{
148
+ pendingChange.current = {
149
+ value
150
+ };
151
+ onAfterChange(value);
152
+ };
139
153
  if (!inputComponentWrapper) {
140
154
  return null;
141
155
  }
@@ -169,7 +183,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions })=>{
169
183
  warnings: warnings,
170
184
  setFieldValue: (val)=>{
171
185
  onChange(val);
172
- onAfterChange(val);
186
+ handleAfterChange(val);
173
187
  onBlur();
174
188
  }
175
189
  }), isUnspecifiedSupported(field) && /*#__PURE__*/ React.createElement("div", {
@@ -178,13 +192,13 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions })=>{
178
192
  key: `${field.id}-unspecified`,
179
193
  field: field,
180
194
  setFieldValue: onChange,
181
- onAfterChange: onAfterChange,
195
+ onAfterChange: handleAfterChange,
182
196
  fieldValue: value
183
197
  })), historicalValue?.value && /*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement(PreviousValueReview, {
184
198
  key: `${field.id}-previous-value-review`,
185
199
  previousValue: historicalValue.value,
186
200
  displayText: historicalValue.display,
187
- onAfterChange: onAfterChange,
201
+ onAfterChange: handleAfterChange,
188
202
  field: field
189
203
  })))
190
204
  }));
@@ -1 +1 @@
1
- {"version":3,"file":"ui-select-extended.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/inputs/ui-select-extended/ui-select-extended.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4D,MAAM,OAAO,CAAC;AAYjF,OAAO,EAAmB,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAO3E,QAAA,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAiMnD,CAAC;AAEF,eAAe,gBAAgB,CAAC;AAEhC,wBAAgB,WAAW,CAAC,IAAI,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAE9E"}
1
+ {"version":3,"file":"ui-select-extended.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/inputs/ui-select-extended/ui-select-extended.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4D,MAAM,OAAO,CAAC;AAYjF,OAAO,EAAmB,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAO3E,QAAA,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CA0NnD,CAAC;AAEF,eAAe,gBAAgB,CAAC;AAEhC,wBAAgB,WAAW,CAAC,IAAI,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAE9E"}
@@ -1 +1 @@
1
- {"version":3,"file":"form-field-renderer.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/renderer/field/form-field-renderer.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAuC,MAAM,OAAO,CAAC;AAM5D,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,qBAAqB,EAC1B,KAAK,UAAU,EAGhB,MAAM,gBAAgB,CAAC;AAYxB,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,qBAAqB,CAAC;IACpC,aAAa,CAAC,EAAE;QACd,eAAe,EAAE,UAAU,CAAC;KAC7B,CAAC;CACH;AAED,eAAO,MAAM,iBAAiB,GAAI,0CAA0C,sBAAsB,sBA4NjG,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAAE,KAAK,EAAE;;CAAA,qBAetC;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,SAAS,WAWzD;AAED,wBAAgB,YAAY,CAAC,SAAS,EAAE,UAAU,sCAEjD"}
1
+ {"version":3,"file":"form-field-renderer.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/renderer/field/form-field-renderer.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+C,MAAM,OAAO,CAAC;AAOpE,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,qBAAqB,EAC1B,KAAK,UAAU,EAGhB,MAAM,gBAAgB,CAAC;AAYxB,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,qBAAqB,CAAC;IACpC,aAAa,CAAC,EAAE;QACd,eAAe,EAAE,UAAU,CAAC;KAC7B,CAAC;CACH;AAED,eAAO,MAAM,iBAAiB,GAAI,0CAA0C,sBAAsB,sBAwOjG,CAAC;AAEF,wBAAgB,aAAa,CAAC,EAAE,KAAK,EAAE;;CAAA,qBAetC;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,SAAS,WAWzD;AAED,wBAAgB,YAAY,CAAC,SAAS,EAAE,UAAU,sCAEjD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-form-engine-lib",
3
- "version": "4.2.2-pre.2610",
3
+ "version": "4.2.2-pre.2615",
4
4
  "description": "React Form Engine for O3",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -32,7 +32,7 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
32
32
  layoutType,
33
33
  sessionMode,
34
34
  workspaceLayout,
35
- methods: { control, getFieldState },
35
+ methods: { control, getFieldState, getValues },
36
36
  } = useFormProviderContext();
37
37
 
38
38
  const value = useWatch({ control, name: field.id, exact: true });
@@ -47,27 +47,43 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
47
47
 
48
48
  const selectedItem = useMemo(() => items.find((item) => item.uuid == value) || null, [items, value]);
49
49
 
50
- const debouncedSearch = debounce((searchTerm: string, dataSource: DataSource<OpenmrsResource>) => {
51
- setIsSearching(true);
52
-
53
- dataSource
54
- .fetchData(searchTerm, config)
55
- .then((dataItems) => {
56
- if (dataItems.length) {
57
- const currentSelectedItem = items.find((item) => item.uuid == value);
58
- const newItems = dataItems.map((item) => dataSource.toUuidAndDisplay(item, config));
59
- if (currentSelectedItem && !newItems.some((item) => item.uuid == currentSelectedItem.uuid)) {
60
- newItems.unshift(currentSelectedItem);
61
- }
62
- setItems(newItems);
63
- }
64
- setIsSearching(false);
65
- })
66
- .catch((err) => {
67
- console.error(err);
68
- setIsSearching(false);
69
- });
70
- }, 300);
50
+ const searchGeneration = useRef(0);
51
+
52
+ const debouncedSearch = useMemo(
53
+ () =>
54
+ debounce((searchTerm: string, dataSource: DataSource<OpenmrsResource>) => {
55
+ const generation = searchGeneration.current;
56
+ setIsSearching(true);
57
+
58
+ dataSource
59
+ .fetchData(searchTerm, config)
60
+ .then((dataItems) => {
61
+ if (generation !== searchGeneration.current) {
62
+ return;
63
+ }
64
+ if (dataItems.length) {
65
+ setItems((currentItems) => {
66
+ const currentValue = getValues(field.id);
67
+ const currentSelectedItem = currentItems.find((item) => item.uuid == currentValue);
68
+ const newItems = dataItems.map((item) => dataSource.toUuidAndDisplay(item, config));
69
+ if (currentSelectedItem && !newItems.some((item) => item.uuid == currentSelectedItem.uuid)) {
70
+ newItems.unshift(currentSelectedItem);
71
+ }
72
+ return newItems;
73
+ });
74
+ }
75
+ setIsSearching(false);
76
+ })
77
+ .catch((err) => {
78
+ if (generation !== searchGeneration.current) {
79
+ return;
80
+ }
81
+ console.error(err);
82
+ setIsSearching(false);
83
+ });
84
+ }, 300),
85
+ [config, field.id, getValues],
86
+ );
71
87
 
72
88
  const searchTermHasMatchingItem = useCallback(
73
89
  (searchTerm: string) => {
@@ -119,8 +135,14 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
119
135
  useEffect(() => {
120
136
  if (dataSource && isSearchable && !isEmpty(searchTerm) && !searchTermHasMatchingItem(searchTerm)) {
121
137
  debouncedSearch(searchTerm, dataSource);
138
+ } else {
139
+ setIsSearching(false);
122
140
  }
123
- }, [dataSource, searchTerm, config]);
141
+ return () => {
142
+ debouncedSearch.cancel();
143
+ searchGeneration.current++;
144
+ };
145
+ }, [dataSource, searchTerm, isSearchable, debouncedSearch]);
124
146
 
125
147
  useEffect(() => {
126
148
  let ignore = false;
@@ -177,6 +199,9 @@ const UiSelectExtended: React.FC<FormFieldInputProps> = ({ field, errors, warnin
177
199
  selectedItem={selectedItem}
178
200
  placeholder={isSearchable ? t('search', 'Search') + '...' : null}
179
201
  onChange={({ selectedItem }) => {
202
+ debouncedSearch.cancel();
203
+ searchGeneration.current++;
204
+ setIsSearching(false);
180
205
  isProcessingSelection.current = true;
181
206
  setFieldValue(selectedItem?.uuid);
182
207
  }}
@@ -34,11 +34,11 @@ const UnspecifiedField: React.FC<UnspecifiedFieldProps> = ({ field, fieldValue,
34
34
 
35
35
  useEffect(() => {
36
36
  if (field.meta.submission?.unspecified && (field.meta.submission.newValue || !isEmpty(fieldValue))) {
37
- setIsUnspecified(false);
38
37
  field.meta.submission.unspecified = false;
39
38
  updateFormField(field);
40
39
  }
41
- }, [field.meta?.submission, fieldValue]);
40
+ setIsUnspecified(!!field.meta.submission?.unspecified);
41
+ }, [field.meta?.submission, field.meta.submission?.unspecified, fieldValue]);
42
42
 
43
43
  const handleOnChange = useCallback(
44
44
  (value) => {
@@ -1,4 +1,5 @@
1
- import React, { useEffect, useMemo, useState } from 'react';
1
+ import React, { useEffect, useMemo, useRef, useState } from 'react';
2
+ import { isEqual } from 'lodash-es';
2
3
  import { ToastNotification } from '@carbon/react';
3
4
  import { Controller, useWatch } from 'react-hook-form';
4
5
  import { useTranslation } from 'react-i18next';
@@ -66,6 +67,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
66
67
  updateFormField,
67
68
  } = context;
68
69
 
70
+ const pendingChange = useRef<{ value: unknown } | null>(null);
69
71
  const fieldValue = useWatch({ control, name: fieldId, exact: true });
70
72
  const noop = () => {};
71
73
 
@@ -109,7 +111,10 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
109
111
  ) {
110
112
  valueAdapter.transformFieldValue(field, fieldValue, context);
111
113
  }
112
- if (isDirty || isTouched) {
114
+ // Controls already process changes synchronously; the watcher also handles external updates.
115
+ const processedChange = pendingChange.current;
116
+ pendingChange.current = null;
117
+ if ((isDirty || isTouched) && (!processedChange || !isEqual(processedChange.value, fieldValue))) {
113
118
  onAfterChange(fieldValue);
114
119
  }
115
120
  }, [fieldValue]);
@@ -128,6 +133,9 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
128
133
  }, [field.meta.submission]);
129
134
 
130
135
  const onAfterChange = (value: any) => {
136
+ if (field.meta.submission?.unspecified && !isEmpty(value)) {
137
+ field.meta.submission.unspecified = false;
138
+ }
131
139
  const { errors: validationErrors, warnings: validationWarnings } = validateFieldValue(
132
140
  field,
133
141
  value,
@@ -171,6 +179,11 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
171
179
  }
172
180
  };
173
181
 
182
+ const handleAfterChange = (value: unknown) => {
183
+ pendingChange.current = { value };
184
+ onAfterChange(value);
185
+ };
186
+
174
187
  if (!inputComponentWrapper) {
175
188
  return null;
176
189
  }
@@ -218,7 +231,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
218
231
  warnings={warnings}
219
232
  setFieldValue={(val) => {
220
233
  onChange(val);
221
- onAfterChange(val);
234
+ handleAfterChange(val);
222
235
  onBlur();
223
236
  }}
224
237
  />
@@ -229,7 +242,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
229
242
  key={`${field.id}-unspecified`}
230
243
  field={field}
231
244
  setFieldValue={onChange}
232
- onAfterChange={onAfterChange}
245
+ onAfterChange={handleAfterChange}
233
246
  fieldValue={value}
234
247
  />
235
248
  )}
@@ -241,7 +254,7 @@ export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: Form
241
254
  key={`${field.id}-previous-value-review`}
242
255
  previousValue={historicalValue.value}
243
256
  displayText={historicalValue.display}
244
- onAfterChange={onAfterChange}
257
+ onAfterChange={handleAfterChange}
245
258
  field={field}
246
259
  />
247
260
  </div>