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 1 commit
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.

76 changes: 76 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,76 @@
/**
* External dependencies
*/
import type { Meta, StoryFn } from '@storybook/react';

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

/**
* Internal dependencies
*/
import BoxControl from '../';
import type { BoxControlValue } from '../types';

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 } }

onChange: { action: 'onChange' },
ciampo marked this conversation as resolved.
Show resolved Hide resolved
},
parameters: {
controls: { expanded: true },
docs: { canvas: { sourceState: 'shown' } },
},
};
export default meta;

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

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

Choose a reason for hiding this comment

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

No need to use BoxControlValue, we can use directly

Suggested change
useState< BoxControlValue >( defaultSideValues );
useState< ( typeof props )[ 'values' ] >( defaultSideValues );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting! I tried (typeof BoxControl)[ 'values' ] without success. Not sure I understand why that didn't work as well 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

typeof BoxControl would return the component type (I believe it would tell you if the component is a class component, a function component, a JSX intrinsic string...), but that type doesn't have the prop types as properties.

On the other hand, typeof props returns the computed type for the properties of the BoxControl component — and that type has the value property.

Copy link
Contributor

Choose a reason for hiding this comment

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

Expanding on this, an alternative version is also to grab the type of the props by using the React.ComponentProps utility type, which requires as argument the component type :

React.ComponentProps< typeof BoxControl >[ 'values' ]

I'm ok with keeping ( typeof props )[ 'values' ], but I hope this helps with understanding these types!


return (
<BoxControl
label="Padding"
ciampo marked this conversation as resolved.
Show resolved Hide resolved
values={ values }
{ ...props }
onChange={ ( nextValue ) => setValues( nextValue ) }
ciampo marked this conversation as resolved.
Show resolved Hide resolved
/>
);
};

export const Default = Template.bind( {} );
Default.args = {
label: 'Box Control',
values: undefined,
ciampo marked this conversation as resolved.
Show resolved Hide resolved
};

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

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

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

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