diff --git a/packages/components/src/checkbox-control/stories/index.story.tsx b/packages/components/src/checkbox-control/stories/index.story.tsx index 5e85ca8fc863ca..45b95367fe23d0 100644 --- a/packages/components/src/checkbox-control/stories/index.story.tsx +++ b/packages/components/src/checkbox-control/stories/index.story.tsx @@ -1,16 +1,7 @@ -/** - * External dependencies - */ -import type { Meta, StoryFn } from '@storybook/react-vite'; - -/** - * WordPress dependencies - */ +import type { Meta, StoryObj } from '@storybook/react-vite'; import { useState } from '@wordpress/element'; - -/** - * Internal dependencies - */ +import { wordpress } from '@wordpress/icons'; +import { Icon, Stack } from '@wordpress/ui'; import CheckboxControl from '..'; import { VStack } from '../../v-stack'; import { HStack } from '../../h-stack'; @@ -44,15 +35,17 @@ const meta: Meta< typeof CheckboxControl > = { }; export default meta; -const DefaultTemplate: StoryFn< typeof CheckboxControl > = ( { +type Story = StoryObj< typeof CheckboxControl >; + +function ControlledCheckboxControl( { onChange, - ...args -} ) => { + ...props +}: React.ComponentProps< typeof CheckboxControl > ) { const [ isChecked, setChecked ] = useState( true ); return ( { setChecked( v ); @@ -60,65 +53,68 @@ const DefaultTemplate: StoryFn< typeof CheckboxControl > = ( { } } /> ); -}; +} +ControlledCheckboxControl.displayName = 'CheckboxControl'; -export const Default: StoryFn< typeof CheckboxControl > = DefaultTemplate.bind( - {} -); -Default.args = { - label: 'Is author', - help: 'Is the user an author or not?', +export const Default: Story = { + render: ( args ) => , + args: { + label: 'Is author', + help: 'Is the user an author or not?', + }, }; -export const Indeterminate: StoryFn< typeof CheckboxControl > = ( { - onChange, - ...args -} ) => { - const [ fruits, setFruits ] = useState( { apple: false, orange: false } ); +export const Indeterminate: Story = { + render: ( { onChange, ...args } ) => { + const [ fruits, setFruits ] = useState( { + apple: false, + orange: false, + } ); - const isAllChecked = Object.values( fruits ).every( Boolean ); - const isIndeterminate = - Object.values( fruits ).some( Boolean ) && ! isAllChecked; + const isAllChecked = Object.values( fruits ).every( Boolean ); + const isIndeterminate = + Object.values( fruits ).some( Boolean ) && ! isAllChecked; - return ( - - { - setFruits( { - apple: v, - orange: v, - } ); - onChange( v ); - } } - /> - - setFruits( ( prevState ) => ( { - ...prevState, - apple, - } ) ) - } - /> - - setFruits( ( prevState ) => ( { - ...prevState, - orange, - } ) ) - } - /> - - ); -}; -Indeterminate.args = { - label: 'Select all', + return ( + + { + setFruits( { + apple: v, + orange: v, + } ); + onChange( v ); + } } + /> + + setFruits( ( prevState ) => ( { + ...prevState, + apple, + } ) ) + } + /> + + setFruits( ( prevState ) => ( { + ...prevState, + orange, + } ) ) + } + /> + + ); + }, + args: { + label: 'Select all', + }, }; /** @@ -131,35 +127,67 @@ Indeterminate.args = { * Similarly, a custom description can be added by omitting the `help` prop * and using the `aria-describedby` prop instead. */ -export const WithCustomLabel: StoryFn< typeof CheckboxControl > = ( { - onChange, - ...args -} ) => { - const [ isChecked, setChecked ] = useState( true ); +export const WithCustomLabel: Story = { + render: ( { onChange, ...args } ) => { + const [ isChecked, setChecked ] = useState( true ); - return ( - - { - setChecked( v ); - onChange( v ); + return ( + + { + setChecked( v ); + onChange( v ); + } } + // Disable reason: For simplicity of the code snippet. + // eslint-disable-next-line no-restricted-syntax + id="my-checkbox-with-custom-label" + aria-describedby="my-custom-description" + /> + + + { /* eslint-disable-next-line no-restricted-syntax */ } +
+ A custom description. +
+
+
+ ); + }, +}; + +/** + * When adding a visual aid, prefer placing it at the trailing end of the row, + * rather than placing it directly before the label, or moving the checkbox to the trailing end. + */ +export const WithVisual: Story = { + ...Default, + render: ( args ) => ( + + + + + - - - { /* eslint-disable-next-line no-restricted-syntax */ } -
- A custom description. -
-
-
- ); + > + + + + ), + args: { + ...Default.args, + help: 'Additional context that helps users understand what this setting does and when they might want to turn it on or off.', + }, };