-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edit Custom Fields for New Product Editor (#45396)
* Create edit modal * Add update function to the useCustomFields hook * Integrate the EditModal in the CustomFields component * Add edition and validation logic to the custom field EditModal component * Fix text control validation error styles * Focus the name field when its invalid * Fix linter error * Fix edit modal min width and controls width * Add changelog file * Fix text overflow in custom fields table * Remove non needed block style file
- Loading branch information
Showing
14 changed files
with
254 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Edit Custom Fields for New Product Editor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
packages/js/product-editor/src/blocks/product-fields/custom-fields/editor.scss
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 73 additions & 32 deletions
105
packages/js/product-editor/src/components/custom-fields/custom-fields.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,96 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { createElement } from '@wordpress/element'; | ||
import { Button } from '@wordpress/components'; | ||
import { createElement, Fragment, useState } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import classNames from 'classnames'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useCustomFields } from '../../hooks/use-custom-fields'; | ||
import { EditModal } from './edit-modal'; | ||
import { EmptyState } from './empty-state'; | ||
import type { Metadata } from '../../types'; | ||
import type { CustomFieldsProps } from './types'; | ||
|
||
export function CustomFields( { className, ...props }: CustomFieldsProps ) { | ||
const { customFields } = useCustomFields(); | ||
const { customFields, updateCustomField } = useCustomFields(); | ||
const [ selectedCustomField, setSelectedCustomField ] = | ||
useState< Metadata< string > >(); | ||
|
||
if ( customFields.length === 0 ) { | ||
return <EmptyState />; | ||
} | ||
|
||
function customFieldEditButtonClickHandler( | ||
customField: Metadata< string > | ||
) { | ||
return function handleCustomFieldEditButtonClick() { | ||
setSelectedCustomField( customField ); | ||
}; | ||
} | ||
|
||
function handleEditModalUpdate( customField: Metadata< string > ) { | ||
updateCustomField( customField ); | ||
setSelectedCustomField( undefined ); | ||
} | ||
|
||
function handleEditModalCancel() { | ||
setSelectedCustomField( undefined ); | ||
} | ||
|
||
return ( | ||
<table | ||
{ ...props } | ||
className={ classNames( | ||
'woocommerce-product-custom-fields__table', | ||
className | ||
) } | ||
> | ||
<thead> | ||
<tr className="woocommerce-product-custom-fields__table-row"> | ||
<th>{ __( 'Name', 'woocommerce' ) }</th> | ||
<th>{ __( 'Value', 'woocommerce' ) }</th> | ||
<th>{ __( 'Actions', 'woocommerce' ) }</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{ customFields.map( ( customField ) => ( | ||
<tr | ||
className="woocommerce-product-custom-fields__table-row" | ||
key={ customField.id ?? customField.key } | ||
> | ||
<td className="woocommerce-product-custom-fields__table-datacell"> | ||
{ customField.key } | ||
</td> | ||
<td className="woocommerce-product-custom-fields__table-datacell"> | ||
{ customField.value } | ||
</td> | ||
<td className="woocommerce-product-custom-fields__table-datacell"></td> | ||
<> | ||
<table | ||
{ ...props } | ||
className={ classNames( | ||
'woocommerce-product-custom-fields__table', | ||
className | ||
) } | ||
> | ||
<thead> | ||
<tr className="woocommerce-product-custom-fields__table-row"> | ||
<th>{ __( 'Name', 'woocommerce' ) }</th> | ||
<th>{ __( 'Value', 'woocommerce' ) }</th> | ||
<th>{ __( 'Actions', 'woocommerce' ) }</th> | ||
</tr> | ||
) ) } | ||
</tbody> | ||
</table> | ||
</thead> | ||
<tbody> | ||
{ customFields.map( ( customField ) => ( | ||
<tr | ||
className="woocommerce-product-custom-fields__table-row" | ||
key={ customField.id ?? customField.key } | ||
> | ||
<td className="woocommerce-product-custom-fields__table-datacell"> | ||
{ customField.key } | ||
</td> | ||
<td className="woocommerce-product-custom-fields__table-datacell"> | ||
{ customField.value } | ||
</td> | ||
<td className="woocommerce-product-custom-fields__table-datacell"> | ||
<Button | ||
variant="tertiary" | ||
onClick={ customFieldEditButtonClickHandler( | ||
customField | ||
) } | ||
> | ||
{ __( 'Edit', 'woocommerce' ) } | ||
</Button> | ||
</td> | ||
</tr> | ||
) ) } | ||
</tbody> | ||
</table> | ||
|
||
{ selectedCustomField && ( | ||
<EditModal | ||
initialValue={ selectedCustomField } | ||
onUpdate={ handleEditModalUpdate } | ||
onCancel={ handleEditModalCancel } | ||
/> | ||
) } | ||
</> | ||
); | ||
} |
106 changes: 106 additions & 0 deletions
106
packages/js/product-editor/src/components/custom-fields/edit-modal/edit-modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Button, Modal } from '@wordpress/components'; | ||
import { createElement, useState, useRef } from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import classNames from 'classnames'; | ||
import type { FocusEvent } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TextControl } from '../../text-control'; | ||
import type { Metadata } from '../../../types'; | ||
import type { EditModalProps } from './types'; | ||
|
||
function validateName( value: string ) { | ||
if ( value.startsWith( '_' ) ) { | ||
return __( | ||
'The name cannot begin with the underscore (_) character.', | ||
'woocommerce' | ||
); | ||
} | ||
} | ||
|
||
export function EditModal( { | ||
initialValue, | ||
onUpdate, | ||
onCancel, | ||
...props | ||
}: EditModalProps ) { | ||
const [ customField, setCustomField ] = | ||
useState< Metadata< string > >( initialValue ); | ||
const [ validationError, setValidationError ] = useState< string >(); | ||
const nameTextRef = useRef< HTMLInputElement >( null ); | ||
|
||
function renderTitle() { | ||
return sprintf( | ||
/* translators: %s: the name of the custom field */ | ||
__( 'Edit %s', 'woocommerce' ), | ||
customField.key | ||
); | ||
} | ||
|
||
function handleNameChange( key: string ) { | ||
setCustomField( ( current ) => ( { ...current, key } ) ); | ||
} | ||
|
||
function handleNameBlur( event: FocusEvent< HTMLInputElement > ) { | ||
const error = validateName( event.target.value ); | ||
setValidationError( error ); | ||
} | ||
|
||
function handleValueChange( value: string ) { | ||
setCustomField( ( current ) => ( { ...current, value } ) ); | ||
} | ||
|
||
function handleUpdateButtonClick() { | ||
const error = validateName( customField.key ); | ||
if ( error ) { | ||
setValidationError( error ); | ||
nameTextRef.current?.focus(); | ||
return; | ||
} | ||
|
||
onUpdate( customField ); | ||
} | ||
|
||
return ( | ||
<Modal | ||
shouldCloseOnClickOutside={ false } | ||
{ ...props } | ||
title={ renderTitle() } | ||
onRequestClose={ onCancel } | ||
className={ classNames( | ||
'woocommerce-product-custom-fields__edit-modal', | ||
props.className | ||
) } | ||
> | ||
<TextControl | ||
ref={ nameTextRef } | ||
label={ __( 'Name', 'woocommerce' ) } | ||
error={ validationError } | ||
value={ customField.key } | ||
onChange={ handleNameChange } | ||
onBlur={ handleNameBlur } | ||
/> | ||
|
||
<TextControl | ||
label={ __( 'Value', 'woocommerce' ) } | ||
value={ customField.value } | ||
onChange={ handleValueChange } | ||
/> | ||
|
||
<div className="woocommerce-product-custom-fields__edit-modal-actions"> | ||
<Button variant="secondary" onClick={ onCancel }> | ||
{ __( 'Cancel', 'woocommerce' ) } | ||
</Button> | ||
|
||
<Button variant="primary" onClick={ handleUpdateButtonClick }> | ||
{ __( 'Update', 'woocommerce' ) } | ||
</Button> | ||
</div> | ||
</Modal> | ||
); | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/js/product-editor/src/components/custom-fields/edit-modal/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './edit-modal'; | ||
export * from './types'; |
15 changes: 15 additions & 0 deletions
15
packages/js/product-editor/src/components/custom-fields/edit-modal/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.woocommerce-product-custom-fields__edit-modal { | ||
min-width: 75%; | ||
|
||
&-actions { | ||
display: flex; | ||
align-items: center; | ||
justify-content: flex-end; | ||
gap: 12px; | ||
margin-top: $grid-unit-40; | ||
} | ||
|
||
.components-base-control { | ||
width: 100%; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/js/product-editor/src/components/custom-fields/edit-modal/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Modal } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Metadata } from '../../../types'; | ||
|
||
export type EditModalProps = Omit< | ||
Modal.Props, | ||
'title' | 'onRequestClose' | 'children' | ||
> & { | ||
initialValue: Metadata< string >; | ||
onUpdate( value: Metadata< string > ): void; | ||
onCancel(): void; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/js/product-editor/src/components/text-control/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.woocommerce-product-text-control { | ||
&.has-error { | ||
.components-input-control__container | ||
.components-input-control__backdrop { | ||
border-color: $studio-red-50; | ||
} | ||
|
||
.components-base-control__help { | ||
color: $studio-red-50; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters