Skip to content
Merged
Changes from all 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
226 changes: 127 additions & 99 deletions packages/components/src/checkbox-control/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -44,81 +35,86 @@ 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 (
<CheckboxControl
{ ...args }
{ ...props }
checked={ isChecked }
onChange={ ( v ) => {
setChecked( v );
onChange( v );
} }
/>
);
};
}
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 ) => <ControlledCheckboxControl { ...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 (
<VStack>
<CheckboxControl
{ ...args }
checked={ isAllChecked }
indeterminate={ isIndeterminate }
onChange={ ( v ) => {
setFruits( {
apple: v,
orange: v,
} );
onChange( v );
} }
/>
<CheckboxControl
label="Apple"
checked={ fruits.apple }
onChange={ ( apple ) =>
setFruits( ( prevState ) => ( {
...prevState,
apple,
} ) )
}
/>
<CheckboxControl
label="Orange"
checked={ fruits.orange }
onChange={ ( orange ) =>
setFruits( ( prevState ) => ( {
...prevState,
orange,
} ) )
}
/>
</VStack>
);
};
Indeterminate.args = {
label: 'Select all',
return (
<VStack>
<CheckboxControl
{ ...args }
checked={ isAllChecked }
indeterminate={ isIndeterminate }
onChange={ ( v ) => {
setFruits( {
apple: v,
orange: v,
} );
onChange( v );
} }
/>
<CheckboxControl
label="Apple"
checked={ fruits.apple }
onChange={ ( apple ) =>
setFruits( ( prevState ) => ( {
...prevState,
apple,
} ) )
}
/>
<CheckboxControl
label="Orange"
checked={ fruits.orange }
onChange={ ( orange ) =>
setFruits( ( prevState ) => ( {
...prevState,
orange,
} ) )
}
/>
</VStack>
);
},
args: {
label: 'Select all',
},
};

/**
Expand 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 (
<HStack justify="flex-start" alignment="top" spacing={ 0 }>
<CheckboxControl
{ ...args }
checked={ isChecked }
onChange={ ( v ) => {
setChecked( v );
onChange( v );
return (
<HStack justify="flex-start" alignment="top" spacing={ 0 }>
<CheckboxControl
{ ...args }
checked={ isChecked }
onChange={ ( v ) => {
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"
/>
<VStack>
<label htmlFor="my-checkbox-with-custom-label">
My custom label
</label>
{ /* eslint-disable-next-line no-restricted-syntax */ }
<div id="my-custom-description" style={ { fontSize: 13 } }>
A custom description.
</div>
</VStack>
</HStack>
);
},
};

/**
* 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 ) => (
<Stack gap="md" align="flex-start" justify="space-between">
<Stack>
<ControlledCheckboxControl { ...args } />
</Stack>
<Stack
align="center"
justify="center"
style={ {
backgroundColor:
'var(--wpds-color-background-surface-neutral-weak)',
borderRadius: 'var(--wpds-border-radius-md)',
flexShrink: 0,
height: 'var(--wpds-dimension-size-lg)',
width: 'var(--wpds-dimension-size-lg)',
} }
// 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"
/>
<VStack>
<label htmlFor="my-checkbox-with-custom-label">
My custom label
</label>
{ /* eslint-disable-next-line no-restricted-syntax */ }
<div id="my-custom-description" style={ { fontSize: 13 } }>
A custom description.
</div>
</VStack>
</HStack>
);
>
<Icon icon={ wordpress } />
</Stack>
</Stack>
),
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.',
},
};
Loading