fix: mount signers field array so setValue reconciles reordered arrays

This commit is contained in:
David Nguyen
2026-08-04 18:43:46 +10:00
parent 9db2014b5f
commit 83eb53fffb
@@ -23,7 +23,7 @@ import { Trans } from '@lingui/react/macro';
import { DocumentSigningOrder, RecipientRole, SendStatus } from '@prisma/client';
import { HelpCircleIcon, PlusIcon, SparklesIcon } from 'lucide-react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useWatch } from 'react-hook-form';
import { useFieldArray, useWatch } from 'react-hook-form';
import { useRevalidator, useSearchParams } from 'react-router';
import { isDeepEqual } from 'remeda';
@@ -134,6 +134,15 @@ export const EnvelopeEditorRecipientForm = () => {
return normalizeGroupedSigningOrders(signers, (signer) => canRecipientBeModified(signer.id));
};
// Keep a mounted field array for `signers` so react-hook-form reconciles
// whole-array `setValue` calls atomically. Without it, reordering the array
// leaves stale partial entries in watched values (missing email/name/role),
// which breaks validation and the autosave sync.
useFieldArray({
control,
name: 'signers',
});
const stepCount = useMemo(() => groupRecipientsBySigningOrder(watchedSigners).steps.length, [watchedSigners]);
const emptySignerIndex = watchedSigners.findIndex(