MCPcopy
hub / github.com/final-form/final-form / calculateNextFormState

Function calculateNextFormState

src/FinalForm.ts:595–701  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

593 !!(state.formState.error || hasAnyError(state.formState.errors));
594
595 const calculateNextFormState = (): FormState<FormValues, InitialFormValues> => {
596 const { fields, formState, lastFormState } = state;
597 const safeFields = { ...fields };
598 const safeFieldKeys = Object.keys(safeFields);
599
600 // calculate dirty/pristine
601 let foundDirty = false;
602 const dirtyFields = safeFieldKeys.reduce((result, key) => {
603 const dirty = !safeFields[key].isEqual(
604 getIn(formState.values as Record<string, any>, key),
605 getIn(formState.initialValues as Record<string, any> || {}, key),
606 );
607 if (dirty) {
608 foundDirty = true;
609 result[key] = true;
610 }
611 return result;
612 }, {});
613
614 // FIX #487: For unregistered fields, check if their values differ from initialValues
615 // This handles cases like FieldArray where the array itself isn't registered
616 if (!foundDirty) {
617 // Dedupe keys using Set to avoid duplicate checks
618 const allKeys = new Set([
619 ...Object.keys(formState.values || {}),
620 ...Object.keys(formState.initialValues || {})
621 ]);
622 for (const key of allKeys) {
623 if (!safeFields[key]) {
624 const currentValue = (formState.values as any)?.[key];
625 const initialValue = (formState.initialValues as any)?.[key];
626 if (!shallowEqual(currentValue, initialValue)) {
627 foundDirty = true;
628 break;
629 }
630 }
631 }
632 }
633 const dirtyFieldsSinceLastSubmit = safeFieldKeys.reduce((result, key) => {
634 // istanbul ignore next
635 const nonNullLastSubmittedValues = formState.lastSubmittedValues || {}; // || {} is for flow, but causes branch coverage complaint
636 if (
637 !safeFields[key].isEqual(
638 getIn(formState.values as Record<string, any>, key),
639 getIn(nonNullLastSubmittedValues, key),
640 )
641 ) {
642 result[key] = true;
643 }
644 return result;
645 }, {});
646 formState.pristine = !foundDirty;
647 formState.dirtySinceLastSubmit = !!(
648 formState.lastSubmittedValues &&
649 Object.values(dirtyFieldsSinceLastSubmit).some((value) => value)
650 );
651 formState.modifiedSinceLastSubmit = !!(
652 formState.lastSubmittedValues &&

Callers 3

callDebugFunction · 0.85
notifyFormListenersFunction · 0.85
createFormFunction · 0.85

Calls 4

getInFunction · 0.85
shallowEqualFunction · 0.85
hasAnyErrorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…