-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ToggleGroupControl: Improve stories for Docs view (#43265)
* ToggleGroupControl: Improve stories for Docs view * Add usage clarification re: TabPanel * Add changelog * Mark as non-polymorphic
- Loading branch information
Showing
6 changed files
with
141 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 0 additions & 211 deletions
211
packages/components/src/toggle-group-control/stories/index.js
This file was deleted.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
packages/components/src/toggle-group-control/stories/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
import { formatLowercase, formatUppercase } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
ToggleGroupControl, | ||
ToggleGroupControlOption, | ||
ToggleGroupControlOptionIcon, | ||
} from '../index'; | ||
import type { | ||
ToggleGroupControlOptionProps, | ||
ToggleGroupControlOptionIconProps, | ||
ToggleGroupControlProps, | ||
} from '../types'; | ||
|
||
const meta: ComponentMeta< typeof ToggleGroupControl > = { | ||
component: ToggleGroupControl, | ||
title: 'Components (Experimental)/ToggleGroupControl', | ||
subcomponents: { ToggleGroupControlOption, ToggleGroupControlOptionIcon }, | ||
argTypes: { | ||
help: { control: { type: 'text' } }, | ||
onChange: { action: 'onChange' }, | ||
value: { control: { type: null } }, | ||
}, | ||
parameters: { | ||
controls: { expanded: true }, | ||
docs: { source: { state: 'open' } }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const Template: ComponentStory< typeof ToggleGroupControl > = ( { | ||
onChange, | ||
...props | ||
} ) => { | ||
const [ value, setValue ] = | ||
useState< ToggleGroupControlProps[ 'value' ] >(); | ||
|
||
return ( | ||
<ToggleGroupControl | ||
{ ...props } | ||
onChange={ ( ...changeArgs ) => { | ||
setValue( ...changeArgs ); | ||
onChange?.( ...changeArgs ); | ||
} } | ||
value={ value } | ||
/> | ||
); | ||
}; | ||
|
||
const mapPropsToOptionComponent = ( { | ||
value, | ||
...props | ||
}: ToggleGroupControlOptionProps ) => ( | ||
<ToggleGroupControlOption value={ value } key={ value } { ...props } /> | ||
); | ||
|
||
const mapPropsToOptionIconComponent = ( { | ||
value, | ||
...props | ||
}: ToggleGroupControlOptionIconProps ) => ( | ||
<ToggleGroupControlOptionIcon value={ value } key={ value } { ...props } /> | ||
); | ||
|
||
export const Default: ComponentStory< typeof ToggleGroupControl > = | ||
Template.bind( {} ); | ||
Default.args = { | ||
children: [ | ||
{ value: 'left', label: 'Left' }, | ||
{ value: 'center', label: 'Center' }, | ||
{ value: 'right', label: 'Right' }, | ||
{ value: 'justify', label: 'Justify' }, | ||
].map( mapPropsToOptionComponent ), | ||
label: 'Label', | ||
}; | ||
|
||
/** | ||
* A tooltip can be shown for each option by enabling the `showTooltip` prop. | ||
* The `aria-label` will be used in the tooltip if provided. Otherwise, the | ||
* `label` will be used. | ||
*/ | ||
export const WithTooltip: ComponentStory< typeof ToggleGroupControl > = | ||
Template.bind( {} ); | ||
WithTooltip.args = { | ||
...Default.args, | ||
children: [ | ||
{ | ||
value: 'asc', | ||
label: 'A→Z', | ||
'aria-label': 'Ascending', | ||
showTooltip: true, | ||
}, | ||
{ | ||
value: 'desc', | ||
label: 'Z→A', | ||
'aria-label': 'Descending', | ||
showTooltip: true, | ||
}, | ||
].map( mapPropsToOptionComponent ), | ||
}; | ||
|
||
/** | ||
* The `ToggleGroupControlOptionIcon` component can be used for icon options. A `label` is required | ||
* on each option for accessibility, which will be shown in a tooltip. | ||
* | ||
* When using icon options within `ToggleGroupControl`, the `__experimentalIsIconGroup` style is preferred. | ||
*/ | ||
export const WithIcons: ComponentStory< typeof ToggleGroupControl > = | ||
Template.bind( {} ); | ||
WithIcons.args = { | ||
...Default.args, | ||
__experimentalIsIconGroup: true, | ||
children: [ | ||
{ value: 'uppercase', label: 'Uppercase', icon: formatUppercase }, | ||
{ value: 'lowercase', label: 'Lowercase', icon: formatLowercase }, | ||
].map( mapPropsToOptionIconComponent ), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.