Skip to content

Commit b93c3f2

Browse files
favor prop-type warnings
1 parent 523213d commit b93c3f2

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

docs/pages/api-docs/avatar-group.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The `MuiAvatarGroup` name can be used for providing [default props](/customizati
3131
| <span class="prop-name">children</span> | <span class="prop-type">node</span> | | The avatars to stack. |
3232
| <span class="prop-name">classes</span> | <span class="prop-type">object</span> | | Override or extend the styles applied to the component. See [CSS API](#css) below for more details. |
3333
| <span class="prop-name">max</span> | <span class="prop-type">number</span> | <span class="prop-default">5</span> | Max avatars to show before +x. |
34-
| <span class="prop-name">spacing</span> | <span class="prop-type">'medium'<br>&#124;&nbsp;'small'<br>&#124;&nbsp;number</span> | | Spacing between avatars. |
34+
| <span class="prop-name">spacing</span> | <span class="prop-type">'medium'<br>&#124;&nbsp;'small'<br>&#124;&nbsp;number</span> | <span class="prop-default">'medium'</span> | Spacing between avatars. |
3535

3636
The `ref` is forwarded to the root element.
3737

packages/material-ui-lab/src/AvatarGroup/AvatarGroup.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isFragment } from 'react-is';
44
import clsx from 'clsx';
55
import { withStyles } from '@material-ui/core/styles';
66
import Avatar from '@material-ui/core/Avatar';
7+
import { chainPropTypes } from '@material-ui/utils';
78

89
const SPACINGS = {
910
small: -16,
@@ -23,13 +24,15 @@ export const styles = (theme) => ({
2324
});
2425

2526
const AvatarGroup = React.forwardRef(function AvatarGroup(props, ref) {
26-
const { children: childrenProp, classes, className, spacing = 'medium', ...other } = props;
27-
let { max = 5 } = props;
28-
29-
if (max < 2) {
30-
max = 2;
31-
console.warn('Material-UI: AvatarGroup `max` should be at least 2.');
32-
}
27+
const {
28+
children: childrenProp,
29+
classes,
30+
className,
31+
max = 5,
32+
spacing = 'medium',
33+
...other
34+
} = props;
35+
const clampedMax = max < 2 ? 2 : max;
3336

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

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

5154
return (
5255
<div className={clsx(classes.root, className)} ref={ref} {...other}>
@@ -96,7 +99,16 @@ AvatarGroup.propTypes = {
9699
/**
97100
* Max avatars to show before +x.
98101
*/
99-
max: PropTypes.number,
102+
max: chainPropTypes(PropTypes.number, (props) => {
103+
if (props.max < 2) {
104+
throw new Error(
105+
[
106+
'Material-UI: the prop `max` should be equal to 2 or above.',
107+
'A value below is clamped to 2.',
108+
].join('\n'),
109+
);
110+
}
111+
}),
100112
/**
101113
* Spacing between avatars.
102114
*/
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react';
22
import { expect } from 'chai';
3-
import { stub } from 'sinon';
43
import { createMount, getClasses } from '@material-ui/core/test-utils';
54
import describeConformance from '@material-ui/core/test-utils/describeConformance';
65
import { createClientRender } from 'test/utils/createClientRender';
@@ -29,47 +28,30 @@ describe('<AvatarGroup />', () => {
2928
skip: ['componentProp'],
3029
}));
3130

32-
it('should display 1 avatar and "+X" if `max` is lower than 2', () => {
33-
const consoleWarn = stub(console, 'warn');
34-
const { container } = render(
35-
<AvatarGroup max={1}>
36-
<Avatar src="image-url" />
37-
<Avatar src="image-url" />
38-
<Avatar src="image-url" />
39-
<Avatar src="image-url" />
40-
</AvatarGroup>,
41-
);
42-
expect(consoleWarn.callCount).to.equal(1);
43-
expect(container.querySelectorAll('.MuiAvatar-root').length).to.equal(2);
44-
expect(container.querySelectorAll('img').length).to.equal(1);
45-
expect(container.textContent).to.equal('+3');
46-
});
47-
48-
it('should display first `max` avatars, followed by "+X"', () => {
31+
it('should display all the avatars', () => {
4932
const { container } = render(
5033
<AvatarGroup max={3}>
5134
<Avatar src="image-url" />
5235
<Avatar src="image-url" />
5336
<Avatar src="image-url" />
54-
<Avatar src="image-url" />
55-
<Avatar src="image-url" />
5637
</AvatarGroup>,
5738
);
5839
expect(container.querySelectorAll('.MuiAvatar-root').length).to.equal(3);
59-
expect(container.querySelectorAll('img').length).to.equal(2);
60-
expect(container.textContent).to.equal('+3');
40+
expect(container.querySelectorAll('img').length).to.equal(3);
41+
expect(container.textContent).to.equal('');
6142
});
6243

63-
it('should display all avatars instead of `max` avatars followed by "+1"', () => {
44+
it('should display 2 avatars and "+2"', () => {
6445
const { container } = render(
6546
<AvatarGroup max={3}>
6647
<Avatar src="image-url" />
6748
<Avatar src="image-url" />
6849
<Avatar src="image-url" />
50+
<Avatar src="image-url" />
6951
</AvatarGroup>,
7052
);
7153
expect(container.querySelectorAll('.MuiAvatar-root').length).to.equal(3);
72-
expect(container.querySelectorAll('img').length).to.equal(3);
73-
expect(container.textContent).to.have.lengthOf(0);
54+
expect(container.querySelectorAll('img').length).to.equal(2);
55+
expect(container.textContent).to.equal('+2');
7456
});
7557
});

0 commit comments

Comments
 (0)