Skip to content

Commit

Permalink
fix withTheme typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Hamilton committed Mar 3, 2021
1 parent b239adc commit 107ecde
Show file tree
Hide file tree
Showing 33 changed files with 336 additions and 351 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"react-native-vector-icons": "^7.0.0",
"react-test-renderer": "^16.13.1",
"rimraf": "^3.0.2",
"typescript": "^4.1.3"
"typescript": "^4.1.3",
"utility-types": "^3.10.0"
},
"peerDependencies": {
"react-native-safe-area-context": "^3.1.9",
Expand Down
1 change: 1 addition & 0 deletions src/avatar/Accessory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const Accessory: React.FunctionComponent<AccessoryProps> = ({
>
<View>
{source ? (
//@ts-ignore
<Image
style={{
width: size,
Expand Down
196 changes: 97 additions & 99 deletions src/avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,107 +50,104 @@ export type AvatarProps = {
ImageComponent?: React.ComponentClass;
};

interface Avatar extends React.FunctionComponent<AvatarProps> {
Accessory: typeof Accessory;
}
interface Avatar extends React.FunctionComponent<AvatarProps> {}

const AvatarComponent: Avatar = Object.assign(
({
onPress,
onLongPress,
Component = onPress || onLongPress ? TouchableOpacity : View,
containerStyle,
icon,
iconStyle,
source,
size = 'small',
avatarStyle,
rounded,
title,
titleStyle,
overlayContainerStyle,
imageProps,
placeholderStyle,
renderPlaceholderContent,
ImageComponent = RNImage,
children,
...attributes
}: React.PropsWithChildren<AvatarProps>) => {
const width =
typeof size === 'number' ? size : avatarSizes[size] || avatarSizes.small;
const height = width;
const titleSize = width / 2;
const iconSize = width / 2;
const PlaceholderContent =
(renderPlaceholderContent &&
renderNode(undefined, renderPlaceholderContent)) ||
(title && (
<Text
style={StyleSheet.flatten([
styles.title,
{ fontSize: titleSize },
titleStyle,
])}
>
{title}
</Text>
)) ||
(icon && (
<Icon
style={iconStyle && iconStyle}
color={icon.color || 'white'}
name={icon.name || 'user'}
size={icon.size || iconSize}
type={icon.type && icon.type}
/>
));
// @ts-ignore
const hidePlaceholder = !(source && source.uri);
// Merge image container style
const imageContainerStyle = StyleSheet.flatten([
styles.overlayContainer,
rounded && { borderRadius: width / 2, overflow: 'hidden' },
overlayContainerStyle,
imageProps && imageProps.containerStyle,
]);
if (imageProps && imageProps.containerStyle) {
delete imageProps.containerStyle;
}
return (
<Component
onPress={onPress}
onLongPress={onLongPress}
const AvatarComponent: Avatar = ({
onPress,
onLongPress,
Component = onPress || onLongPress ? TouchableOpacity : View,
containerStyle,
icon,
iconStyle,
source,
size = 'small',
avatarStyle,
rounded,
title,
titleStyle,
overlayContainerStyle,
imageProps,
placeholderStyle,
renderPlaceholderContent,
ImageComponent = RNImage,
children,
...attributes
}: React.PropsWithChildren<AvatarProps>) => {
const width =
typeof size === 'number' ? size : avatarSizes[size] || avatarSizes.small;
const height = width;
const titleSize = width / 2;
const iconSize = width / 2;
const PlaceholderContent =
(renderPlaceholderContent &&
renderNode(undefined, renderPlaceholderContent)) ||
(title && (
<Text
style={StyleSheet.flatten([
styles.container,
{ height, width },
rounded && { borderRadius: width / 2 },
containerStyle,
styles.title,
{ fontSize: titleSize },
titleStyle,
])}
{...attributes}
>
<Image
placeholderStyle={StyleSheet.flatten([
placeholderStyle,
hidePlaceholder && styles.hiddenPlaceholderStyle,
])}
PlaceholderContent={PlaceholderContent}
containerStyle={imageContainerStyle}
source={source}
borderRadius={rounded ? width / 2 : undefined}
{...imageProps}
style={StyleSheet.flatten([
styles.avatar,
imageProps && imageProps.style,
avatarStyle,
])}
ImageComponent={ImageComponent}
/>
{children}
</Component>
);
},
{ Accessory: Accessory }
);
{title}
</Text>
)) ||
(icon && (
<Icon
//@ts-ignore
style={iconStyle && iconStyle}
color={icon.color || 'white'}
name={icon.name || 'user'}
size={icon.size || iconSize}
type={icon.type && icon.type}
/>
));
// @ts-ignore
const hidePlaceholder = !(source && source.uri);
// Merge image container style
const imageContainerStyle = StyleSheet.flatten([
styles.overlayContainer,
rounded && { borderRadius: width / 2, overflow: 'hidden' },
overlayContainerStyle,
imageProps && imageProps.containerStyle,
]);
if (imageProps && imageProps.containerStyle) {
delete imageProps.containerStyle;
}
return (
<Component
onPress={onPress}
onLongPress={onLongPress}
style={StyleSheet.flatten([
styles.container,
{ height, width },
rounded && { borderRadius: width / 2 },
containerStyle,
])}
{...attributes}
>
<Image
placeholderStyle={StyleSheet.flatten([
placeholderStyle,
hidePlaceholder && styles.hiddenPlaceholderStyle,
])}
PlaceholderContent={PlaceholderContent}
//@ts-ignore
containerStyle={imageContainerStyle}
source={source}
borderRadius={rounded ? width / 2 : undefined}
{...imageProps}
style={StyleSheet.flatten([
styles.avatar,
imageProps && imageProps.style,
avatarStyle,
])}
ImageComponent={ImageComponent}
/>
{children}
</Component>
);
};

const styles = StyleSheet.create({
container: {
Expand All @@ -176,6 +173,7 @@ const styles = StyleSheet.create({

const Avatar = React.memo(AvatarComponent, isEqual);
export { Avatar };
const ThemedAvatar = withTheme(Avatar, 'Avatar');

const ThemedAvatar = Object.assign(withTheme(Avatar, 'Avatar'), {
Accessory: Accessory,
});
export default ThemedAvatar;
28 changes: 14 additions & 14 deletions src/avatar/__tests__/__snapshots__/Avatar.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`Avatar Component Placeholders renders using icon prop 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
PlaceholderContent={
<Themed.Icon
Expand Down Expand Up @@ -57,7 +57,7 @@ exports[`Avatar Component Placeholders renders using icon with defaults 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
PlaceholderContent={
<Themed.Icon
Expand Down Expand Up @@ -103,7 +103,7 @@ exports[`Avatar Component Placeholders shouldn't show placeholder if not using s
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
PlaceholderContent={
<Text
Expand Down Expand Up @@ -151,7 +151,7 @@ exports[`Avatar Component Placeholders shouldn't show placeholder if source does
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
PlaceholderContent={
<Text
Expand Down Expand Up @@ -200,7 +200,7 @@ exports[`Avatar Component Placeholders shouldn't show placeholder if source exis
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
PlaceholderContent={
<Text
Expand Down Expand Up @@ -253,7 +253,7 @@ exports[`Avatar Component Sizes accepts a number 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down Expand Up @@ -287,7 +287,7 @@ exports[`Avatar Component Sizes accepts large 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down Expand Up @@ -321,7 +321,7 @@ exports[`Avatar Component Sizes accepts medium 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down Expand Up @@ -355,7 +355,7 @@ exports[`Avatar Component Sizes accepts small 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down Expand Up @@ -389,7 +389,7 @@ exports[`Avatar Component Sizes accepts xlarge 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down Expand Up @@ -423,7 +423,7 @@ exports[`Avatar Component allows custom imageProps 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down Expand Up @@ -459,7 +459,7 @@ exports[`Avatar Component renders rounded 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
borderRadius={17}
containerStyle={
Expand Down Expand Up @@ -497,7 +497,7 @@ exports[`Avatar Component renders touchable if onPress given 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down Expand Up @@ -753,7 +753,7 @@ exports[`Avatar Component should render without issues 1`] = `
}
}
>
<ForwardRef(forwardRef)
<Themed.Image
ImageComponent={[Function]}
containerStyle={
Object {
Expand Down
1 change: 1 addition & 0 deletions src/buttons/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,5 @@ const styles = StyleSheet.create({
});

export { Button };
//@ts-ignore
export default withTheme(Button, 'Button');
Loading

0 comments on commit 107ecde

Please sign in to comment.