Skip to content

Commit

Permalink
Reactively cascade form field update event
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmale committed Sep 5, 2024
1 parent c9df6af commit 54b71e0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/group/obs-group.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const ObsGroup: React.FC<FormFieldInputProps> = ({ field }) => {
return (
<div className={classNames(styles.flexColumn)} key={keyId}>
<div className={styles.groupContainer}>
<FormFieldRenderer field={child} valueAdapter={formFieldAdapters[child.type]} />
<FormFieldRenderer fieldId={child.id} valueAdapter={formFieldAdapters[child.type]} />
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/inputs/select/dropdown.component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useCallback } from 'react';
import React, { useMemo, useCallback, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Dropdown as DropdownInput, Layer } from '@carbon/react';
import { shouldUseInlineLayout } from '../../../utils/form-helper';
Expand Down
10 changes: 6 additions & 4 deletions src/components/renderer/field/form-field-renderer.component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import {
type FormField,
type RenderType,
Expand All @@ -24,14 +24,14 @@ import { getFieldControlWithFallback } from '../../../utils/form-helper';
import { handleFieldLogic } from './fieldLogic';

export interface FormFieldRendererProps {
field: FormField;
fieldId: string;
valueAdapter: FormFieldValueAdapter;
repeatOptions?: {
targetRendering: RenderType;
};
}

export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFieldRendererProps) => {
export const FormFieldRenderer = ({ fieldId, valueAdapter, repeatOptions }: FormFieldRendererProps) => {
const [inputComponentWrapper, setInputComponentWrapper] = useState<{
value: React.ComponentType<FormFieldInputProps>;
}>(null);
Expand All @@ -50,9 +50,11 @@ export const FormFieldRenderer = ({ field, valueAdapter, repeatOptions }: FormFi
removeInvalidField,
} = context;

const fieldValue = useWatch({ control, name: field.id, exact: true });
const fieldValue = useWatch({ control, name: fieldId, exact: true });
const noop = () => {};

const field = useMemo(() => formFields.find((field) => field.id === fieldId), [fieldId, formFields]);

useEffect(() => {
if (hasRendering(field, 'repeating') && repeatOptions?.targetRendering) {
getRegisteredControl(repeatOptions.targetRendering).then((component) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export const SectionRenderer = ({ section }: { section: FormSection }) => {
{section.questions.map((question) =>
formFieldAdapters[question.type] ? (
<div key={`${sectionId}-${question.id}`} className={styles.sectionBody}>
<FormFieldRenderer key={question.id} field={question} valueAdapter={formFieldAdapters[question.type]} />
<FormFieldRenderer
key={question.id}
fieldId={question.id}
valueAdapter={formFieldAdapters[question.type]}
/>
</div>
) : null,
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/repeat/repeat.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const Repeat: React.FC<FormFieldInputProps> = ({ field }) => {
return rows.map((field, index) => {
const component = (
<FormFieldRenderer
field={field}
fieldId={field.id}
valueAdapter={formFieldAdapters[field.type]}
repeatOptions={{ targetRendering: getQuestionWithSupportedRendering(field).questionOptions.rendering }}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFormStateHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function useFormStateHelpers(dispatch: Dispatch<Action>, formFields: Form
dispatch({ type: 'ADD_FORM_FIELD', value: field });
}, []);
const updateFormField = useCallback((field: FormField) => {
dispatch({ type: 'UPDATE_FORM_FIELD', value: field });
dispatch({ type: 'UPDATE_FORM_FIELD', value: structuredClone(field) });
}, []);

const getFormField = useCallback(
Expand Down

0 comments on commit 54b71e0

Please sign in to comment.