From 17c114a74ae376809c902cba9ba672c26896dcac Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 25 Dec 2024 16:27:45 +0530 Subject: [PATCH] merge trunk into storybook/resolution-control --- .../resolution-tool/stories/index.story.js | 156 ++++++++++++------ 1 file changed, 102 insertions(+), 54 deletions(-) diff --git a/packages/block-editor/src/components/resolution-tool/stories/index.story.js b/packages/block-editor/src/components/resolution-tool/stories/index.story.js index 75db0f90a568b..c69f3a0df93bb 100644 --- a/packages/block-editor/src/components/resolution-tool/stories/index.story.js +++ b/packages/block-editor/src/components/resolution-tool/stories/index.story.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { useReducer } from '@wordpress/element'; +import { useState, useEffect } from '@wordpress/element'; import { Panel, __experimentalToolsPanel as ToolsPanel, @@ -10,66 +10,114 @@ import { /** * Internal dependencies */ -import ResolutionTool from '..'; +import ResolutionTool from '../'; -export default { - title: 'BlockEditor (Private APIs)/ResolutionControl', +const meta = { + title: 'BlockEditor/ResolutionTool', component: ResolutionTool, + parameters: { + docs: { + canvas: { sourceState: 'shown' }, + description: { + component: + 'A control for selecting image resolution with preset size options.', + }, + }, + }, argTypes: { - panelId: { control: false }, - onChange: { action: 'changed' }, + value: { + control: 'radio', + options: [ 'thumbnail', 'medium', 'large', 'full' ], + description: 'Currently selected resolution value.', + table: { + type: { summary: 'string' }, + defaultValue: { summary: 'thumbnail' }, + }, + }, + onChange: { + action: 'onChange', + control: { type: null }, + description: 'Handles change in resolution selection.', + table: { + type: { summary: 'function' }, + }, + }, + options: { + control: 'object', + description: 'Array of resolution options to display.', + table: { + type: { summary: 'array' }, + defaultValue: { + summary: JSON.stringify( [ + { label: 'Thumbnail', value: 'thumbnail' }, + { label: 'Medium', value: 'medium' }, + { label: 'Large', value: 'large' }, + { label: 'Full Size', value: 'full' }, + ] ), + }, + }, + }, + defaultValue: { + control: 'radio', + options: [ 'thumbnail', 'medium', 'large', 'full' ], + description: 'Default resolution value.', + table: { + type: { summary: 'string' }, + defaultValue: { summary: 'thumbnail' }, + }, + }, + isShownByDefault: { + control: 'boolean', + description: + 'Whether the control is shown by default in the panel.', + table: { + type: { summary: 'boolean' }, + defaultValue: { summary: true }, + }, + }, + panelId: { + control: { type: null }, + description: 'ID of the parent tools panel.', + table: { + type: { summary: 'string' }, + }, + }, }, }; -export const Default = ( { - label, - panelId, - onChange: onChangeProp, - ...props -} ) => { - const [ attributes, setAttributes ] = useReducer( - ( prevState, nextState ) => ( { ...prevState, ...nextState } ), - {} - ); - const { resolution } = attributes; - const resetAll = ( resetFilters = [] ) => { - let newAttributes = {}; +export default meta; + +export const Default = { + render: function Template( { onChange, defaultValue, value, ...args } ) { + const [ resolution, setResolution ] = useState( value ?? defaultValue ); + + useEffect( () => { + if ( value !== resolution ) { + setResolution( value ); + } + }, [ value ] ); - resetFilters.forEach( ( resetFilter ) => { - newAttributes = { - ...newAttributes, - ...resetFilter( newAttributes ), - }; - } ); + const handleChange = ( newValue ) => { + setResolution( newValue ); + onChange?.( newValue ); + }; - setAttributes( newAttributes ); - onChangeProp( undefined ); - }; - return ( - - - { - setAttributes( { resolution: newValue } ); - onChangeProp( newValue ); + return ( + + { + setResolution( defaultValue ); + onChange?.( defaultValue ); } } - value={ resolution } - resetAllFilter={ () => ( { - resolution: undefined, - } ) } - { ...props } - /> - - - ); -}; -Default.args = { - label: 'Settings', - defaultValue: 'full', - panelId: 'panel-id', + > + + + + ); + }, };