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

Storybook: Add UnitControl story #67346

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

im3dabasia
Copy link
Contributor

@im3dabasia im3dabasia commented Nov 27, 2024

Part of #67165

What?

This PR will add stories for UnitControl component in the Storybook.

Why?

As part of the ongoing effort to improve component documentation and testing (tracked in #22891), we need comprehensive stories for all Block Editor components.

Testing Instructions

  1. Run npm run storybook:dev
  2. Open the storybook on http://localhost:50240/
  3. Check the UnitControl stories.

Screenshots or screencast

Screen.Recording.2024-11-27.at.4.57.25.PM.mov

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 27, 2024
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @im3dabasia! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@im3dabasia im3dabasia marked this pull request as ready for review November 27, 2024 11:32
@im3dabasia im3dabasia requested a review from ellatrix as a code owner November 27, 2024 11:32
Copy link

github-actions bot commented Nov 27, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: im3dabasia <[email protected]>
Co-authored-by: t-hamano <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@akasunil akasunil added [Type] Developer Documentation Documentation for developers Storybook Storybook and its stories for components labels Nov 28, 2024
@miminari miminari requested review from jsnajdr and ItsJonQ and removed request for ellatrix November 29, 2024 00:17
@im3dabasia im3dabasia force-pushed the storybook/unit-control branch from cf31838 to da9cedf Compare December 20, 2024 12:55
@im3dabasia
Copy link
Contributor Author

Hey @t-hamano,

I have refactored this PR to align with the patterns suggested in the issue. Please review my work when you have a moment. Thank you!

Screen.Recording.2024-12-23.at.2.42.02.PM.mov

@im3dabasia
Copy link
Contributor Author

Would it be a good idea to show a story with custom units? I had considered adding this but have removed it for now. Let me know if it makes sense to include a story for this.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

We may also want to add the following three props to match the README:

  • disabledUnits
  • isPressEnterToChange
  • isUnitSelectTabbable

canvas: { sourceState: 'shown' },
description: {
component:
'UnitControl allows the user to set a numeric quantity as well as a unit ',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
'UnitControl allows the user to set a numeric quantity as well as a unit ',
'UnitControl allows the user to set a numeric quantity as well as a unit.',

Comment on lines +84 to +106
render: function Template( { onChange, onUnitChange, value, ...args } ) {
const [ componentValue, setComponentValue ] = useState( value || '' );

useEffect( () => {
setComponentValue( value );
}, [ value ] );

const handleValueChange = ( newValue ) => {
setComponentValue( newValue );
if ( onChange ) {
onChange( newValue );
}
};

return (
<UnitControl
{ ...args }
value={ componentValue }
onChange={ handleValueChange }
onUnitChange={ onUnitChange }
/>
);
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
render: function Template( { onChange, onUnitChange, value, ...args } ) {
const [ componentValue, setComponentValue ] = useState( value || '' );
useEffect( () => {
setComponentValue( value );
}, [ value ] );
const handleValueChange = ( newValue ) => {
setComponentValue( newValue );
if ( onChange ) {
onChange( newValue );
}
};
return (
<UnitControl
{ ...args }
value={ componentValue }
onChange={ handleValueChange }
onUnitChange={ onUnitChange }
/>
);
},
render: function Template( { onChange, ...args } ) {
const [ value, setValue ] = useState();
return (
<UnitControl
{ ...args }
value={ value }
onChange={ ( ...changeArgs ) => {
onChange( ...changeArgs );
setValue( ...changeArgs );
} }
/>
);
},
args: {
label: 'Label',
},

Is there a reason you implemented it this way? The above implementation should work too.

At the same time, it might be a good idea to display a default label, just like with the components package.

},
labelPosition: {
control: 'radio',
options: [ 'top', 'side', 'bottom' ],
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
options: [ 'top', 'side', 'bottom' ],
options: [ 'top', 'side', 'bottom', 'edge' ],

},
},
value: {
control: 'text',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
control: 'text',
control: { type: null },

I believe the control is not necessary since the value can be updated directly via the UI.

@t-hamano
Copy link
Contributor

Would it be a good idea to show a story with custom units?

This component is a kind of wrapper component for the UnitControl component in the components package. And this UnitControl component exported by the block-editor package doesn't seem to be used anywhere in Gutenberg right now.

Considering this, I think one story is enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository Storybook Storybook and its stories for components [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants