Skip to content

Commit

Permalink
Merge branch 'add/handle-message-error' of github.com:WordPress/guten…
Browse files Browse the repository at this point in the history
…berg into add/handle-message-error
  • Loading branch information
gigitux committed Dec 4, 2024
2 parents 3da37fd + 7b8acfa commit 3e97598
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
21 changes: 16 additions & 5 deletions packages/dataviews/src/normalize-form-fields.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Internal dependencies
*/
import type { Form } from './types';
import type { Form, FormFieldValidation } from './types';

interface NormalizedFormField {
export type NormalizedFormField = {
id: string;
layout: 'regular' | 'panel';
labelPosition: 'side' | 'top' | 'none';
}
} & { validation: FormFieldValidation };

export default function normalizeFormFields(
form: Form
Expand All @@ -20,12 +20,19 @@ export default function normalizeFormFields(
const labelPosition =
form.labelPosition ?? ( layout === 'regular' ? 'top' : 'side' );

return ( form.fields ?? [] ).map( ( field ) => {
return Object.entries( form.fields ?? {} ).map( ( [ id, field ] ) => {
if ( typeof field === 'string' ) {
return {
id: field,
id,
layout,
labelPosition,
validation: {
callback: () => ( {
isValid: true,
errorMessage: '',
} ),
showErrorOnlyWhenDirty: true,
},
};
}

Expand All @@ -35,8 +42,12 @@ export default function normalizeFormFields(
( fieldLayout === 'regular' ? 'top' : 'side' );
return {
...field,
id,
layout: fieldLayout,
labelPosition: fieldLabelPosition,
validation: {
...field.validation,
},
};
} );
}
24 changes: 22 additions & 2 deletions packages/dataviews/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,25 +549,45 @@ export type SimpleFormField = {
id: string;
layout?: 'regular' | 'panel';
labelPosition?: 'side' | 'top' | 'none';
};
} & { validation: FormFieldValidation };

export type CombinedFormField = {
id: string;
label?: string;
layout?: 'regular' | 'panel';
labelPosition?: 'side' | 'top' | 'none';
children: Array< FormField | string >;
} & { validation: FormFieldValidation };

export type ValidationResult = {
isValid: boolean;
errorMessage: string | undefined;
};

export type FormField = SimpleFormField | CombinedFormField;
export type FormFieldValidation = {
/**
* The validation should be triggered only when the field is dirty.
*/
showErrorOnlyWhenDirty: boolean;
/**
* The validation function.
*/
callback: ( data: any ) => ValidationResult;
};

export type FormField = SimpleFormField | CombinedFormField;
/**
* The form configuration.
*/
export type Form = {
type?: 'regular' | 'panel';
fields?: Array< FormField | string >;
labelPosition?: 'side' | 'top' | 'none';
touchedFields: string[];
messageErrors: Record< string, string | undefined >;
setTouchedFields: ( touchedFields: string[] ) => void;
setErrors: ( field: string, error: string | undefined ) => void;
isFormValid: ( data: Record< string, any > ) => boolean;
};

export interface DataFormProps< Item > {
Expand Down
6 changes: 2 additions & 4 deletions packages/fields/src/actions/duplicate-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import type { BasePost, CoreDataError } from '../types';
import { getItemTitle } from './utils';

const fields = [ titleField ];
const formDuplicateAction = {
fields: [ 'title' ],
};

const duplicatePost: Action< BasePost > = {
id: 'duplicate-post',
Expand Down Expand Up @@ -139,7 +136,8 @@ const duplicatePost: Action< BasePost > = {
<DataForm
data={ item }
fields={ fields }
form={ formDuplicateAction }
// @ts-ignore
form={ form }
onChange={ ( changes ) =>
setItem( ( prev ) => ( {
...prev,
Expand Down
4 changes: 3 additions & 1 deletion packages/fields/src/actions/reorder-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function ReorderModal( {

async function onOrder( event: React.FormEvent ) {
event.preventDefault();

// @ts-ignore
if ( ! isItemValid( item, fields, formOrderAction ) ) {
return;
}
Expand Down Expand Up @@ -68,6 +68,7 @@ function ReorderModal( {
} );
}
}
// @ts-ignore
const isSaveDisabled = ! isItemValid( item, fields, formOrderAction );
return (
<form onSubmit={ onOrder }>
Expand All @@ -80,6 +81,7 @@ function ReorderModal( {
<DataForm
data={ item }
fields={ fields }
// @ts-ignore
form={ formOrderAction }
onChange={ ( changes ) =>
setItem( {
Expand Down

0 comments on commit 3e97598

Please sign in to comment.