Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
feat(UIKIT-1663,MenuItem): Добавлен note (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
pan1caisreal authored Aug 8, 2024
1 parent a89ad66 commit cfa6dd0
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
46 changes: 46 additions & 0 deletions packages/components/src/DropdownButton/DropdownButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,52 @@ export const StartIcon = () => {
);
};

export const Note = () => {
const dropdownItems = (
<>
<MenuItem note="Перейти в профиль" onClick={() => console.log('v1')}>
Профиль
</MenuItem>
<MenuItem onClick={() => console.log('v3')}>Выход</MenuItem>
</>
);

return (
<DropdownButton
startIcon={<DialogOutlineMd />}
variant="light"
name="With icon"
>
{dropdownItems}
</DropdownButton>
);
};

export const DisableReason = () => {
const dropdownItems = (
<>
<MenuItem
disabledReason="Заблокировано"
disabled
onClick={() => console.log('v1')}
>
Профиль
</MenuItem>
<MenuItem onClick={() => console.log('v3')}>Выход</MenuItem>
</>
);

return (
<DropdownButton
startIcon={<DialogOutlineMd />}
variant="light"
name="With icon"
>
{dropdownItems}
</DropdownButton>
);
};

/** Для стилизации основной кнопки доступны ButtonProps */
export const ButtonProps = () => (
<Grid container rowSpacing={5}>
Expand Down
22 changes: 22 additions & 0 deletions packages/components/src/MenuItem/MenuItem.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ export const Example = () => {
);
};

/**
* prop ```note``` выводит Tooltip на не заблокированном элементе меню
*/
export const Note = () => {
return (
<MenuList>
<MenuItem note="Перейти в профиль">
<ListItemIcon>
<ProfileOutlineMd />
</ListItemIcon>
Мой профиль
</MenuItem>
<MenuItem>
<ListItemIcon>
<ProfileOutlineMd />
</ListItemIcon>
Выход
</MenuItem>
</MenuList>
);
};

export const Selected = () => {
return (
<MenuList>
Expand Down
40 changes: 40 additions & 0 deletions packages/components/src/MenuItem/MenuItem.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest';
import { renderWithTheme, screen, userEvents } from '@astral/tests';

import { MenuItem } from './MenuItem';

describe('MenuItem', () => {
it('Tooltip отображается если передан note и disabled=false', async () => {
const label = 'Мой профиль';

renderWithTheme(<MenuItem note="Перейти в профиль">{label}</MenuItem>);

const item = screen.getByText(label);

await userEvents.hover(item);

const tooltip = await screen.findByRole('tooltip');

expect(tooltip).toBeVisible();
expect(tooltip).toHaveTextContent('Перейти в профиль');
});

it('Tooltip отображается если передан disabledReason и disabled=true', async () => {
const label = 'Мой профиль';

renderWithTheme(
<MenuItem disabled disabledReason="Заблокировано">
{label}
</MenuItem>,
);

const item = screen.getByLabelText('Заблокировано');

await userEvents.hover(item);

const tooltip = await screen.findByRole('tooltip');

expect(tooltip).toBeVisible();
expect(tooltip).toHaveTextContent('Заблокировано');
});
});
13 changes: 11 additions & 2 deletions packages/components/src/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export type MenuItemProps<TComponent extends ElementType = ElementType> =
* Текст тултипа при заблокированном состоянии элемента меню
*/
disabledReason?: string;
/**
* Текст тултипа при наведении на элемент меню
*/
note?: string;
/**
* Позиционирование тултипа
* @default left
*/
tooltipPlacement?: TooltipProps['placement'];
/**
* Тип элемента
Expand All @@ -38,15 +46,16 @@ const InnerMenuItem = <TComponent extends ElementType>(
disabled,
component = 'div',
title,
tooltipPlacement,
tooltipPlacement = 'left',
note,
...rest
} = props;

return (
<li>
<Tooltip
key={title}
title={disabled && disabledReason}
title={disabled ? disabledReason : note}
placement={tooltipPlacement}
withoutContainer={!disabled}
>
Expand Down

0 comments on commit cfa6dd0

Please sign in to comment.