diff --git a/packages/js/product-editor/changelog/fix-46610 b/packages/js/product-editor/changelog/fix-46610 new file mode 100644 index 00000000000..34120a7bfae --- /dev/null +++ b/packages/js/product-editor/changelog/fix-46610 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix validation for the custom fields diff --git a/packages/js/product-editor/src/components/custom-fields/create-modal/create-modal.tsx b/packages/js/product-editor/src/components/custom-fields/create-modal/create-modal.tsx index 8c8f11bf468..e129ac86820 100644 --- a/packages/js/product-editor/src/components/custom-fields/create-modal/create-modal.tsx +++ b/packages/js/product-editor/src/components/custom-fields/create-modal/create-modal.tsx @@ -14,7 +14,11 @@ import type { FocusEvent } from 'react'; */ import { TRACKS_SOURCE } from '../../../constants'; import { TextControl } from '../../text-control'; -import { validate, type ValidationErrors } from '../utils/validations'; +import { + ValidationError, + validate, + type ValidationErrors, +} from '../utils/validations'; import type { Metadata } from '../../../types'; import type { CreateModalProps } from './types'; @@ -25,6 +29,7 @@ const DEFAULT_CUSTOM_FIELD = { } satisfies Metadata< string >; export function CreateModal( { + values, onCreate, onCancel, ...props @@ -86,14 +91,20 @@ export function CreateModal( { prop: keyof Metadata< string > ) { return function handleBlur( event: FocusEvent< HTMLInputElement > ) { - const error = validate( { - ...customField, - [ prop ]: event.target.value, - } ); + const error = validate( + { + ...customField, + [ prop ]: event.target.value, + }, + [ ...customFields, ...values ] + ); const id = String( customField.id ); setValidationError( ( current ) => ( { ...current, - [ id ]: error, + [ id ]: { + ...( current[ id ] as ValidationError ), + [ prop ]: error[ prop ], + }, } ) ); }; } @@ -133,7 +144,10 @@ export function CreateModal( { function handleAddButtonClick() { const { errors, hasErrors } = customFields.reduce( ( prev, customField ) => { - const _errors = validate( customField ); + const _errors = validate( customField, [ + ...customFields, + ...values, + ] ); prev.errors[ String( customField.id ) ] = _errors; if ( _errors.key ) { @@ -165,7 +179,10 @@ export function CreateModal( { } onCreate( - customFields.map( ( { id, ...customField } ) => customField ) + customFields.map( ( { id, ...customField } ) => ( { + key: customField.key.trim(), + value: customField.value?.trim(), + } ) ) ); recordEvent( 'product_custom_fields_add_new_button_click', { diff --git a/packages/js/product-editor/src/components/custom-fields/create-modal/types.ts b/packages/js/product-editor/src/components/custom-fields/create-modal/types.ts index 0a47208def4..106afb023ff 100644 --- a/packages/js/product-editor/src/components/custom-fields/create-modal/types.ts +++ b/packages/js/product-editor/src/components/custom-fields/create-modal/types.ts @@ -12,6 +12,7 @@ export type CreateModalProps = Omit< Modal.Props, 'title' | 'onRequestClose' | 'children' > & { + values: Metadata< string >[]; onCreate( value: Metadata< string >[] ): void; onCancel(): void; }; diff --git a/packages/js/product-editor/src/components/custom-fields/custom-fields.tsx b/packages/js/product-editor/src/components/custom-fields/custom-fields.tsx index d496405a5f7..53adee918d4 100644 --- a/packages/js/product-editor/src/components/custom-fields/custom-fields.tsx +++ b/packages/js/product-editor/src/components/custom-fields/custom-fields.tsx @@ -32,8 +32,8 @@ export function CustomFields( { } = useCustomFields(); const [ showCreateModal, setShowCreateModal ] = useState( false ); - const [ selectedCustomField, setSelectedCustomField ] = - useState< Metadata< string > >(); + const [ selectedCustomFieldIndex, setSelectedCustomFieldIndex ] = + useState< number >(); function handleAddNewButtonClick() { setShowCreateModal( true ); @@ -43,11 +43,11 @@ export function CustomFields( { } ); } - function customFieldEditButtonClickHandler( - customField: Metadata< string > - ) { + function customFieldEditButtonClickHandler( customFieldIndex: number ) { return function handleCustomFieldEditButtonClick() { - setSelectedCustomField( customField ); + setSelectedCustomFieldIndex( customFieldIndex ); + + const customField = customFields[ customFieldIndex ]; recordEvent( 'product_custom_fields_show_edit_modal', { source: TRACKS_SOURCE, @@ -85,12 +85,12 @@ export function CustomFields( { } function handleEditModalUpdate( customField: Metadata< string > ) { - updateCustomField( customField ); - setSelectedCustomField( undefined ); + updateCustomField( customField, selectedCustomFieldIndex ); + setSelectedCustomFieldIndex( undefined ); } function handleEditModalCancel() { - setSelectedCustomField( undefined ); + setSelectedCustomFieldIndex( undefined ); recordEvent( 'product_custom_fields_cancel_edit_modal', { source: TRACKS_SOURCE, @@ -123,7 +123,7 @@ export function CustomFields( {
- { customFields.map( ( customField ) => ( + { customFields.map( ( customField, index ) => (