Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: Do not render the image when src prop is undefined.",
"packageName": "@fluentui/react-avatar",
"email": "esteban.230@hotmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,10 @@ describe('Avatar', () => {
expect(root.getAttribute('aria-label')).toBe('First Last');
expect(root.getAttribute('aria-labelledby')).toBeFalsy();
});

it('does not render an image when the src attribute is undefined', () => {
render(<Avatar image={{ src: undefined, alt: 'test-image' }} />);

expect(screen.queryByAltText('test-image')).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const useAvatar_unstable = (props: AvatarProps, ref: React.Ref<HTMLElemen
);

const [imageHidden, setImageHidden] = React.useState<true | undefined>(undefined);
const image: AvatarState['image'] = resolveShorthand(props.image, {
let image: AvatarState['image'] = resolveShorthand(props.image, {
defaultProps: {
alt: '',
role: 'presentation',
Expand All @@ -54,6 +54,11 @@ export const useAvatar_unstable = (props: AvatarProps, ref: React.Ref<HTMLElemen
},
});

// Image shouldn't be rendered if its src is not set
if (!image?.src) {
image = undefined;
}

// Hide the image if it fails to load and restore it on a successful load
if (image) {
image.onError = mergeCallbacks(image.onError, () => setImageHidden(true));
Expand Down