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

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';

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

const meta = {
title: 'BlockEditor/UnitControl',
component: UnitControl,
parameters: {
docs: {
canvas: { sourceState: 'shown' },
description: {
component:
'UnitControl allows the user to set a numeric quantity as well as a unit ',
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
argTypes: {
onChange: {
action: 'onChange',
description: 'Callback function when the value changes.',
table: {
type: { summary: 'function' },
},
},
onUnitChange: {
action: 'onUnitChange',
description: 'Callback function when the unit changes.',
table: {
type: { summary: 'function' },
},
},
labelPosition: {
control: 'radio',
options: [ 'top', 'side', 'bottom' ],
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
description: 'The position of the label.',
table: {
type: { summary: 'string' },
defaultValue: { summary: 'top' },
},
},
label: {
control: 'text',
description: 'The label for the control.',
table: {
type: { summary: 'string' },
},
},
value: {
control: 'text',
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
description: 'The value of the control.',
table: {
type: { summary: 'string' },
},
},
size: {
control: 'radio',
options: [ 'default', 'small' ],
description: 'The size of the control.',
table: {
type: { summary: 'string' },
defaultValue: { summary: 'default' },
},
},
disabled: {
control: 'boolean',
description: 'Whether the control is disabled.',
table: {
type: { summary: 'boolean' },
defaultValue: { summary: false },
},
},
},
};

export default meta;

export const Default = {
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 }
/>
);
},
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
};
Loading