Skip to content

Commit

Permalink
(feat) Add ability to track deleted fields in repeat component
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau committed Jan 8, 2025
1 parent 266dfe4 commit 43a6257
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/adapters/obs-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const formContext = {
formFieldAdapters: null,
formFieldValidators: null,
customDependencies: null,
deletedFields: [],
getFormField: jest.fn(),
addFormField: jest.fn(),
updateFormField: jest.fn(),
Expand All @@ -36,6 +37,7 @@ const formContext = {
removeInvalidField: jest.fn(),
setInvalidFields: jest.fn(),
setForm: jest.fn(),
setDeletedFields: jest.fn(),
} as FormContextProps;

describe('ObsAdapter - transformFieldValue', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/program-state-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const formContext = {
customDependencies: {
patientPrograms: [],
},
deletedFields: [],
getFormField: jest.fn(),
addFormField: jest.fn(),
updateFormField: jest.fn(),
Expand All @@ -38,6 +39,7 @@ const formContext = {
removeInvalidField: jest.fn(),
setInvalidFields: jest.fn(),
setForm: jest.fn(),
setDeletedFields: jest.fn(),
} as FormContextProps;

const field = {
Expand Down
7 changes: 5 additions & 2 deletions src/components/renderer/form/form-renderer.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const FormRenderer = ({
formState: { isDirty },
} = methods;

const [{ formFields, invalidFields, formJson }, dispatch] = useReducer(formStateReducer, {
const [{ formFields, invalidFields, formJson, deletedFields }, dispatch] = useReducer(formStateReducer, {
...initialState,
formFields: evaluatedFields,
formJson: evaluatedFormJson,
Expand All @@ -52,6 +52,7 @@ export const FormRenderer = ({
addInvalidField,
removeInvalidField,
setForm,
setDeletedFields,
} = useFormStateHelpers(dispatch, formFields);

useEffect(() => {
Expand All @@ -75,6 +76,7 @@ export const FormRenderer = ({
formFields,
formJson,
invalidFields,
deletedFields,
addFormField,
updateFormField,
getFormField,
Expand All @@ -83,8 +85,9 @@ export const FormRenderer = ({
addInvalidField,
removeInvalidField,
setForm,
setDeletedFields,
};
}, [processorContext, workspaceLayout, methods, formFields, formJson, invalidFields]);
}, [processorContext, workspaceLayout, methods, formFields, formJson, invalidFields, deletedFields]);

useEffect(() => {
registerForm(formJson.name, isSubForm, context);
Expand Down
7 changes: 6 additions & 1 deletion src/components/renderer/form/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type FormState = {
formFields: FormField[];
invalidFields: FormField[];
formJson: FormSchema;
deletedFields: FormField[];
};

type Action =
Expand All @@ -15,12 +16,14 @@ type Action =
| { type: 'ADD_INVALID_FIELD'; value: FormField }
| { type: 'REMOVE_INVALID_FIELD'; value: string }
| { type: 'CLEAR_INVALID_FIELDS' }
| { type: 'SET_FORM_JSON'; value: any };
| { type: 'SET_FORM_JSON'; value: any }
| { type: 'SET_DELETED_FIELDS'; value: FormField[] };

const initialState: FormState = {
formFields: [],
invalidFields: [],
formJson: null,
deletedFields: [],
};

const formStateReducer = (state: FormState, action: Action): FormState => {
Expand All @@ -46,6 +49,8 @@ const formStateReducer = (state: FormState, action: Action): FormState => {
return { ...state, invalidFields: [] };
case 'SET_FORM_JSON':
return { ...state, formJson: action.value };
case 'SET_DELETED_FIELDS':
return { ...state, deletedFields: action.value };
default:
return state;
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/repeat/repeat.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const Repeat: React.FC<FormFieldInputProps> = ({ field }) => {
methods: { getValues, setValue },
addFormField,
removeFormField,
deletedFields,
setDeletedFields,
} = context;

useEffect(() => {
Expand Down Expand Up @@ -113,6 +115,7 @@ const Repeat: React.FC<FormFieldInputProps> = ({ field }) => {
clearSubmission(field);
}
setRows(rows.filter((q) => q.id !== field.id));
setDeletedFields([...deletedFields, field]);
removeFormField(field.id);
};

Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useFormStateHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export function useFormStateHelpers(dispatch: Dispatch<Action>, formFields: Form
dispatch({ type: 'SET_FORM_JSON', value: updateFormSectionReferences(formJson) });
}, []);

const setDeletedFields = useCallback((fields: FormField[]) => {
dispatch({ type: 'SET_DELETED_FIELDS', value: fields });
}, []);

return {
addFormField,
updateFormField,
Expand All @@ -63,5 +67,6 @@ export function useFormStateHelpers(dispatch: Dispatch<Action>, formFields: Form
addInvalidField,
removeInvalidField,
setForm,
setDeletedFields,
};
}
2 changes: 2 additions & 0 deletions src/provider/form-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface FormContextProps extends FormProcessorContextProps {
methods: UseFormReturn<any>;
workspaceLayout: 'minimized' | 'maximized';
isSubmitting?: boolean;
deletedFields: FormField[];
getFormField?: (field: string) => FormField;
addFormField?: (field: FormField) => void;
updateFormField?: (field: FormField) => void;
Expand All @@ -15,6 +16,7 @@ export interface FormContextProps extends FormProcessorContextProps {
removeInvalidField?: (fieldId: string) => void;
setInvalidFields?: (fields: FormField[]) => void;
setForm?: (formJson: FormSchema) => void;
setDeletedFields?: (fields: FormField[]) => void;
}

export interface FormProviderProps extends FormContextProps {
Expand Down

0 comments on commit 43a6257

Please sign in to comment.