Skip to content

Commit

Permalink
Custom ImageComponent for Avatar (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabativi authored and iRoachie committed Jun 15, 2018
1 parent cd73615 commit 3cfc564
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/avatar.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ title: Avatar
* [`titleStyle`](#titlestyle)
* [`placeholderStyle`](#placeholderstyle)
* [`renderPlaceholderContent`](#renderplaceholdercontent)
* [`ImageComponent`](#ImageComponent)

---

Expand Down Expand Up @@ -325,3 +326,13 @@ Custom placeholder element (by default, it's the title)
| Type | Default |
| :------------------------: | :-----: |
| React component or element | none |

---

### `ImageComponent`

Custom ImageComponent for Avatar

| Type | Default |
| :------------------------: | :-----: |
| React component or element | Image |
8 changes: 6 additions & 2 deletions src/avatar/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const Avatar = ({
imageProps,
placeholderStyle,
renderPlaceholderContent,
ImageComponent,
...attributes
}) => {
const width =
Expand Down Expand Up @@ -119,6 +120,7 @@ const Avatar = ({
imageProps && imageProps.style,
avatarStyle,
]}
ImageComponent={ImageComponent}
/>
{Utils}
</Component>
Expand Down Expand Up @@ -209,6 +211,7 @@ Avatar.propTypes = {
placeholderStyle: ViewPropTypes.style,
renderPlaceholderContent: nodeType,
imageProps: PropTypes.object,
ImageComponent: PropTypes.element,
};

Avatar.defaultProps = {
Expand All @@ -223,6 +226,7 @@ Avatar.defaultProps = {
underlayColor: DEFAULT_COLORS[0],
style: null,
},
ImageComponent: Image,
};

class FadeInImage extends React.PureComponent {
Expand All @@ -242,10 +246,10 @@ class FadeInImage extends React.PureComponent {
}

render() {
const { placeholderStyle, PlaceholderContent, containerStyle, style, ...attributes } = this.props
const { placeholderStyle, PlaceholderContent, containerStyle, style, ImageComponent, ...attributes } = this.props
return Platform.OS === 'ios' ? (
<View style={[styles.overlayContainer, containerStyle]}>
<Image {...attributes} onLoadEnd={this.onLoadEnd} style={[styles.avatar, style]} />
<ImageComponent {...attributes} onLoadEnd={this.onLoadEnd} style={[styles.avatar, style]} />
<Animated.View style={[styles.placeholderContainer, { opacity: this.placeholderContainerOpacity }]}>
<View style={[style, styles.placeholder, placeholderStyle]}>
{PlaceholderContent}
Expand Down
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ export interface AvatarProps {
*/

size?: 'small' | 'medium' | 'large' | 'xlarge' | number;

/**
* Image Component of Avatar
* @default React Native default Image component
*/

ImageComponent?: React.ComponentClass;
}

/**
Expand Down

0 comments on commit 3cfc564

Please sign in to comment.