Skip to content

Image - pass width and height to image style #2384

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

Merged
merged 9 commits into from
Jan 9, 2023
55 changes: 37 additions & 18 deletions demo/src/screens/__tests__/__snapshots__/AvatarScreen.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 50,
}
}
height={50}
onError={[Function]}
source={
Object {
Expand All @@ -830,6 +829,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 50,
"width": 50,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -851,7 +854,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={50}
/>
<View
accessibilityLabel="badge"
Expand Down Expand Up @@ -1039,7 +1041,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 40,
}
}
height={40}
onError={[Function]}
source={
Object {
Expand All @@ -1055,6 +1056,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 40,
"width": 40,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -1076,7 +1081,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={40}
/>
<View
accessibilityLabel="badge"
Expand Down Expand Up @@ -1275,7 +1279,6 @@ exports[`AvatarScreen renders screen 1`] = `
accessibilityRole="image"
accessible={false}
assetGroup="icons"
height={60}
onError={[Function]}
onLoad={[Function]}
source={
Expand All @@ -1292,6 +1295,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 60,
"width": 60,
},
Array [
Array [
Object {
Expand All @@ -1318,7 +1325,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={60}
/>
</View>
</View>
Expand Down Expand Up @@ -1445,7 +1451,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 70,
}
}
height={70}
onError={[Function]}
source={
Object {
Expand All @@ -1461,6 +1466,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 70,
"width": 70,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -1482,7 +1491,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={70}
/>
<View
accessibilityLabel="badge"
Expand Down Expand Up @@ -1670,7 +1678,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 60,
}
}
height={60}
onError={[Function]}
source={
Object {
Expand All @@ -1686,6 +1693,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 60,
"width": 60,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -1707,7 +1718,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={60}
/>
<View
accessibilityLabel="badge"
Expand Down Expand Up @@ -1805,6 +1815,7 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
undefined,
Object {
"backgroundColor": "#FFC50D",
"flex": 1,
Expand Down Expand Up @@ -1939,7 +1950,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 48,
}
}
height={48}
onError={[Function]}
source={
Object {
Expand All @@ -1955,6 +1965,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 48,
"width": 48,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -1976,7 +1990,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={48}
/>
</View>
</View>
Expand Down Expand Up @@ -2139,7 +2152,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 50,
}
}
height={50}
onError={[Function]}
onLoadEnd={[Function]}
onLoadStart={[Function]}
Expand All @@ -2157,6 +2169,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 50,
"width": 50,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -2178,7 +2194,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={50}
/>
</View>
</View>
Expand Down Expand Up @@ -2341,7 +2356,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 50,
}
}
height={50}
onError={[Function]}
onLoadEnd={[Function]}
onLoadStart={[Function]}
Expand All @@ -2359,6 +2373,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 50,
"width": 50,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -2380,7 +2398,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={50}
/>
</View>
</View>
Expand Down Expand Up @@ -2506,7 +2523,6 @@ exports[`AvatarScreen renders screen 1`] = `
"width": 50,
}
}
height={50}
onError={[Function]}
source={
Object {
Expand All @@ -2522,6 +2538,10 @@ exports[`AvatarScreen renders screen 1`] = `
undefined,
undefined,
false,
Object {
"height": 50,
"width": 50,
},
Array [
Object {
"alignItems": "center",
Expand All @@ -2543,7 +2563,6 @@ exports[`AvatarScreen renders screen 1`] = `
]
}
testID="undefined.image"
width={50}
/>
</View>
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ exports[`Button container size should have no padding of button is an icon butto
undefined,
undefined,
false,
undefined,
Array [
Object {
"marginRight": 8,
Expand Down Expand Up @@ -1819,6 +1820,7 @@ exports[`Button icon should apply color on icon 1`] = `
undefined,
undefined,
false,
undefined,
Array [
Object {
"tintColor": "green",
Expand Down Expand Up @@ -1876,6 +1878,7 @@ exports[`Button icon should apply color on icon 2`] = `
undefined,
undefined,
false,
undefined,
Array [
Object {
"tintColor": "#FC3D2F",
Expand Down Expand Up @@ -1933,6 +1936,7 @@ exports[`Button icon should include custom iconStyle provided as a prop 1`] = `
undefined,
undefined,
false,
undefined,
Array [
Object {
"tintColor": "#FFFFFF",
Expand Down Expand Up @@ -1995,6 +1999,7 @@ exports[`Button icon should return icon style according to different variations
undefined,
undefined,
false,
undefined,
Array [
Object {
"tintColor": "#5A48F5",
Expand Down Expand Up @@ -2052,6 +2057,7 @@ exports[`Button icon should return icon style according to different variations
undefined,
undefined,
false,
undefined,
Array [
Object {
"tintColor": "#5A48F5",
Expand Down Expand Up @@ -2114,6 +2120,7 @@ exports[`Button icon should return icon style according to different variations
undefined,
undefined,
false,
undefined,
Array [
Object {
"tintColor": "#FFFFFF",
Expand Down Expand Up @@ -3116,6 +3123,7 @@ exports[`Button labelColor should return undefined color if this is an icon butt
undefined,
undefined,
false,
undefined,
Array [
Object {
"tintColor": "#FFFFFF",
Expand Down
4 changes: 4 additions & 0 deletions src/components/image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class Image extends PureComponent<Props, State> {
const {
tintColor,
style,
width,
height,
supportRTL,
cover,
aspectRatio,
Expand All @@ -221,6 +223,7 @@ class Image extends PureComponent<Props, State> {
const shouldFlipRTL = supportRTL && Constants.isRTL;
const ImageView = this.shouldUseImageBackground() ? ImageBackground : RNImage;
const {margins} = modifiers;
const sizeProps = width || height;

return (
// @ts-ignore
Expand All @@ -233,6 +236,7 @@ class Image extends PureComponent<Props, State> {
aspectRatio && {aspectRatio},
!useImageInsideContainer && margins,
useImageInsideContainer && styles.containImage,
!cover && sizeProps && {width, height},
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think the cover is needed here, if the user makes a mistake they can fix it (this will be bugged without an overlay as well)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, personally I prefer to remove the extra const sizeProps.
Maybe just {width, height} or (width || height) && {width, height}?

Copy link
Collaborator Author

@Inbal-Tish Inbal-Tish Jan 8, 2023

Choose a reason for hiding this comment

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

What do you mean by 'this will be bugged without an overlay as well'? This solution works whether you pass overlay or not...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I meant that if the user adds width and forgets height (or vice versa, or adds a width with cover etc) they will have bugs that they will need to fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's for sure

style,
useImageInsideContainer && styles.shrink
]}
Expand Down