diff --git a/docs/pages/api-docs/avatar-group.md b/docs/pages/api-docs/avatar-group.md index 393a0e92dbd03a..bdd0d2e5ccb838 100644 --- a/docs/pages/api-docs/avatar-group.md +++ b/docs/pages/api-docs/avatar-group.md @@ -26,6 +26,7 @@ You can learn more about the difference by [reading this guide](/guides/minimizi |:-----|:-----|:--------|:------------| | children | node | | The avatars to stack. | | classes | object | | Override or extend the styles applied to the component. See [CSS API](#css) below for more details. | +| max | number | 5 | Max avatars to show before +x. | | spacing | 'medium'
| 'small'
| number
| 'medium' | Spacing between avatars. | The `ref` is forwarded to the root element. diff --git a/docs/src/pages/components/avatars/GroupAvatars.js b/docs/src/pages/components/avatars/GroupAvatars.js index 1af0c027edd5d6..17c8816ff1c540 100644 --- a/docs/src/pages/components/avatars/GroupAvatars.js +++ b/docs/src/pages/components/avatars/GroupAvatars.js @@ -1,17 +1,14 @@ import React from 'react'; import Avatar from '@material-ui/core/Avatar'; import AvatarGroup from '@material-ui/lab/AvatarGroup'; -import Tooltip from '@material-ui/core/Tooltip'; export default function GroupAvatars() { return ( - + - - +3 - + ); } diff --git a/docs/src/pages/components/avatars/GroupAvatars.tsx b/docs/src/pages/components/avatars/GroupAvatars.tsx index 1af0c027edd5d6..17c8816ff1c540 100644 --- a/docs/src/pages/components/avatars/GroupAvatars.tsx +++ b/docs/src/pages/components/avatars/GroupAvatars.tsx @@ -1,17 +1,14 @@ import React from 'react'; import Avatar from '@material-ui/core/Avatar'; import AvatarGroup from '@material-ui/lab/AvatarGroup'; -import Tooltip from '@material-ui/core/Tooltip'; export default function GroupAvatars() { return ( - + - - +3 - + ); } diff --git a/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.d.ts b/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.d.ts index 1a0403bf6a7fa5..62d7792bc53d98 100644 --- a/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.d.ts +++ b/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.d.ts @@ -7,6 +7,10 @@ export interface AvatarGroupProps * The avatars to stack. */ children: React.ReactNode; + /** + * Max avatars to show before +x. + */ + max?: number; /** * Spacing between avatars. */ diff --git a/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.js b/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.js index a78d20a321378d..273066efc309b9 100644 --- a/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.js +++ b/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.js @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'; import { isFragment } from 'react-is'; import clsx from 'clsx'; import { withStyles } from '@material-ui/core/styles'; +import Avatar from '@material-ui/core/Avatar'; const SPACINGS = { small: -16, @@ -22,7 +23,14 @@ export const styles = theme => ({ }); const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) { - const { children: childrenProp, classes, className, spacing = 'medium', ...other } = props; + const { + children: childrenProp, + classes, + className, + spacing = 'medium', + max = 5, + ...other + } = props; const children = React.Children.toArray(childrenProp).filter(child => { if (process.env.NODE_ENV !== 'production') { @@ -39,9 +47,11 @@ const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) { return React.isValidElement(child); }); + const extraAvatars = children.length > max ? children.length - max : 0; + return (
- {children.map((child, index) => { + {children.slice(0, children.length - extraAvatars).map((child, index) => { return React.cloneElement(child, { className: clsx(child.props.className, classes.avatar), style: { @@ -51,6 +61,17 @@ const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) { }, }); })} + {extraAvatars ? ( + + +{extraAvatars} + + ) : null}
); }); @@ -73,6 +94,10 @@ AvatarGroup.propTypes = { * @ignore */ className: PropTypes.string, + /** + * Max avatars to show before +x. + */ + max: PropTypes.number, /** * Spacing between avatars. */ diff --git a/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.test.js b/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.test.js index 422a8ebc3ddb9f..f1fbf364c49361 100644 --- a/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.test.js +++ b/packages/material-ui-lab/src/AvatarGroup/AvatarGroup.test.js @@ -1,11 +1,15 @@ import * as React from 'react'; +import { expect } from 'chai'; import { createMount, getClasses } from '@material-ui/core/test-utils'; import describeConformance from '@material-ui/core/test-utils/describeConformance'; +import { createClientRender } from 'test/utils/createClientRender'; import AvatarGroup from './AvatarGroup'; +import { Avatar } from '@material-ui/core'; describe('', () => { let mount; let classes; + const render = createClientRender(); before(() => { mount = createMount({ strict: true }); @@ -23,4 +27,16 @@ describe('', () => { refInstanceof: window.HTMLDivElement, skip: ['componentProp'], })); + + it('should not display all the avatars', () => { + const { container } = render( + + + + + , + ); + expect(container.querySelectorAll('img').length).to.equal(1); + expect(container.textContent).to.equal('+2'); + }); });