From 08d5cc05aa7ded167a2fb507e1fcad8007da3c17 Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Tue, 30 Jan 2024 20:10:12 +0800 Subject: [PATCH] [Experimental] Fix a bug where new attribute filter block flickers in editor. (#44147) --- .../inner-blocks/attribute-filter/edit.tsx | 168 ++++++++++++------ .../44147-dev-fix-flickering-attribute-editor | 4 + 2 files changed, 116 insertions(+), 56 deletions(-) create mode 100644 plugins/woocommerce/changelog/44147-dev-fix-flickering-attribute-editor diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-filter/inner-blocks/attribute-filter/edit.tsx b/plugins/woocommerce-blocks/assets/js/blocks/product-filter/inner-blocks/attribute-filter/edit.tsx index 1d685ebb6c8..e706d3c4c5b 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-filter/inner-blocks/attribute-filter/edit.tsx +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-filter/inner-blocks/attribute-filter/edit.tsx @@ -2,7 +2,7 @@ * External dependencies */ import { __ } from '@wordpress/i18n'; -import { useEffect, useState } from '@wordpress/element'; +import { useCallback, useEffect, useState } from '@wordpress/element'; import { BlockControls, useBlockProps } from '@wordpress/block-editor'; import { getSetting } from '@woocommerce/settings'; import { @@ -39,6 +39,67 @@ import './style.scss'; const ATTRIBUTES = getSetting< AttributeSetting[] >( 'attributes', [] ); +const Toolbar = ( { + onClick, + isEditing, +}: { + onClick: () => void; + isEditing: boolean; +} ) => ( + + + +); + +const Wrapper = ( { + children, + onClickToolbarEdit, + isEditing, + blockProps, +}: { + children: React.ReactNode; + onClickToolbarEdit: () => void; + isEditing: boolean; + blockProps: object; +} ) => ( +
+ + { children } +
+); + +const AttributeSelectPlaceholder = ( { + attributeId, + setAttributeId, + onClickDone, +}: { + attributeId: number; + setAttributeId: ( id: number ) => void; + onClickDone: () => void; +} ) => ( + +
+ + +
+
+); + const Edit = ( props: EditProps ) => { const { attributes: blockAttributes, @@ -93,76 +154,63 @@ const Edit = ( props: EditProps ) => { ); }, [ attributeTerms, filteredCounts ] ); - const Toolbar = () => ( - - setIsEditing( ! isEditing ), - isActive: isEditing, - }, - ] } - /> - + const onClickDone = useCallback( () => { + setIsEditing( false ); + debouncedSpeak( + __( + 'Now displaying a preview of the Filter Products by Attribute block.', + 'woocommerce' + ) + ); + }, [ setIsEditing ] ); + + const setAttributeId = useCallback( + ( id ) => { + setAttributes( { + attributeId: id, + } ); + }, + [ setAttributes ] ); - const AttributeSelectPlaceholder = () => ( - -
- - setAttributes( { - attributeId: id, - } ) - } - /> - -
-
- ); - - const Wrapper = ( { children }: { children: React.ReactNode } ) => ( -
- - { children } -
- ); + const toggleEditing = useCallback( () => { + setIsEditing( ! isEditing ); + }, [ isEditing ] ); // Block rendering starts. if ( Object.keys( ATTRIBUTES ).length === 0 ) return ( - + ); if ( isEditing ) return ( - - + + ); if ( ! attributeId || ! attributeObject ) return ( - +

{ __( @@ -176,7 +224,11 @@ const Edit = ( props: EditProps ) => { if ( attributeOptions.length === 0 ) return ( - +

{ __( @@ -189,7 +241,11 @@ const Edit = ( props: EditProps ) => { ); return ( - + { displayStyle === 'dropdown' ? ( diff --git a/plugins/woocommerce/changelog/44147-dev-fix-flickering-attribute-editor b/plugins/woocommerce/changelog/44147-dev-fix-flickering-attribute-editor new file mode 100644 index 00000000000..06a5034f3b9 --- /dev/null +++ b/plugins/woocommerce/changelog/44147-dev-fix-flickering-attribute-editor @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +[Experimental] Fix a bug where the editor for attribute selector would flicker and re-render constantly. \ No newline at end of file