From 5437ed50c3b9423dac4cae4dcd2055e76ddf8182 Mon Sep 17 00:00:00 2001 From: Nathan Silveira Date: Fri, 19 May 2023 12:45:11 -0300 Subject: [PATCH] Use SelectTree in Parent Category field (#38261) * Increment label CSS to look the same as TextControl's label * Migrate Parent Category field to SelectTree * Add changelogs * Fix unit test that was edited by mistake * Revert css changes in select-control.scss * Use BaseControl as label on SelectTree * Increment changelog * Increment changelogs * Refactor test * Refactor category-field functions and use them in related places * Fix ordered list appearing empty --- .../changelog/improve-selecttree-label | 4 + .../select-tree.tsx | 100 +++++++------- .../src/experimental-tree-control/tree.tsx | 58 ++++---- .../changelog/update-parent-categ | 4 + .../category-field.tsx | 56 ++++---- .../create-category-modal.scss | 1 + .../create-category-modal.tsx | 125 ++++++------------ .../test/use-category-search.test.ts | 23 ++-- .../use-category-search.ts | 27 ---- 9 files changed, 171 insertions(+), 227 deletions(-) create mode 100644 packages/js/components/changelog/improve-selecttree-label create mode 100644 packages/js/product-editor/changelog/update-parent-categ diff --git a/packages/js/components/changelog/improve-selecttree-label b/packages/js/components/changelog/improve-selecttree-label new file mode 100644 index 0000000000000..5a831787bf311 --- /dev/null +++ b/packages/js/components/changelog/improve-selecttree-label @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Use BaseControl in the SelectTree label diff --git a/packages/js/components/src/experimental-select-tree-control/select-tree.tsx b/packages/js/components/src/experimental-select-tree-control/select-tree.tsx index 587088ebca883..114e696326b25 100644 --- a/packages/js/components/src/experimental-select-tree-control/select-tree.tsx +++ b/packages/js/components/src/experimental-select-tree-control/select-tree.tsx @@ -5,7 +5,7 @@ import { chevronDown } from '@wordpress/icons'; import classNames from 'classnames'; import { createElement, useState } from '@wordpress/element'; import { useInstanceId } from '@wordpress/compose'; -import { TextControl } from '@wordpress/components'; +import { BaseControl, TextControl } from '@wordpress/components'; /** * Internal dependencies @@ -130,61 +130,55 @@ export const SelectTree = function SelectTree( { } ) } > - - { props.multiple ? ( - - item?.label || '' } - getItemValue={ ( item ) => item?.value || '' } - onRemove={ ( item ) => { - if ( - ! Array.isArray( item ) && - props.onRemove - ) { - props.onRemove( item ); + + { props.multiple ? ( + + item?.label || '' } + getItemValue={ ( item ) => item?.value || '' } + onRemove={ ( item ) => { + if ( + ! Array.isArray( item ) && + props.onRemove + ) { + props.onRemove( item ); + } + } } + getSelectedItemProps={ () => ( {} ) } + /> + + ) : ( + { + if ( onInputChange ) onInputChange( value ); + const item = items.find( + ( i ) => i.label === value + ); + if ( props.onSelect && item ) { + props.onSelect( item ); + } + if ( ! value && props.onRemove ) { + props.onRemove( props.selected as Item ); } } } - getSelectedItemProps={ () => ( {} ) } /> - - ) : ( - { - if ( onInputChange ) onInputChange( value ); - const item = items.find( - ( i ) => i.label === value - ); - if ( props.onSelect && item ) { - props.onSelect( item ); - } - if ( ! value && props.onRemove ) { - props.onRemove( props.selected as Item ); - } - } } - /> - ) } + ) } + -
    - { items.map( ( child, index ) => ( - { - ( - rootListRef.current - ?.closest( 'ol[role="tree"]' ) - ?.parentElement?.querySelector( - '.experimental-woocommerce-tree__button' - ) as HTMLButtonElement - )?.focus(); - } } - /> - ) ) } -
+ { items.length || isCreateButtonVisible ? ( +
    + { items.map( ( child, index ) => ( + { + ( + rootListRef.current + ?.closest( 'ol[role="tree"]' ) + ?.parentElement?.querySelector( + '.experimental-woocommerce-tree__button' + ) as HTMLButtonElement + )?.focus(); + } } + /> + ) ) } +
+ ) : null } { isCreateButtonVisible && (