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

BoxControl: Update story and refactor to Typescript #56462

Merged
merged 4 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
75 changes: 0 additions & 75 deletions packages/components/src/box-control/stories/index.story.js

This file was deleted.

90 changes: 90 additions & 0 deletions packages/components/src/box-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* External dependencies
*/
import type { Meta, StoryFn } from '@storybook/react';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import BoxControl from '../';

const meta: Meta< typeof BoxControl > = {
title: 'Components (Experimental)/BoxControl',
component: BoxControl,
argTypes: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the example Template below controls the BoxControl by passing it the value prop, we should disable controls for the value prop

Copy link
Contributor

Choose a reason for hiding this comment

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

The controls for the values prop are still enabled

Screenshot 2023-11-24 at 14 04 20

I believe that the right setting is values: { control: { type: null } }

values: { control: null },
},
parameters: {
actions: { argTypesRegex: '^on.*' },
controls: { expanded: true },
docs: { canvas: { sourceState: 'shown' } },
},
};
export default meta;

const defaultSideValues = {
top: '10px',
right: '10px',
bottom: '10px',
left: '10px',
};

const TemplateUncontrolled: StoryFn< typeof BoxControl > = ( props ) => {
return <BoxControl { ...props } />;
};

const TemplateControlled: StoryFn< typeof BoxControl > = ( props ) => {
const [ values, setValues ] =
useState< ( typeof props )[ 'values' ] >( defaultSideValues );
Copy link
Contributor

@ciampo ciampo Nov 24, 2023

Choose a reason for hiding this comment

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

While testing that the onChange callback was being shown correctly in the actions tab in Storybook, I realised that the values being reported looked incorrect.

For example, interacting with BoxControl in the "Single Side" example should result in only changing the bottom value, but with the changes from this PR, the resulting value includes values also for the remaining sides:

Screenshot 2023-11-24 at 14 23 49

After looking into this, I realised that this is happening because we're initializing the component always with the same defaultValues object, which includes values for all sides. It seems that BoxControl only updates the side that gets updated, and leaves the remaining sides untouched.

Prior to these changes, each example had its own specific default value, which prevented this issue from manifesting.

An easy fix for now is to avoid passing any default values

Suggested change
const [ values, setValues ] =
useState< ( typeof props )[ 'values' ] >( defaultSideValues );
const [ values, setValues ] =
const [ values, setValues ] = useState< ( typeof props )[ 'values' ] >();

Definitely not in the scope of this PR, although I wonder if the component should somehow "validate" the incoming value, and compare it against the sides prop to see if there is any mismatch? Probably something with very low-priority, though, since it doesn't look like this component is being used much across the editor.


return (
<BoxControl
values={ values }
{ ...props }
onChange={ ( nextValue ) => {
setValues( nextValue );
props.onChange?.( nextValue );
} }
/>
);
};

export const Default = TemplateUncontrolled.bind( {} );
Default.args = {
label: 'Label',
};

export const Controlled = TemplateControlled.bind( {} );
Controlled.args = {
...Default.args,
};

export const ArbitrarySides = TemplateControlled.bind( {} );
ArbitrarySides.args = {
...Default.args,
sides: [ 'top', 'bottom' ],
};

export const SingleSide = TemplateControlled.bind( {} );
SingleSide.args = {
...Default.args,
sides: [ 'bottom' ],
};

export const AxialControls = TemplateControlled.bind( {} );
AxialControls.args = {
...Default.args,
splitOnAxis: true,
};

export const AxialControlsWithSingleSide = TemplateControlled.bind( {} );
AxialControlsWithSingleSide.args = {
...Default.args,
sides: [ 'horizontal' ],
splitOnAxis: true,
};
Loading