From 0fc75c514871d330888c3c0a81a48f5afdf43475 Mon Sep 17 00:00:00 2001 From: Maikel Perez Date: Wed, 13 Mar 2024 19:16:12 -0300 Subject: [PATCH] Remove Custom Fields for New Product Editor (#45493) * Add remove custom field for the new product editor * Add changelog file * Show add button when the custom fields are empty * Ensures to always convert the metas into a string array since it can be anything * Add changelog file --- .../product-editor/changelog/add-44169-remove | 4 + .../custom-fields/custom-fields.tsx | 114 +++++++++++------- .../src/hooks/use-custom-fields/types.ts | 2 +- .../use-custom-fields/use-custom-fields.ts | 32 ++++- .../hooks/use-custom-fields/utils/index.ts | 8 +- .../woocommerce/changelog/add-44169-remove | 4 + .../Features/ProductBlockEditor/Init.php | 3 +- 7 files changed, 114 insertions(+), 53 deletions(-) create mode 100644 packages/js/product-editor/changelog/add-44169-remove create mode 100644 plugins/woocommerce/changelog/add-44169-remove diff --git a/packages/js/product-editor/changelog/add-44169-remove b/packages/js/product-editor/changelog/add-44169-remove new file mode 100644 index 00000000000..9dbf72c281e --- /dev/null +++ b/packages/js/product-editor/changelog/add-44169-remove @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Remove Custom Fields for New Product Editor 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 396e3c383e6..abed847edea 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 @@ -4,6 +4,7 @@ import { Button } from '@wordpress/components'; import { createElement, Fragment, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; +import { closeSmall } from '@wordpress/icons'; import classNames from 'classnames'; /** @@ -21,17 +22,17 @@ export function CustomFields( { renderActionButtonsWrapper = ( buttons ) => buttons, ...props }: CustomFieldsProps ) { - const { customFields, addCustomFields, updateCustomField } = - useCustomFields(); + const { + customFields, + addCustomFields, + updateCustomField, + removeCustomField, + } = useCustomFields(); const [ showCreateModal, setShowCreateModal ] = useState( false ); const [ selectedCustomField, setSelectedCustomField ] = useState< Metadata< string > >(); - if ( customFields.length === 0 ) { - return ; - } - function handleAddNewButtonClick() { setShowCreateModal( true ); } @@ -44,6 +45,14 @@ export function CustomFields( { }; } + function customFieldRemoveButtonClickHandler( + customField: Metadata< string > + ) { + return function handleCustomFieldRemoveButtonClick() { + removeCustomField( customField ); + }; + } + function handleCreateModalCreate( value: Metadata< string >[] ) { addCustomFields( value ); setShowCreateModal( false ); @@ -70,46 +79,61 @@ export function CustomFields( { ) } - - - - - - - - - - { customFields.map( ( customField ) => ( - - - - + { customFields.length === 0 ? ( + + ) : ( +
{ __( 'Name', 'woocommerce' ) }{ __( 'Value', 'woocommerce' ) }{ __( 'Actions', 'woocommerce' ) }
- { customField.key } - - { customField.value } - - -
+ + + + + - ) ) } - -
{ __( 'Name', 'woocommerce' ) }{ __( 'Value', 'woocommerce' ) }{ __( 'Actions', 'woocommerce' ) }
+ + + { customFields.map( ( customField ) => ( + + + { customField.key } + + + { customField.value } + + + + +