Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion docs/src/pages/components/avatars/GroupAvatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default function GroupAvatars() {
<Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
<Avatar alt="Travis Howard" src="/static/images/avatar/2.jpg" />
<Avatar alt="Cindy Baker" src="/static/images/avatar/3.jpg" />
<Avatar alt="Cindy Baker" src="/static/images/avatar/3.jpg" />
<Avatar alt="Agnes Walker" src="/static/images/avatar/4.jpg" />
<Avatar alt="Trevor Henderson" src="/static/images/avatar/5.jpg" />
</AvatarGroup>
);
}
3 changes: 2 additions & 1 deletion docs/src/pages/components/avatars/GroupAvatars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default function GroupAvatars() {
<Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
<Avatar alt="Travis Howard" src="/static/images/avatar/2.jpg" />
<Avatar alt="Cindy Baker" src="/static/images/avatar/3.jpg" />
<Avatar alt="Cindy Baker" src="/static/images/avatar/3.jpg" />
<Avatar alt="Agnes Walker" src="/static/images/avatar/4.jpg" />
<Avatar alt="Trevor Henderson" src="/static/images/avatar/5.jpg" />
</AvatarGroup>
);
}
17 changes: 14 additions & 3 deletions packages/material-ui-lab/src/AvatarGroup/AvatarGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isFragment } from 'react-is';
import clsx from 'clsx';
import { withStyles } from '@material-ui/core/styles';
import Avatar from '@material-ui/core/Avatar';
import { chainPropTypes } from '@material-ui/utils';

const SPACINGS = {
small: -16,
Expand All @@ -27,10 +28,11 @@ const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) {
children: childrenProp,
classes,
className,
spacing = 'medium',
max = 5,
spacing = 'medium',
...other
} = props;
const clampedMax = max < 2 ? 2 : max;

const children = React.Children.toArray(childrenProp).filter((child) => {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -47,7 +49,7 @@ const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) {
return React.isValidElement(child);
});

const extraAvatars = children.length > max ? children.length - max : 0;
const extraAvatars = children.length > clampedMax ? children.length - clampedMax + 1 : 0;

return (
<div className={clsx(classes.root, className)} ref={ref} {...other}>
Expand Down Expand Up @@ -97,7 +99,16 @@ AvatarGroup.propTypes = {
/**
* Max avatars to show before +x.
*/
max: PropTypes.number,
max: chainPropTypes(PropTypes.number, (props) => {
if (props.max < 2) {
throw new Error(
[
'Material-UI: the prop `max` should be equal to 2 or above.',
'A value below is clamped to 2.',
].join('\n'),
);
}
}),
/**
* Spacing between avatars.
*/
Expand Down
27 changes: 21 additions & 6 deletions packages/material-ui-lab/src/AvatarGroup/AvatarGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,30 @@ describe('<AvatarGroup />', () => {
skip: ['componentProp'],
}));

it('should not display all the avatars', () => {
it('should display all the avatars', () => {
const { container } = render(
<AvatarGroup max={1}>
<Avatar src="broken-url" />
<Avatar src="broken-url" />
<Avatar src="broken-url" />
<AvatarGroup max={3}>
<Avatar src="image-url" />
<Avatar src="image-url" />
<Avatar src="image-url" />
</AvatarGroup>,
);
expect(container.querySelectorAll('img').length).to.equal(1);
expect(container.querySelectorAll('.MuiAvatar-root').length).to.equal(3);
expect(container.querySelectorAll('img').length).to.equal(3);
expect(container.textContent).to.equal('');
});

it('should display 2 avatars and "+2"', () => {
const { container } = render(
<AvatarGroup max={3}>
<Avatar src="image-url" />
<Avatar src="image-url" />
<Avatar src="image-url" />
<Avatar src="image-url" />
</AvatarGroup>,
);
expect(container.querySelectorAll('.MuiAvatar-root').length).to.equal(3);
expect(container.querySelectorAll('img').length).to.equal(2);
expect(container.textContent).to.equal('+2');
});
});