BoxControl: Update story and refactor to Typescript#56462
Conversation
|
Size Change: +1.02 kB (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
| const meta: Meta< typeof BoxControl > = { | ||
| title: 'Components (Experimental)/BoxControl', | ||
| component: BoxControl, | ||
| argTypes: { |
There was a problem hiding this comment.
Since the example Template below controls the BoxControl by passing it the value prop, we should disable controls for the value prop
|
|
||
| const Template: StoryFn< typeof BoxControl > = ( props ) => { | ||
| const [ values, setValues ] = | ||
| useState< BoxControlValue >( defaultSideValues ); |
There was a problem hiding this comment.
No need to use BoxControlValue, we can use directly
| useState< BoxControlValue >( defaultSideValues ); | |
| useState< ( typeof props )[ 'values' ] >( defaultSideValues ); |
There was a problem hiding this comment.
Interesting! I tried (typeof BoxControl)[ 'values' ] without success. Not sure I understand why that didn't work as well 🤔
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
| const [ values, setValues ] = | ||
| useState< ( typeof props )[ 'values' ] >( defaultSideValues ); |
There was a problem hiding this comment.
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:
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
| 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.
ciampo
left a comment
There was a problem hiding this comment.
Preemptively approving, since this PR should be good to be merged after remaining open feedback (#56462 (comment), #56462 (comment)) is addressed 🚀


What?
Via #56185 (comment) it was found that
BoxControl's story hasn't been updated to use new storybook features. This required updating to Typescript as well.Why?
For consistency and easier use of the docs.
How?
Refactoring to Typescript, following our storybook standards by adding a meta object, and using the template and args format.
Testing Instructions
npm run: storybook devBoxControlexamples behave and look the same as before