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

[img] Add support for custom alt tag. #165

Merged
merged 1 commit into from
May 11, 2019
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import Avatar, { ConfigProvider } from 'react-avatar';
| `maxInitials` | *number* | | Set max nr of characters used for the initials. If maxInitials=2 and the name is Foo Bar Var the initials will be FB |
| `initials` | *string or function* | [defaultInitials][3] | Set the initials to show or a function that derives them from the component props, the method should have the signature `fn(name, props)` |
| `value` | *string* | | Show a value as avatar |
| `alt` | *string* | `name` or `value` | The `alt` attribute used on the avatar `img` tag. If not set we will fallback to either `name` or `value` |
| `color` | *string* | random | Used in combination with `name` and `value`. Give the background a fixed color with a hex like for example #FF0000 |
| `fgColor` | *string* | #FFF | Used in combination with `name` and `value`. Give the text a fixed color with a hex like for example #FF0000 |
| `size` | *[length][1]* | 50px | Size of the avatar |
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export interface ReactAvatarProps {
* Show a value as avatar
*/
value?: string;
/**
* The alt attribute used on the avatar img tag. If not set we will fallback to either name or value
*/
alt?: string;
/**
* Used in combination with `name` and `value`. Give the background a fixed color with a hex like for example #FF0000
*/
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Avatar extends PureComponent {
// PropTypes defined on sources
...sourcePropTypes,

alt: PropTypes.string,
className: PropTypes.string,
fgColor: PropTypes.string,
color: PropTypes.string,
Expand Down Expand Up @@ -242,10 +243,9 @@ class Avatar extends PureComponent {
}

_renderAsImage() {
const { className, round, unstyled, name, value } = this.props;
const { className, round, unstyled, alt, name, value } = this.props;
const { internal } = this.state;
const size = parseSize(this.props.size);
const alt = name || value;

const imageStyle = unstyled ? null : {
maxWidth: '100%',
Expand All @@ -260,7 +260,7 @@ class Avatar extends PureComponent {
height={size.str}
style={imageStyle}
src={this.state.src}
alt={alt}
alt={alt || name || value}
onError={internal && internal.fetch} />
);
}
Expand Down