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

[AvatarGroup] Allow specifying total number of avatars #29898

Merged
merged 24 commits into from
Dec 3, 2021

Conversation

eduardomcv
Copy link
Contributor

Closes #29649

Allow the developer to specify a total number of avatars. If a total is provided, all AvatarGroup calculations will be based on this number, rather than relying solely on children.length.

This is useful in use cases where the avatars are based on paginated lists and the server exposes the total count. There shouldn't be a need to request the whole list from the server (or to generate a list with that length) just to fulfil the requirement of the AvatarGroup.

@mui-pr-bot
Copy link

mui-pr-bot commented Nov 25, 2021

Details of bundle changes

Generated by 🚫 dangerJS against 82295ef

@mbrookes mbrookes changed the title [AvatarGroup] Allow specifying total avatars amount [AvatarGroup] Allow specifying total number of avatars Nov 25, 2021
@mbrookes mbrookes added component: avatar This is the name of the generic UI component, not the React module! new feature New feature or request labels Nov 25, 2021
@oliviertassinari oliviertassinari changed the title [AvatarGroup] Allow specifying total number of avatars [Avatar] Allow specifying total number of avatars in AvatarGroup Nov 28, 2021
@oliviertassinari oliviertassinari changed the title [Avatar] Allow specifying total number of avatars in AvatarGroup [AvatarGroup] Allow specifying total number of avatars Nov 28, 2021
@siriwatknp siriwatknp added the on hold There is a blocker, we need to wait label Nov 29, 2021
@siriwatknp
Copy link
Member

@eduardomcv Can you check my comment before we make a decision about this new prop? #29649 (comment)

Comment on lines 102 to 108
const totalAvatars = total === undefined || total < 0 ? children.length : total;
const clampedChildren = totalAvatars < children.length ? totalAvatars : children.length;

const shownAvatars =
totalAvatars <= clampedMax || clampedChildren < clampedMax ? clampedChildren : clampedMax - 1;

const extraAvatars = totalAvatars - shownAvatars;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the clampedChildren? it makes the code very hard to understand and we need to document it.

Suggested change
const totalAvatars = total === undefined || total < 0 ? children.length : total;
const clampedChildren = totalAvatars < children.length ? totalAvatars : children.length;
const shownAvatars =
totalAvatars <= clampedMax || clampedChildren < clampedMax ? clampedChildren : clampedMax - 1;
const extraAvatars = totalAvatars - shownAvatars;
const totalAvatars = total || children.length;
const extraAvatars = totalAvatars > clampedMax ? totalAvatars - clampedMax + 1 : 0;

Copy link
Contributor Author

@eduardomcv eduardomcv Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siriwatknp I removed clampedChildren and refactored the code into something closer to your suggestion.

A problem arises when there are less child avatars than the clampedMax. In summary, we need to take into account children.length when calculating extraAvatars. Using the example from the 'should respect total' test to illustrate the problem:

<AvatarGroup total={10}>
  <Avatar src="/fake.png" />
  <Avatar src="/fake.png" />
  <Avatar src="/fake.png" />
</AvatarGroup>

In this example, there will be 10 totalAvatars, and since max was not specified, clampedMax will be 5. Applying the suggested calculation above, extraAvatars will equal 6 (10 - 5 + 1). However, this is incorrect, because given the fact that there are only 3 child avatars available, the component will render 3 avatars and a "+6".

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic should be simple as I suggested. cc @mnajdova

@siriwatknp
Copy link
Member

siriwatknp commented Dec 2, 2021

@eduardomcv Sorry that I change your code because I added more tests and they failed, so I have to adjust the implementation to make it works. Can you review it again? (if you have a better way, feel free to change because I think we have a good amount of tests that should cover most cases)

Also added one demo for the total prop.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, I left some small details.

docs/src/pages/components/avatars/avatars.md Outdated Show resolved Hide resolved
packages/mui-material/src/AvatarGroup/AvatarGroup.d.ts Outdated Show resolved Hide resolved
Comment on lines +77 to +87
it('should respect total and clamp down shown avatars', () => {
const { container } = render(
<AvatarGroup total={1}>
<Avatar src="/fake.png" />
<Avatar src="/fake.png" />
</AvatarGroup>,
);
expect(container.querySelectorAll('.MuiAvatar-root').length).to.equal(1);
expect(container.querySelectorAll('img').length).to.equal(1);
expect(container.textContent).to.equal('');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this test. There are two avatars, but the total number is one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It tests that the total prop should be used instead of children.length

siriwatknp and others added 3 commits December 2, 2021 17:20
Copy link
Contributor Author

@eduardomcv eduardomcv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siriwatknp LGTM!

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for submitting the PR! Awesome.

@siriwatknp siriwatknp merged commit e6433d5 into mui:master Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: avatar This is the name of the generic UI component, not the React module! new feature New feature or request on hold There is a blocker, we need to wait
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Avatar] Allow the customization of how the "+x" is calculated
5 participants