Skip to content

Commit

Permalink
fix(AvatarStack): remaining avatars calculation (#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov authored Oct 4, 2024
1 parent a6dfd63 commit 76943c9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
26 changes: 15 additions & 11 deletions src/components/AvatarStack/AvatarStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {block} from '../utils/cn';
import {AvatarStackItem} from './AvatarStackItem';
import {AvatarStackMore} from './AvatarStackMore';
import {AvatarStackMoreButton} from './AvatarStackMoreButton';
import {AVATAR_STACK_DEFAULT_MAX} from './constants';
import type {AvatarStackProps} from './types';

import './AvatarStack.scss';

const b = block('avatar-stack');

const AvatarStackComponent = ({
max = 3,
max = AVATAR_STACK_DEFAULT_MAX,
total,
overlapSize = 's',
size,
Expand All @@ -21,8 +22,17 @@ const AvatarStackComponent = ({
renderMore,
}: AvatarStackProps) => {
const visibleItems: React.ReactElement[] = [];
let moreItems = 0;
const normalizedMax = max < 1 ? 1 : max;

/** 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;

/** Remaining avatars */
const moreItems = normalizedTotal - normalizedMax;

React.Children.forEach(children, (child) => {
if (!React.isValidElement(child)) {
Expand All @@ -31,18 +41,12 @@ const AvatarStackComponent = ({

const item = <AvatarStackItem key={visibleItems.length}>{child}</AvatarStackItem>;

if (visibleItems.length <= normalizedMax) {
if (visibleItems.length < normalizedMax) {
visibleItems.unshift(item);
} else {
moreItems += 1;
}
});

moreItems = Math.max(moreItems, total ? total - normalizedMax : 0);

const hasMoreButton = moreItems > 0;
/** Avatars + more button, or just avatars, when avatars count is equal to `max` or less */
const normalOverflow = moreItems >= 1;

return (
// Safari remove role=list with some styles, applied to li items, so we need
Expand All @@ -58,7 +62,7 @@ const AvatarStackComponent = ({
)}
</AvatarStackItem>
) : null}
{normalOverflow ? visibleItems.slice(0, normalizedMax) : visibleItems}
{visibleItems}
</ul>
);
};
Expand Down
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions src/components/AvatarStack/__stories__/AvatarStack.stories.tsx

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/components/AvatarStack/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const AVATAR_STACK_DEFAULT_MAX = 3;

0 comments on commit 76943c9

Please sign in to comment.