Skip to content

Commit 8e62836

Browse files
committed
Use subscribe submit state in ArrayInput
1 parent 756d0cf commit 8e62836

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

packages/ra-ui-materialui/src/input/ArrayInput/ArrayInput.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ArrayInputBase,
88
type ArrayInputBaseProps,
99
} from 'ra-core';
10-
import { useFormContext, useFormState } from 'react-hook-form';
10+
import { useFormContext } from 'react-hook-form';
1111
import {
1212
InputLabel,
1313
FormControl,
@@ -89,23 +89,25 @@ export const ArrayInput = (inProps: ArrayInputProps) => {
8989
const parentSourceContext = useSourceContext();
9090
const finalSource = parentSourceContext.getSource(arraySource);
9191
const { subscribe } = useFormContext();
92-
const { isSubmitted } = useFormState();
93-
const [{ error, hasBeenInteractedWith }, setArrayInputState] =
92+
const [{ error, hasBeenInteractedWith, isSubmitted }, setArrayInputState] =
9493
React.useState<{
9594
error: any;
9695
hasBeenInteractedWith: boolean;
96+
isSubmitted: boolean;
9797
}>({
9898
error: undefined,
9999
hasBeenInteractedWith: false,
100+
isSubmitted: false,
100101
});
101102
React.useEffect(() => {
102103
return subscribe({
103104
formState: {
104105
dirtyFields: true,
105106
errors: true,
107+
isSubmitted: true,
106108
touchedFields: true,
107109
},
108-
callback: ({ dirtyFields, errors, touchedFields }) => {
110+
callback: ({ dirtyFields, errors, isSubmitted, touchedFields }) => {
109111
const error = get(errors ?? {}, finalSource);
110112
const hasBeenInteractedWith =
111113
get(dirtyFields ?? {}, finalSource, false) !== false ||
@@ -115,17 +117,18 @@ export const ArrayInput = (inProps: ArrayInputProps) => {
115117
isEqual(previousState, {
116118
error,
117119
hasBeenInteractedWith,
120+
isSubmitted: !!isSubmitted,
118121
})
119122
? previousState
120123
: {
121124
error,
122125
hasBeenInteractedWith,
126+
isSubmitted: !!isSubmitted,
123127
}
124128
);
125129
},
126130
});
127131
}, [finalSource, subscribe]);
128-
129132
const displayedError =
130133
hasBeenInteractedWith || isSubmitted ? error : undefined;
131134
const displayedErrorMessage = (displayedError?.root?.message ??

0 commit comments

Comments
 (0)