Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ttoomey committed Jan 12, 2025
1 parent 97d37aa commit 48f07f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/modules/form/components/DynamicFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import DynamicViewField from './viewable/DynamicViewField';
import { formatCurrency } from '@/modules/hmis/hmisUtil';
import { Component, FormItem, ItemType } from '@/types/gqlTypes';

/**
* Wraps a form item with appropriate dependency and autofill handlers based on its configuration
*/
export const renderItemWithWrappers = (
renderChild: (disabled?: boolean) => ReactNode,
item: FormItem,
Expand All @@ -39,9 +42,11 @@ export const renderItemWithWrappers = (
disabledDependencyMap[item.linkId] ||
!isEmpty(item.enableWhen) ||
item.item?.every((item) => item.enableWhen);

const hasAutofill =
autofillInvertedDependencyMap[item.linkId] || !isEmpty(item.autofillValues);

// Apply dependency and autofill wrappers as needed
if (hasDependencies && hasAutofill) {
return (
<DependentFormItemWrapper handlers={handlers} item={item}>
Expand Down Expand Up @@ -86,6 +91,10 @@ export interface Props {
renderFn?: (children: ReactNode) => ReactNode;
}

/**
* Renders form field components based on FormItem definitions, handling dependencies,
* autofill behavior, and conditional rendering
*/
const DynamicFormField: React.FC<Props> = ({
handlers,
clientId,
Expand All @@ -101,10 +110,12 @@ const DynamicFormField: React.FC<Props> = ({
const values = handlers.getValues();
const { definition, localConstants, getFieldErrors } = handlers;

// Handles changes to individual form items, converting values as needed
const itemChanged: ItemChangedFn = useCallback(
({ linkId, value: baseValue, type }) => {
const item = handlers.itemMap[linkId];
let value = baseValue;
// Convert string values to appropriate number types
if (item) {
if (item.type === ItemType.Integer) value = parseInt(value) || 0;
if (item.type === ItemType.Currency) value = parseFloat(value) || 0;
Expand All @@ -115,6 +126,8 @@ const DynamicFormField: React.FC<Props> = ({
},
[handlers]
);

// Handles batch changes to multiple form items
const severalItemsChanged: SeveralItemsChangedFn = useCallback(
({ values, type }) => {
Object.entries(values).forEach(([linkId, value]) =>
Expand All @@ -124,6 +137,7 @@ const DynamicFormField: React.FC<Props> = ({
[itemChanged]
);

// Renders the appropriate field component based on item type and configuration
const renderChild = useCallback(
(isDisabled?: boolean) => {
if (item.hidden) return null;
Expand Down
5 changes: 5 additions & 0 deletions src/modules/form/components/ValueWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export type ValueWrapperProps = {
children: (value: any) => ReactNode;
};

/**
* Wraps form fields to isolate re-renders.
*
* It uses react-hook-form's useWatch to track field value changes and only re-render when those specific fields change.
*/
const ValueWrapper: React.FC<ValueWrapperProps> = ({
handlers,
name,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/form/components/group/DisabilityTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const DisabilityTable = ({
?.linkId;
}, [item]);

// Link IDs for items that, if YES, consitute a Disabling Condition per HUD spec.
// Link IDs for items that, if YES, constitute a Disabling Condition per HUD spec.
// This is highly dependent on the structure of the item group.
const dependentLinkIds = useMemo(() => {
if (!isValidDisabilityGroup(item))
Expand Down Expand Up @@ -252,7 +252,7 @@ const DisabilityTable = ({
cellItem,
idx,
disabilityTypeLabelId
// dont pass a label because it is labelled-by the header cells
// don't pass a label because it is labelled-by the header cells
)}
</TableCell>
))}
Expand Down

0 comments on commit 48f07f5

Please sign in to comment.