Skip to content

Commit

Permalink
[Avatar] Revert #17694, correct the API docs, add tests (#18026)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrookes authored and oliviertassinari committed Oct 27, 2019
1 parent 589657f commit 0d5af7d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/pages/api/avatar.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You can learn more about the difference by [reading this guide](/guides/minimizi
| Name | Type | Default | Description |
|:-----|:-----|:--------|:------------|
| <span class="prop-name">alt</span> | <span class="prop-type">string</span> | | Used in combination with `src` or `srcSet` to provide an alt attribute for the rendered `img` element. |
| <span class="prop-name">children</span> | <span class="prop-type">node</span> | | Used to render icon or text elements inside the Avatar. `src` and `alt` props will not be used and no `img` will be rendered by default.<br>This can be an element, or just a string. |
| <span class="prop-name">children</span> | <span class="prop-type">node</span> | | Used to render icon or text elements inside the Avatar if `src` is not set. This can be an element, or just a string. |
| <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. |
| <span class="prop-name">component</span> | <span class="prop-type">elementType</span> | <span class="prop-default">'div'</span> | The component used for the root node. Either a string to use a DOM element or a component. |
| <span class="prop-name">imgProps</span> | <span class="prop-type">object</span> | | Attributes applied to the `img` element if the component is used to display an image. |
Expand Down
28 changes: 12 additions & 16 deletions packages/material-ui/src/Avatar/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,22 @@ const Avatar = React.forwardRef(function Avatar(props, ref) {
...other
} = props;

let children = childrenProp;
let children = null;
const img = src || srcSet;

if (img) {
children = (
<React.Fragment>
<img
alt={alt}
src={src}
srcSet={srcSet}
sizes={sizes}
className={classes.img}
{...imgProps}
/>
{children}
</React.Fragment>
<img
alt={alt}
src={src}
srcSet={srcSet}
sizes={sizes}
className={classes.img}
{...imgProps}
/>
);
} else {
children = childrenProp;
}

return (
Expand Down Expand Up @@ -94,10 +93,7 @@ Avatar.propTypes = {
*/
alt: PropTypes.string,
/**
* Used to render icon or text elements inside the Avatar.
* `src` and `alt` props will not be used and no `img` will
* be rendered by default.
*
* Used to render icon or text elements inside the Avatar if `src` is not set.
* This can be an element, or just a string.
*/
children: PropTypes.node,
Expand Down
14 changes: 14 additions & 0 deletions packages/material-ui/src/Avatar/Avatar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ describe('<Avatar />', () => {
});
});

describe('image avatar with unrendered children', () => {
it('should render a div containing an img, not children', () => {
const wrapper = mount(<Avatar src="something.jpg">MB</Avatar>);
assert.strictEqual(wrapper.find('img').length, 1);
assert.strictEqual(wrapper.text(), '');
});

it('should be able to add more props to the image', () => {
const onError = () => {};
const wrapper = mount(<Avatar src="something.jpg" imgProps={{ onError }} />);
assert.strictEqual(wrapper.find('img').props().onError, onError);
});
});

describe('font icon avatar', () => {
let wrapper;

Expand Down

0 comments on commit 0d5af7d

Please sign in to comment.