Skip to content

Commit

Permalink
Block Editor: Fix the 'Reset all' bug for the 'ResolutionTool' compon…
Browse files Browse the repository at this point in the history
…ent (#68296)


Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
  • Loading branch information
3 people authored Dec 26, 2024
1 parent 10ada5a commit c0329d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/resolution-tool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function ResolutionTool( {
options = DEFAULT_SIZE_OPTIONS,
defaultValue = DEFAULT_SIZE_OPTIONS[ 0 ].value,
isShownByDefault = true,
resetAllFilter,
} ) {
const displayValue = value ?? defaultValue;
return (
Expand All @@ -42,6 +43,7 @@ export default function ResolutionTool( {
onDeselect={ () => onChange( defaultValue ) }
isShownByDefault={ isShownByDefault }
panelId={ panelId }
resetAllFilter={ resetAllFilter }
>
<SelectControl
__nextHasNoMarginBottom
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useReducer } from '@wordpress/element';
import {
Panel,
__experimentalToolsPanel as ToolsPanel,
Expand All @@ -21,28 +21,55 @@ export default {
},
};

export const Default = ( { panelId, onChange: onChangeProp, ...props } ) => {
const [ resolution, setResolution ] = useState( undefined );
const resetAll = () => {
setResolution( undefined );
export const Default = ( {
label,
panelId,
onChange: onChangeProp,
...props
} ) => {
const [ attributes, setAttributes ] = useReducer(
( prevState, nextState ) => ( { ...prevState, ...nextState } ),
{}
);
const { resolution } = attributes;
const resetAll = ( resetFilters = [] ) => {
let newAttributes = {};

resetFilters.forEach( ( resetFilter ) => {
newAttributes = {
...newAttributes,
...resetFilter( newAttributes ),
};
} );

setAttributes( newAttributes );
onChangeProp( undefined );
};
return (
<Panel>
<ToolsPanel panelId={ panelId } resetAll={ resetAll }>
<ToolsPanel
label={ label }
panelId={ panelId }
resetAll={ resetAll }
>
<ResolutionTool
panelId={ panelId }
onChange={ ( newValue ) => {
setResolution( newValue );
setAttributes( { resolution: newValue } );
onChangeProp( newValue );
} }
value={ resolution }
resetAllFilter={ () => ( {
resolution: undefined,
} ) }
{ ...props }
/>
</ToolsPanel>
</Panel>
);
};
Default.args = {
label: 'Settings',
defaultValue: 'full',
panelId: 'panel-id',
};

1 comment on commit c0329d9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in c0329d9.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12500961509
📝 Reported issues:

Please sign in to comment.