Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Menu): allow MenuItem at any level #3247

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 0 additions & 29 deletions packages/react-ui/components/DropdownMenu/DropdownMenu.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,32 +218,3 @@ const renderCaption = ({ openMenu, closeMenu }) => {
</MenuItem>
</DropdownMenu>
```

Условный рендер элементов меню (с сохранением поведения [MenuItem](#/Components/MenuItem)).

```jsx harmony
import {Button, MenuItem, Gapped, MenuSeparator} from '@skbkontur/react-ui';

const [showItems, setShowItems] = React.useState(false);

const hiddenItems = [
<MenuSeparator />,
<MenuItem>А я скрываюсь</MenuItem>,
<MenuItem>И я</MenuItem>,
<MenuItem>Я с вами</MenuItem>,
];


<Gapped>
<Button onClick={() => setShowItems(!showItems)}>
{showItems ? 'Спрятать' : 'Показать'} элементы
</Button>

<DropdownMenu caption={<Button use="primary">Открыть меню</Button>}>
<MenuItem>Меня видно всегда</MenuItem>
<MenuItem>Меня тоже</MenuItem>
<MenuItem>Ага, и меня!</MenuItem>
{showItems && hiddenItems}
</DropdownMenu>
</Gapped>
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import React, { useState } from 'react';

import { delay } from '../../../lib/utils';
import { Button } from '../../Button';
import { CreeveyTests, Meta } from '../../../typings/stories';
import { DropdownMenu } from '../DropdownMenu';
import { MenuHeader } from '../../../components/MenuHeader';
import { MenuItem } from '../../../components/MenuItem';
import { MenuHeader } from '../../MenuHeader';
import { MenuItem } from '../../MenuItem';
import { OkIcon } from '../../../internal/icons/16px';
import { PopupMenuDataTids } from '../../../internal/PopupMenu';

Expand Down Expand Up @@ -74,3 +74,64 @@ WithItemsAndIconsWithoutTextAlignment.parameters = {
skip: { 'themes dont affect logic': { in: /^(?!\bchrome\b)/ } },
},
};

const navigateInNestedMenuItems: CreeveyTests = {
async navigate() {
await this.browser
.actions({
bridge: true,
})
.click(this.browser.findElement({ css: `[data-tid~="${PopupMenuDataTids.caption}"]` }))
.sendKeys(this.keys.DOWN)
.sendKeys(this.keys.DOWN)
.perform();
const arrowDown = await this.browser.takeScreenshot();

await this.browser
.actions({
bridge: true,
})
.sendKeys(this.keys.ENTER)
.perform();
await delay(1000);
const enter = await this.browser.takeScreenshot();

await this.browser
.actions({
bridge: true,
})
.click(this.browser.findElement({ css: `[data-tid~="${PopupMenuDataTids.caption}"]` }))
.sendKeys(this.keys.DOWN)
.sendKeys(this.keys.DOWN)
.sendKeys(this.keys.DOWN)
.sendKeys(this.keys.DOWN)
.perform();
const circle = await this.browser.takeScreenshot();

await this.expect({ arrowDown, enter, circle }).to.matchImages();
},
};

export const WithNestedMenuItems = () => {
const [caption, setCaption] = useState('not selected');
const onClick = () => {
setCaption('selected');
};
return (
<DropdownMenu menuWidth="300px" caption={<Button use="primary">{caption}</Button>}>
<>
<div>
<MenuItem>Раз</MenuItem>
<MenuItem onClick={onClick}>Два</MenuItem>
</div>
<MenuItem>Три</MenuItem>
</>
</DropdownMenu>
);
};
WithNestedMenuItems.storyName = 'With nested menu items';
WithNestedMenuItems.parameters = {
creevey: {
tests: navigateInNestedMenuItems,
},
};
6 changes: 5 additions & 1 deletion packages/react-ui/components/MenuHeader/MenuHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { ReactNode, useContext } from 'react';
import { ThemeContext } from '../../lib/theming/ThemeContext';
import { CommonProps, CommonWrapper } from '../../internal/CommonWrapper';
import { cx } from '../../lib/theming/Emotion';
import { MenuContext } from '../../internal/Menu/MenuContext';

import { styles } from './MenuHeader.styles';

Expand All @@ -24,14 +25,17 @@ export const MenuHeaderDataTids = {
*/
function MenuHeader({ _enableIconPadding = false, children, ...rest }: MenuHeaderProps) {
const theme = useContext(ThemeContext);
const menuContext = useContext(MenuContext);

const enableIconPadding = _enableIconPadding || menuContext.enableIconPadding;

return (
<CommonWrapper {...rest}>
<div
data-tid={MenuHeaderDataTids.root}
className={cx({
[styles.root(theme)]: true,
[styles.withLeftPadding(theme)]: _enableIconPadding,
[styles.withLeftPadding(theme)]: enableIconPadding,
})}
>
{children}
Expand Down
10 changes: 8 additions & 2 deletions packages/react-ui/components/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { ThemeContext } from '../../lib/theming/ThemeContext';
import { Theme } from '../../lib/theming/Theme';
import { CommonProps, CommonWrapper, CommonWrapperRestProps } from '../../internal/CommonWrapper';
import { cx } from '../../lib/theming/Emotion';
import { rootNode, TSetRootNode } from '../../lib/rootNode/rootNodeDecorator';
import { rootNode, TSetRootNode } from '../../lib/rootNode';
import { MenuContext, MenuContextType } from '../../internal/Menu/MenuContext';

import { styles } from './MenuItem.styles';

Expand Down Expand Up @@ -132,6 +133,9 @@ export class MenuItem extends React.Component<MenuItemProps> {
private setRootNode!: TSetRootNode;
private rootRef: Nullable<HTMLElement> = null;

public static contextType = MenuContext;
public context: MenuContextType = this.context;

public render() {
return (
<ThemeContext.Consumer>
Expand Down Expand Up @@ -183,14 +187,16 @@ export class MenuItem extends React.Component<MenuItemProps> {
);
}

const enableIconPadding = Boolean(iconElement) || !!_enableIconPadding || this.context.enableIconPadding;

const className = cx({
[styles.root(this.theme)]: true,
[styles.rootMobile(this.theme)]: isMobile,
[styles.loose()]: !!loose,
[styles.hover(this.theme)]: hover,
[styles.selected(this.theme)]: state === 'selected',
[styles.link(this.theme)]: !!link,
[styles.withIcon(this.theme)]: Boolean(iconElement) || !!_enableIconPadding,
[styles.withIcon(this.theme)]: enableIconPadding,
[styles.disabled(this.theme)]: !!this.props.disabled,
});

Expand Down
Loading