Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Fix the 'Reset all' bug for the 'ResolutionTool' component #68296

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } ),
{}
);
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
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',
};
Loading