@@ -19,8 +19,8 @@ import {
1919 warning ,
2020 setSubmissionErrors ,
2121 useRecordFromLocation ,
22+ useFormIsDirty ,
2223} from 'ra-core' ;
23- import isEmpty from 'lodash/isEmpty' ;
2424
2525/**
2626 * Submit button for resource forms (Edit and Create).
@@ -73,9 +73,9 @@ export const SaveButton = <RecordType extends RaRecord = any>(
7373 const translate = useTranslate ( ) ;
7474 const form = useFormContext ( ) ;
7575 const saveContext = useSaveContext ( ) ;
76- const { dirtyFields , isValidating, isSubmitting } = useFormState ( ) ;
76+ const { isValidating, isSubmitting } = useFormState ( ) ;
7777 // useFormState().isDirty might differ from useFormState().dirtyFields (https://github.com/react-hook-form/react-hook-form/issues/4740)
78- const isDirty = hasDirtyFields ( dirtyFields ) ;
78+ const isDirty = useFormIsDirty ( ) ;
7979 // Use form isDirty, isValidating and form context saving to enable or disable the save button
8080 // if alwaysEnable is undefined and the form wasn't prefilled
8181 const recordFromLocation = useRecordFromLocation ( ) ;
@@ -227,44 +227,3 @@ declare module '@mui/material/styles' {
227227 } ;
228228 }
229229}
230-
231- const hasDirtyFields = (
232- dirtyFields : Partial <
233- Readonly < {
234- [ x : string ] : any ;
235- } >
236- >
237- ) : boolean => {
238- // dirtyFields can contains simple keys with boolean values, nested objects or arrays
239- // We must ignore values that are false
240- return Object . values ( dirtyFields ) . some ( value => {
241- if ( typeof value === 'boolean' ) {
242- return value ;
243- } else if ( Array . isArray ( value ) ) {
244- // Some arrays contain only booleans (scalar arrays), some arrays contain objects (object arrays)
245- for ( const item of value ) {
246- if ( item === true ) {
247- return true ;
248- }
249- // FIXME: because we currently don't set default values correctly for arrays,
250- // new items are either empty objects, or undefined in dirtyFields. Consider them as dirty.
251- if (
252- ( typeof item === 'object' && isEmpty ( item ) ) ||
253- item === undefined
254- ) {
255- return true ;
256- }
257- if (
258- typeof item === 'object' &&
259- item !== null &&
260- hasDirtyFields ( item )
261- ) {
262- return true ;
263- }
264- }
265- } else if ( typeof value === 'object' && value !== null ) {
266- return hasDirtyFields ( value ) ;
267- }
268- return false ;
269- } ) ;
270- } ;
0 commit comments