-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for nested obsgroups
- Loading branch information
1 parent
a26a78c
commit c16113f
Showing
9 changed files
with
420 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,54 @@ | ||
import React from 'react'; | ||
import React, { useMemo } from 'react'; | ||
import classNames from 'classnames'; | ||
import { type FormFieldInputProps } from '../../types'; | ||
import styles from './obs-group.scss'; | ||
import { FormFieldRenderer } from '../renderer/field/form-field-renderer.component'; | ||
import { 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 }) => { | ||
export const ObsGroup: React.FC<FormFieldInputProps> = ({ field, ...restProps }) => { | ||
const { t } = useTranslation(); | ||
const { formFieldAdapters } = useFormProviderContext(); | ||
const showLabel = useMemo(() => field.questions?.length > 1, [field]); | ||
|
||
const groupContent = field.questions | ||
?.filter((child) => !child.isHidden) | ||
.map((child, index) => { | ||
const keyId = child.id + '_' + index; | ||
if (formFieldAdapters[child.type]) { | ||
return ( | ||
<div className={classNames(styles.flexColumn)} key={keyId}> | ||
<div className={styles.groupContainer}> | ||
<FormFieldRenderer fieldId={child.id} valueAdapter={formFieldAdapters[child.type]} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
}); | ||
const content = useMemo( | ||
() => | ||
field.questions | ||
?.filter((child) => !child.isHidden) | ||
.map((child, index) => { | ||
const keyId = `${child.id}_${index}`; | ||
|
||
return <div className={styles.flexRow}>{groupContent}</div>; | ||
if (child.type === 'obsGroup' && isGroupField(child.questionOptions.rendering)) { | ||
return ( | ||
<div key={keyId} className={styles.nestedGroupContainer}> | ||
<ObsGroup field={child} {...restProps} /> | ||
</div> | ||
); | ||
} else if (formFieldAdapters[child.type]) { | ||
return ( | ||
<div className={classNames(styles.flexColumn)} key={keyId}> | ||
<div className={styles.groupContainer}> | ||
<FormFieldRenderer fieldId={child.id} valueAdapter={formFieldAdapters[child.type]} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
}), | ||
[field], | ||
); | ||
|
||
return ( | ||
<div className={styles.groupContainer}> | ||
{showLabel ? ( | ||
<FormGroup legendText={t(field.label)} className={styles.boldLegend}> | ||
{content} | ||
</FormGroup> | ||
) : ( | ||
content | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ObsGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,7 @@ | |
.groupContainer { | ||
margin: 0.5rem 0; | ||
} | ||
|
||
.boldLegend > legend { | ||
font-weight: bolder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.