From f01eef8991622119e547c7869d7dabc1f5bc2acf Mon Sep 17 00:00:00 2001 From: Alexey Sudilovskiy Date: Wed, 16 Oct 2024 18:12:56 +0200 Subject: [PATCH] feat(AvatarStack): forward ref to root element of component --- src/components/AvatarStack/AvatarStack.tsx | 97 ++++++++++++---------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/src/components/AvatarStack/AvatarStack.tsx b/src/components/AvatarStack/AvatarStack.tsx index f7af7de69..378fad743 100644 --- a/src/components/AvatarStack/AvatarStack.tsx +++ b/src/components/AvatarStack/AvatarStack.tsx @@ -12,60 +12,65 @@ import './AvatarStack.scss'; const b = block('avatar-stack'); -const AvatarStackComponent = ({ - max = AVATAR_STACK_DEFAULT_MAX, - total, - overlapSize = 's', - size, - children, - className, - renderMore, -}: AvatarStackProps) => { - const visibleItems: React.ReactElement[] = []; +const AvatarStackComponent = React.forwardRef( + ( + { + max = AVATAR_STACK_DEFAULT_MAX, + total, + overlapSize = 's', + size, + children, + className, + renderMore, + }, + ref, + ) => { + const visibleItems: React.ReactElement[] = []; - /** All avatars amount */ - const normalizedTotal = total ? Math.max(total, max) : React.Children.count(children); + /** All avatars amount */ + const normalizedTotal = total ? Math.max(total, max) : React.Children.count(children); - /** Amount avatars to be visible (doesn't include badge with remaining avatars) */ - let normalizedMax = max < 1 ? 1 : max; - // Skip rendering badge with +1, just show avatar instead - normalizedMax = normalizedTotal - normalizedMax > 1 ? normalizedMax : normalizedTotal; + /** Amount avatars to be visible (doesn't include badge with remaining avatars) */ + let normalizedMax = max < 1 ? 1 : max; + // Skip rendering badge with +1, just show avatar instead + normalizedMax = normalizedTotal - normalizedMax > 1 ? normalizedMax : normalizedTotal; - /** Remaining avatars */ - const moreItems = normalizedTotal - normalizedMax; + /** Remaining avatars */ + const moreItems = normalizedTotal - normalizedMax; - React.Children.forEach(children, (child) => { - if (!React.isValidElement(child)) { - return; - } + React.Children.forEach(children, (child) => { + if (!React.isValidElement(child)) { + return; + } - const item = {child}; + const item = {child}; - if (visibleItems.length < normalizedMax) { - visibleItems.unshift(item); - } - }); + if (visibleItems.length < normalizedMax) { + visibleItems.unshift(item); + } + }); - const hasMoreButton = moreItems > 0; + const hasMoreButton = moreItems > 0; - return ( - // Safari remove role=list with some styles, applied to li items, so we need - // to restore role manually - // eslint-disable-next-line jsx-a11y/no-redundant-roles - - ); -}; + return ( + // Safari remove role=list with some styles, applied to li items, so we need + // to restore role manually + // eslint-disable-next-line jsx-a11y/no-redundant-roles + + ); + }, +); AvatarStackComponent.displayName = 'AvatarStack';