Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Avatar] Improve fallback when children is empty string or boolean #40766

Merged
merged 6 commits into from Jan 28, 2024
Merged

[material-ui][Avatar] Improve fallback when children is empty string or boolean #40766

merged 6 commits into from Jan 28, 2024

Conversation

mirus-ua
Copy link
Contributor

@mirus-ua mirus-ua commented Jan 23, 2024

I faced an unexpected behavior when our BE service didn't return a valid image URL, and my Avatar component showed me an empty circle with a background. After a quick investigation, I found that we may pass both a URL and children as a fallback in some cases in the project. If both of them are falsy, from the UX perspective, it leads to unexpected results, at least according to the current documentation.

You can check it in the current version of the library with the following code snippet.

function App() {
  const firstName = undefined;
  const lastName = undefined;
  const children = "";
  const src = 'invalid'

  const userInitials = getUserInitials(firstName, lastName)

  const alt = typeof children === 'string' ? children : userInitials;

  return (
      <Avatar src={src} alt={alt}>{children}</Avatar>
  )
}

I don't see any reason why this slight quality-of-life improvement could be wrong, but I gladly accept any feedback.

@mui-bot
Copy link

mui-bot commented Jan 23, 2024

Netlify deploy preview

https://deploy-preview-40766--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against f61a104

@danilo-leal danilo-leal added component: avatar This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material labels Jan 23, 2024
@mirus-ua
Copy link
Contributor Author

@oliviertassinari hello

I found an example of how you handle expected errors here: packages/mui-material/src/Avatar/Avatar.test.js
So right now, I covered an empty string and a boolean value case + added tests for them.
Do you have any new thoughts about the PR?

BTW ci/circleci: test_unit-1 failed with an error in a package where I didn't do any changes -- @mui/envinfo

Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove the test or extend it so that we expect a console error? If the last is correct, could you reference how similar cases were handled in the codebase?

We should remove the test. It's not needed. The PropType warning is likely because true is not a valid React node for the children prop, unlike strings or null. React considers false as a valid React node, rendering nothing.

@ZeeshanTamboli ZeeshanTamboli added the bug 🐛 Something doesn't work label Jan 27, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Avatar] Improve fallback when children is empty string [material-ui][Avatar] Improve fallback when children is empty string Jan 27, 2024
@mirus-ua
Copy link
Contributor Author

Should I remove the test or extend it so that we expect a console error? If the last is correct, could you reference how similar cases were handled in the codebase?

We should remove the test. It's not needed. The PropType warning is likely because true is not a valid React node for the children prop, unlike strings or null. React considers false as a valid React node, rendering nothing.

Done

@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui][Avatar] Improve fallback when children is empty string [material-ui][Avatar] Improve fallback when children is empty string or boolean Jan 28, 2024
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@ZeeshanTamboli ZeeshanTamboli merged commit 0ebf32f into mui:master Jan 28, 2024
19 checks passed
} else if (childrenProp != null) {
} else if (childrenProp != null && childrenProp !== '' && typeof childrenProp !== 'boolean') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can shorten the condition without making any test fail: #40834.

mostafa-rio pushed a commit to mostafa-rio/material-ui that referenced this pull request Feb 3, 2024
…g or boolean (mui#40766)

Co-authored-by: ZeeshanTamboli <zeeshan.tamboli@gmail.com>
@DiegoAndai
Copy link
Member

Hey! We've been discussing this change at #41291. You might want to check that out and join the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: avatar This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants