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

Add support for image path value in <SimpleList leftAvatar> #6418

Merged
merged 2 commits into from
Jul 7, 2021

Conversation

fzaninotto
Copy link
Member

Problem

By default, SimpleList expect avatar values to be initials. When passing a avatar URL, the avatar shows the end of the URL instead of the image.

const UserList = (props) => (
    <List {...props}>
        <SimpleList
            primaryText={record => record.name}
            secondaryText={record => record.role}
            leftAvatar={record => record.avatar}
        />
    </List>
);

image

To set an image as avatar in SimpleList, developers must return an <img> element. This isn't super intuitive.

const UserList = (props) => (
    <List {...props}>
        <SimpleList
            primaryText={record => record.name}
            secondaryText={record => record.role}
-           leftAvatar={record => record.avatar}
+           leftAvatar={record => <img alt="" src={record.avatar} />}
        />
    </List>
);

Note: user records look like:

    [
        {
            id: 1,
            name: 'Logan Schowalter',
            role: 'admin',
            avatar: 'https://marmelab.com/posters/avatar-1.jpeg',
        },
        {
            id: 2,
            name: 'Breanna Gibson',
            role: 'user',
            avatar: 'https://marmelab.com/posters/avatar-2.jpeg',
        },
        {
            id: 3,
            name: 'Annamarie Mayer',
            role: 'user',
            avatar: 'https://marmelab.com/posters/avatar-3.jpeg',
        },
    ],

Solution

Avatar accepts both text (displays initials) or image paths (displays image)

image

@fzaninotto fzaninotto added the RFR Ready For Review label Jul 6, 2021
avatarCallback: FunctionToElement<RecordType>
) => {
const avatarValue = avatarCallback(data[id], id);
if (typeof avatarValue === 'string' && avatarValue.startsWith('http')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about base64 images ?

@djhi djhi added this to the 3.17.0 milestone Jul 7, 2021
@djhi djhi merged commit cc53f89 into next Jul 7, 2021
@djhi djhi deleted the simplelist-avatar-image branch July 7, 2021 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants