Skip to content

Commit

Permalink
fix performance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Nov 28, 2024
1 parent e01fb7f commit 7b8acfa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
12 changes: 6 additions & 6 deletions packages/dataviews/src/dataform-hooks/use-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ export const useForm = < Item >(
} );

const setTouchedFields = ( touchedFields: string[] ) => {
setForm( {
...form,
setForm( ( prevForm ) => ( {
...prevForm,
touchedFields,
} );
} ) );
};

const setErrors = ( field: string, error: string | undefined ) => {
setForm( {
...form,
setForm( ( prevForm ) => ( {
...prevForm,
messageErrors: {
...form.messageErrors,
[ field ]: error,
},
} );
} ) );
};

const isFormValid = ( data: Item ) => {
Expand Down
38 changes: 24 additions & 14 deletions packages/dataviews/src/dataforms-layouts/data-form-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __experimentalVStack as VStack } from '@wordpress/components';
import { useContext, useEffect, useMemo } from '@wordpress/element';
import { useContext, useEffect, useMemo, useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -48,9 +48,15 @@ export function DataFormLayout< Item >( {
[ form ]
);

const isFirstValidationAlreadyRunRef = useRef( false );

const { setTouchedFields, setErrors, touchedFields, messageErrors } = form;

useEffect( () => {
if ( isFirstValidationAlreadyRunRef.current ) {
return;
}

normalizedFormFields.forEach( ( formField ) => {
const { isValid, errorMessage } = formField.validation.callback( {
...data,
Expand All @@ -61,6 +67,8 @@ export function DataFormLayout< Item >( {
setErrors( formField.id, undefined );
}
} );

isFirstValidationAlreadyRunRef.current = true;
}, [ data, normalizedFormFields, setErrors ] );

return (
Expand Down Expand Up @@ -89,36 +97,38 @@ export function DataFormLayout< Item >( {
return children( FieldLayout, formField );
}

const shouldShowError = formField.validation
.showErrorOnlyWhenDirty
? touchedFields.includes( formField.id )
: true;

const errorMessage = shouldShowError
? messageErrors[ formField.id ]
: undefined;

return (
<FieldLayout
key={ formField.id }
data={ data }
field={ formField }
errorMessage={
( formField.validation.showErrorOnlyWhenDirty &&
touchedFields.includes( formField.id ) ) ||
( ! formField.validation.showErrorOnlyWhenDirty &&
messageErrors[ formField.id ] )
? messageErrors[ formField.id ]
: undefined
}
errorMessage={ errorMessage }
onChange={ ( value ) => {
onChange( value );

if ( ! touchedFields.includes( formField.id ) ) {
setTouchedFields( [
// @ts-ignore
...form.touchedFields,
...touchedFields,
formField.id,
] );
}

onChange( value );
const { isValid, errorMessage } =
const { isValid, errorMessage: message } =
formField.validation.callback( {
...data,
...value,
} );
if ( ! isValid ) {
setErrors( formField.id, errorMessage );
setErrors( formField.id, message );
} else {
setErrors( formField.id, undefined );
}
Expand Down

0 comments on commit 7b8acfa

Please sign in to comment.