Skip to content

Commit

Permalink
Generate default stories for MenuItem
Browse files Browse the repository at this point in the history
Add more stories for menu-item

Update MenuItem import index.story.tsx

Update packages/components/src/menu-item/index.tsx

Co-authored-by: Lena Morita <lena@jaguchi.com>
  • Loading branch information
bangank36 and mirka committed Aug 21, 2023
1 parent 70ccdef commit 4a2c2d6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/menu-item/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Determines where to display the provided `icon`.
- Type: `boolean`
- Required: No

Whether or not the menu item is currently selected.
Whether or not the menu item is currently selected, `isSelected` is only taken into account when the `role` prop is either `"menuitemcheckbox"` or `"menuitemradio"`.

### `shortcut`

Expand Down
7 changes: 5 additions & 2 deletions packages/components/src/menu-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Icon from '../icon';
import type { WordPressComponentProps } from '../ui/context';
import type { MenuItemProps } from './types';

export function MenuItem(
function UnforwardedMenuItem(
props: WordPressComponentProps< MenuItemProps, 'button', false >,
ref: ForwardedRef< HTMLButtonElement >
) {
Expand Down Expand Up @@ -97,6 +97,7 @@ export function MenuItem(
* <MenuItem
* icon={ isActive ? 'yes' : 'no' }
* isSelected={ isActive }
* role="menuitemcheckbox"
* onClick={ () => setIsActive( ( state ) => ! state ) }
* >
* Toggle
Expand All @@ -105,4 +106,6 @@ export function MenuItem(
* };
* ```
*/
export default forwardRef( MenuItem );
export const MenuItem = forwardRef( UnforwardedMenuItem );

export default MenuItem;
80 changes: 80 additions & 0 deletions packages/components/src/menu-item/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* External dependencies
*/
import type { Meta, StoryFn } from '@storybook/react';

/**
* WordPress dependencies
*/
import { link, more, check } from '@wordpress/icons';

/**
* Internal dependencies
*/
import MenuGroup from '../../menu-group';
import MenuItem from '..';
import Shortcut from '../../shortcut';

const meta: Meta< typeof MenuItem > = {
component: MenuItem,
title: 'Components/MenuItem',
argTypes: {
children: { control: { type: null } },
icon: {
control: { type: 'select' },
options: [ 'check', 'link', 'more' ],
mapping: {
check,
link,
more,
},
},
},
parameters: {
controls: {
expanded: true,
},
docs: { canvas: { sourceState: 'shown' } },
},
};
export default meta;

const Template: StoryFn< typeof MenuItem > = ( props ) => {
return (
<MenuGroup>
<MenuItem { ...props }>Menu Item 1</MenuItem>
</MenuGroup>
);
};

export const Default: StoryFn< typeof MenuItem > = Template.bind( {} );

/**
* When the `role` prop is either `"menuitemcheckbox"` or `"menuitemradio"`, the
* `isSelected` prop should be used so screen readers can tell which item is currently selected.
*/
export const IsSelected = Template.bind( {} );
IsSelected.args = {
...Default.args,
isSelected: true,
role: 'menuitemcheckbox',
};

export const WithIcon = Template.bind( {} );
WithIcon.args = {
...Default.args,
icon: link,
iconPosition: 'left',
};

export const WithInfo = Template.bind( {} );
WithInfo.args = {
...Default.args,
info: 'Menu Item description',
};

export const WithSuffix = Template.bind( {} );
WithSuffix.args = {
...Default.args,
suffix: <Shortcut shortcut="Ctrl+M"></Shortcut>,
};
5 changes: 3 additions & 2 deletions packages/components/src/menu-item/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { ReactNode } from 'react';
*/
import type { ButtonAsButtonProps } from '../button/types';

export type MenuItemProps = ButtonAsButtonProps & {
export type MenuItemProps = Pick< ButtonAsButtonProps, 'isDestructive' > & {
/**
* A CSS `class` to give to the container element.
*/
Expand All @@ -33,7 +33,8 @@ export type MenuItemProps = ButtonAsButtonProps & {
*/
iconPosition?: ButtonAsButtonProps[ 'iconPosition' ];
/**
* Whether or not the menu item is currently selected.
* Whether or not the menu item is currently selected, `isSelected` is only taken into
* account when the `role` prop is either `"menuitemcheckbox"` or `"menuitemradio"`.
*/
isSelected?: boolean;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ const ToolsPanelHeader = (
<MenuGroup>
<MenuItem
aria-disabled={ ! canResetAll }
// @ts-expect-error - TODO: If this "tertiary" style is something we really want to allow on MenuItem,
// we should rename it and explicitly allow it as an official API. All the other Button variants
// don't make sense in a MenuItem context, and should be disallowed.
variant={ 'tertiary' }
onClick={ () => {
if ( canResetAll ) {
Expand Down

0 comments on commit 4a2c2d6

Please sign in to comment.