) {
+ return [...exisingItems, {}];
}
export function RepeatableGroups(props: RepeatableGroupsProps) {
@@ -20,8 +26,14 @@ export function RepeatableGroups(props: RepeatableGroupsProps) {
const { parentPath, questionItem } = groupItem;
const { linkId, required } = questionItem;
const fieldName = [...parentPath, linkId];
- const { value, onChange } = useFieldController(fieldName, questionItem);
+ const { onChange } = useFieldController(fieldName, questionItem);
+
+ const { getValues } = useFormContext();
+
+ const value = _.get(getValues(), fieldName);
+
const items = value.items && value.items.length ? value.items : required ? [{}] : [];
+ const buildValue = props.buildValue ?? defaultBuildValue;
return (
@@ -56,8 +68,7 @@ export function RepeatableGroups(props: RepeatableGroupsProps) {
type="link"
className={s.addButton}
onClick={() => {
- const existingItems = items || [];
- const updatedInput = { items: [...existingItems, {}] };
+ const updatedInput = { items: buildValue(items ?? []) };
onChange(updatedInput);
}}
size="small"
diff --git a/src/components/BaseQuestionnaireResponseForm/widgets/Group/index.tsx b/src/components/BaseQuestionnaireResponseForm/widgets/Group/index.tsx
index 173e98e2..1760d573 100644
--- a/src/components/BaseQuestionnaireResponseForm/widgets/Group/index.tsx
+++ b/src/components/BaseQuestionnaireResponseForm/widgets/Group/index.tsx
@@ -1,4 +1,5 @@
import classNames from 'classnames';
+import _ from 'lodash';
import { GroupItemProps, QuestionItems } from 'sdc-qrf';
import { Text } from 'src/components/Typography';
diff --git a/src/setupTests.ts b/src/setupTests.ts
index 8f4612d8..54fb8a4b 100644
--- a/src/setupTests.ts
+++ b/src/setupTests.ts
@@ -1,5 +1,6 @@
import '@testing-library/jest-dom/extend-expect';
+import { cleanup } from '@testing-library/react';
import {
Consent,
Encounter,
@@ -220,6 +221,8 @@ afterEach(async () => {
data: { query: `select drop_before_all(${txId});` },
});
});
+ cleanup();
+ vi.clearAllMocks();
});
// afterAll(() => {