-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathobs-group.component.tsx
More file actions
65 lines (57 loc) · 2.27 KB
/
obs-group.component.tsx
File metadata and controls
65 lines (57 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { type FormFieldInputProps } from '../../types';
import styles from './obs-group.scss';
import { ErrorFallback, FormFieldRenderer, isGroupField } from '../renderer/field/form-field-renderer.component';
import { useFormProviderContext } from '../../provider/form-provider';
import { FormGroup } from '@carbon/react';
import { useTranslation } from 'react-i18next';
export const ObsGroup: React.FC<FormFieldInputProps> = ({ field, ...restProps }) => {
const { t } = useTranslation();
const { formFieldAdapters, formFields } = useFormProviderContext();
// Get the actual field from formFields to ensure we have the latest evaluated state
const evaluatedField = formFields.find((f) => f.id === field.id) || field;
// If the obsGroup itself is hidden, don't render it at all
const isGroupHidden = evaluatedField.isHidden || evaluatedField.isParentHidden || false;
if (isGroupHidden) {
return null;
}
const content = evaluatedField.questions
.map((child) => formFields.find((field) => field.id === child.id))
.filter((child) => !child.isHidden && !child.isParentHidden)
.map((child, index) => {
const key = `${child.id}_${index}`;
if (child.id === field.id) {
return (
<ErrorFallback error={new Error('ObsGroup child has same id as parent question')}/>
);
}
if (child.type === 'obsGroup' && isGroupField(child.questionOptions.rendering)) {
return (
<div key={key} className={styles.nestedGroupContainer}>
<ObsGroup field={child} {...restProps} />
</div>
);
} else if (formFieldAdapters[child.type]) {
return (
<div className={classNames(styles.flexColumn)} key={key}>
<div className={styles.groupContainer}>
<FormFieldRenderer fieldId={child.id} valueAdapter={formFieldAdapters[child.type]} />
</div>
</div>
);
}
});
return (
<div className={styles.groupContainer}>
{content.length > 1 ? (
<FormGroup legendText={t(evaluatedField.label)} className={styles.boldLegend}>
{content}
</FormGroup>
) : (
content
)}
</div>
);
};
export default ObsGroup;