diff --git a/src/components/Checkbox/Checkbox.stories.tsx b/src/components/Checkbox/Checkbox.stories.tsx index 9c87845dc8..7cacc69209 100644 --- a/src/components/Checkbox/Checkbox.stories.tsx +++ b/src/components/Checkbox/Checkbox.stories.tsx @@ -2,29 +2,18 @@ import type { StoryObj, Meta } from '@storybook/react'; import React from 'react'; import { Checkbox } from './Checkbox'; -const defaultArgs = { - disabled: false, - label: 'Checkbox', -}; - const meta: Meta = { title: 'Components/Checkbox', component: Checkbox, - args: defaultArgs, + args: { + disabled: false, + label: 'Checkbox', + }, parameters: { badges: ['1.0'], }, - decorators: [ - (Story) => ( -
- {Story()} -
- ), - ], + decorators: [(Story) =>
{Story()}
], }; export default meta; @@ -55,60 +44,22 @@ export const MediumChecked: Story = { }, }; -export const Large: Story = { - ...Default, - args: { - size: 'lg', - }, -}; - -export const LargeChecked: Story = { - ...Large, - args: { - ...Checked.args, - ...Large.args, - }, -}; - export const Indeterminate: Story = { args: { indeterminate: true, }, }; +/** + * `Checkbox` can be disabled in each available state. + */ export const Disabled: Story = { - render: () => ( - - - {/* Un-checked */} - - - - - {/* Checked */} - - - - - {/* Indeterminate */} - - - - - -
- - - -
- - - -
- - - -
+ render: (args) => ( +
+ + + +
), parameters: { axe: { @@ -117,35 +68,23 @@ export const Disabled: Story = { }, }; +/** + * `Checkbox` doesn't require a visible label if `aria-label` is provided. + */ export const WithoutVisibleLabel: Story = { args: { 'aria-label': 'a checkbox has no name', label: undefined, }, - render: (args) => ( -
- - - - - - -
- ), }; -export const LongLabels = { - render: () => { - const label = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'; - - return ( -
- - - - -
- ); +/** + * Long labels will sit adjacent to the text box, and allow clicking to change the state of the checkbox. When constrained, + * the text will wrap, fixing the checkbox to the top edge. + */ +export const LongLabels: Story = { + args: { + label: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', }, parameters: { axe: { @@ -153,21 +92,3 @@ export const LongLabels = { }, }, }; - -export const WithCustomPositioning = { - parameters: { - docs: { - source: { - type: 'dynamic', - }, - }, - }, - render: () => ( -
- - Label on Left - - -
- ), -}; diff --git a/src/components/Checkbox/Checkbox.tsx b/src/components/Checkbox/Checkbox.tsx index e3d2675c72..da717ac048 100644 --- a/src/components/Checkbox/Checkbox.tsx +++ b/src/components/Checkbox/Checkbox.tsx @@ -3,6 +3,7 @@ import React, { forwardRef } from 'react'; import useForwardedRef from '../../util/useForwardedRef'; import { useId } from '../../util/useId'; import type { EitherInclusive } from '../../util/utility-types'; +import type { Size } from '../../util/variant-types'; import { InputLabel, type InputLabelProps } from '../InputLabel/InputLabel'; import styles from './Checkbox.module.css'; @@ -42,9 +43,11 @@ type CheckboxProps = Omit & { */ id?: string; /** - * Size of the checkbox label. + * Size of the checkbox and associated label. + * + * **Defaults to 'lg'.** */ - size?: CheckboxLabelProps['size']; + size?: Extract; } & EitherInclusive< { /** @@ -89,8 +92,6 @@ const CheckboxInput = React.forwardRef( }, ); -CheckboxInput.displayName = 'CheckboxInput'; - const CheckboxLabel = ({ className, size, ...other }: CheckboxLabelProps) => { const componentClassName = clsx( size === 'md' && styles['checkbox__label--md'], @@ -103,9 +104,10 @@ const CheckboxLabel = ({ className, size, ...other }: CheckboxLabelProps) => { /** * `import {Checkbox} from "@chanzuckerberg/eds";` * - * Checkbox control indicating if something is selected or unselected. + * Checkbox control indicating if something is selected or unselected. Uncontrolled by default, + * it can be used in place of boolean-like form data. * - * NOTE: Requires either a visible label or `aria-label` prop. + * **NOTE**: Requires either a visible `label` or `aria-label` prop. */ export const Checkbox = Object.assign( forwardRef((props, ref) => { @@ -138,3 +140,4 @@ export const Checkbox = Object.assign( ); Checkbox.displayName = 'Checkbox'; +CheckboxInput.displayName = 'CheckboxInput'; diff --git a/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap b/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap index 4f69389d91..6209f78d2a 100644 --- a/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap +++ b/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap @@ -49,128 +49,59 @@ exports[` Disabled story renders snapshot 1`] = `
- - - - - - - - - - - - - - - -
-
- - -
-
-
- - -
-
-
- - -
-
-
- - -
-
-
- - -
-
-
- - -
-
+
+ + +
+
+ + +
+
+ + +
+
`; @@ -181,12 +112,12 @@ exports[` Disabled story renders snapshot 2`] = ` @@ -194,28 +125,6 @@ exports[` Disabled story renders snapshot 2`] = ` `; exports[` Indeterminate story renders snapshot 1`] = ` -
-
- - -
-
-`; - -exports[` Large story renders snapshot 1`] = `
@@ -237,7 +146,7 @@ exports[` Large story renders snapshot 1`] = `
`; -exports[` LargeChecked story renders snapshot 1`] = ` +exports[` LongLabels story renders snapshot 1`] = `
@@ -245,96 +154,20 @@ exports[` LargeChecked story renders snapshot 1`] = ` class="checkbox" >
`; -exports[` LongLabels story renders snapshot 1`] = ` -
-
-
- - -
-
- - -
-
- - -
-
- - -
-
-
-`; - exports[` Medium story renders snapshot 1`] = `
MediumChecked story renders snapshot 1`] = `
`; -exports[` WithCustomPositioning story renders snapshot 1`] = ` +exports[` WithoutVisibleLabel story renders snapshot 1`] = `
-
`; - -exports[` WithoutVisibleLabel story renders snapshot 1`] = ` -
-
-
- -
-
- -
-
- -
-
- -
-
- -
-
- -
-
-
-`; diff --git a/src/components/InputLabel/InputLabel.tsx b/src/components/InputLabel/InputLabel.tsx index 712a22a0ce..67aeaa1b88 100644 --- a/src/components/InputLabel/InputLabel.tsx +++ b/src/components/InputLabel/InputLabel.tsx @@ -1,5 +1,6 @@ import clsx from 'clsx'; import React from 'react'; +import type { Size } from '../../util/variant-types'; import styles from './InputLabel.module.css'; export type InputLabelProps = { @@ -16,9 +17,11 @@ export type InputLabelProps = { */ htmlFor: string; /** - * Size of the label. Defaults to lg. + * Size of the label. + * + * **Defaults to 'lg'.** */ - size?: 'md' | 'lg'; + size?: Extract; /** * Indicates disabled state of the input. */