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

plasma-web: Pass ref to DropdownItem #769

Merged
merged 2 commits into from
Sep 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion packages/plasma-hope/api/plasma-hope.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ export { DisabledProps }
export const Dropdown: FC<DropdownProps>;

// @public
export const DropdownItem: FC<DropdownItemProps>;
export const DropdownItem: React_2.ForwardRefExoticComponent<DropdownItemProps & React_2.RefAttributes<HTMLLIElement>>;

// @public (undocumented)
export interface DropdownItemProps extends DropdownNodeType, Omit<DetailedHTMLProps<HTMLAttributes<HTMLLIElement>, HTMLLIElement>, 'onClick' | 'ref'> {
Expand Down
46 changes: 26 additions & 20 deletions packages/plasma-hope/src/components/Dropdown/DropdownItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef, useCallback, useMemo } from 'react';
import type { DetailedHTMLProps, HTMLAttributes, ReactNode, FC, SyntheticEvent } from 'react';
import React, { useRef, useCallback, useMemo, forwardRef } from 'react';
import type { DetailedHTMLProps, HTMLAttributes, ReactNode, SyntheticEvent } from 'react';
import styled, { css } from 'styled-components';
import {
body1,
Expand All @@ -9,6 +9,7 @@ import {
surfaceLiquid03,
applyDisabled,
applyEllipsis,
useForkRef,
} from '@salutejs/plasma-core';
import { IconChevronRight, IconDone } from '@salutejs/plasma-icons';

Expand Down Expand Up @@ -130,25 +131,30 @@ const StyledDot = styled.div`
/**
* Элемент выпадающего списка.
*/
export const DropdownItem: FC<DropdownItemProps> = ({
value,
label,
isActive,
isDisabled,
isHovered,
color,
contentLeft,
items = [],
role = 'menuitem',
index,
onClick: onClickExternal,
onHover,
onFocus,
...rest
}) => {
export const DropdownItem = forwardRef<HTMLLIElement, DropdownItemProps>((props, outerRef) => {
const {
value,
label,
isActive,
isDisabled,
isHovered,
color,
contentLeft,
items = [],
role = 'menuitem',
index,
onClick: onClickExternal,
onHover,
onFocus,
...rest
} = props;

const ref = useRef<HTMLLIElement>(null);
const forkRef = useForkRef(outerRef, ref);
const hasItems = Boolean(items.length);

const isActiveAsSingleOrNode = Boolean(isActive || items.filter((item) => item.isActive)?.length);

const contentRight = useMemo(() => {
if (hasItems) {
return (
Expand Down Expand Up @@ -187,7 +193,7 @@ export const DropdownItem: FC<DropdownItemProps> = ({
return (
<StyledDropdownItem
{...rest}
ref={ref}
ref={forkRef}
$withAssistive={Boolean(onHover)}
$hover={isHovered}
$disabled={isDisabled}
Expand All @@ -205,4 +211,4 @@ export const DropdownItem: FC<DropdownItemProps> = ({
{contentRight}
</StyledDropdownItem>
);
};
});
45 changes: 43 additions & 2 deletions website/plasma-web-docs/docs/components/Dropdown.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ export function App() {
}
```

## Позицирование
## Позиционирование
Yakutoc marked this conversation as resolved.
Show resolved Hide resolved
Помимо стандартных способов расположения, можно также указать значение `auto`, которое будет автоматически определять нужное расположение.

Если необходимо автоматически использовать лишь определенные раположения, то нужно передать массив расположений. Например:
Если необходимо автоматически использовать лишь определенные расположения, то нужно передать массив расположений. Например:

```tsx live
import React from 'react';
Expand Down Expand Up @@ -341,3 +341,44 @@ export function App() {
:::caution
Для обеспечения корректной работы навигации с помощью клавиатуры необходимо указать свойство `id`.
:::


## Ref для DropdownItem

Достаточно указать свойство `ref` на компоненте.

```tsx live
import React, { useState, useRef } from 'react';
import { DropdownPopup, DropdownList, DropdownItem, Button } from '@salutejs/plasma-web';
import { IconHeart } from '@salutejs/plasma-icons';

export function App() {
const [isOpen, setIsOpen] = useState(false);
const dropDownItemRef = useRef<HTMLLIElement | null>(null);

return (
<div style={{ height:"350px", display: 'block' }}>
<DropdownPopup
isOpen={isOpen}
trigger="click"
placement="bottom"
onToggle={(is) => setIsOpen(is)}
disclosure={<Button text="Меню" />}
>
<DropdownList>
<DropdownItem value={100001} label="Новости" />
<DropdownItem value={100002} label="Каталог" color="var(--plasma-colors-accent)" />
<DropdownItem
value={100003}
label="О нас"
color="var(--plasma-colors-critical)"
contentLeft={<IconHeart color="inherit" />}
ref={dropDownItemRef}
/>
<DropdownItem value={100004} label="Недоступно" isDisabled />
</DropdownList>
</DropdownPopup>
</div>
);
}
```