Skip to content

Commit 25ce430

Browse files
authored
Added background view to error image to prevent it from being stretched (#1623)
* Added background view to error image to prevent it from being stretched * Fixed failing snapshot (only missing undefined object in this case) * rename * removed width and height props * Moved inline style into styles + lower zIndex to keep image children in priority * added additonal error-image sizes variations * refactor * typings * update snapshot
1 parent 174313f commit 25ce430

File tree

8 files changed

+60
-7
lines changed

8 files changed

+60
-7
lines changed
10.2 KB
Loading
15.4 KB
Loading
27.4 KB
Loading
43.6 KB
Loading

generatedTypes/src/commons/modifiers.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export declare function extractModifierProps(props: Dictionary<any>): _.Dictiona
9090
* TODO:
9191
* @deprecated switch to Modifiers#extractComponentProps
9292
*/
93-
export declare function extractOwnProps(props: Dictionary<any>, ignoreProps: string[]): _.Omit<_.Dictionary<any>, string>;
94-
export declare function extractComponentProps(component: any, props: Dictionary<any>, ignoreProps?: string[]): _.Omit<_.Dictionary<any>, string>;
93+
export declare function extractOwnProps(props: Dictionary<any>, ignoreProps: string[]): _.Omit<Partial<Dictionary<any>>, string>;
94+
export declare function extractComponentProps(component: any, props: Dictionary<any>, ignoreProps?: string[]): _.Omit<Partial<Dictionary<any>>, string>;
9595
export declare function getThemeProps(props?: any, context?: any): any;
9696
export declare function generateModifiersStyle(options: {
9797
color: boolean;

generatedTypes/src/components/image/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ declare class Image extends PureComponent<Props, State> {
9090
getImageSource(): any;
9191
onError: (event: NativeSyntheticEvent<ImageErrorEventData>) => void;
9292
renderSvg: () => JSX.Element;
93+
renderErrorImage: () => JSX.Element;
94+
renderImage: (useImageInsideContainer: boolean) => JSX.Element;
9395
renderRegularImage(): JSX.Element;
9496
render(): JSX.Element;
9597
}

src/components/button/__tests__/__snapshots__/index.spec.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ exports[`Button container size should have no padding of button is an icon butto
895895
},
896896
undefined,
897897
],
898+
false,
898899
]
899900
}
900901
/>
@@ -1722,6 +1723,7 @@ exports[`Button icon should apply color on icon 1`] = `
17221723
},
17231724
undefined,
17241725
],
1726+
false,
17251727
]
17261728
}
17271729
/>
@@ -1777,6 +1779,7 @@ exports[`Button icon should apply color on icon 2`] = `
17771779
},
17781780
undefined,
17791781
],
1782+
false,
17801783
]
17811784
}
17821785
/>
@@ -1835,6 +1838,7 @@ exports[`Button icon should include custom iconStyle provided as a prop 1`] = `
18351838
"tintColor": "red",
18361839
},
18371840
],
1841+
false,
18381842
]
18391843
}
18401844
/>
@@ -1892,6 +1896,7 @@ exports[`Button icon should return icon style according to different variations
18921896
},
18931897
undefined,
18941898
],
1899+
false,
18951900
]
18961901
}
18971902
/>
@@ -1947,6 +1952,7 @@ exports[`Button icon should return icon style according to different variations
19471952
},
19481953
undefined,
19491954
],
1955+
false,
19501956
]
19511957
}
19521958
/>
@@ -2002,6 +2008,7 @@ exports[`Button icon should return icon style according to different variations
20022008
},
20032009
undefined,
20042010
],
2011+
false,
20052012
]
20062013
}
20072014
/>
@@ -2984,6 +2991,7 @@ exports[`Button labelColor should return undefined color if this is an icon butt
29842991
},
29852992
undefined,
29862993
],
2994+
false,
29872995
]
29882996
}
29892997
/>

src/components/image/index.tsx

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {asBaseComponent, ForwardRefInjectedProps, BaseComponentInjectedProps, Ma
1616
import Assets from '../../assets';
1717
import Overlay, {OverlayTypeType, OverlayIntensityType} from '../overlay';
1818
import SvgImage from './SvgImage';
19+
import View from '../view';
20+
import {Colors} from '../../style';
1921

2022

2123
export type ImageProps = RNImageProps & MarginModifiers & {
@@ -159,14 +161,36 @@ class Image extends PureComponent<Props, State> {
159161
this.setState({error: true});
160162
this.props.onError?.(event);
161163
}
162-
}
164+
};
163165

164166
renderSvg = () => {
165167
const {source, ...others} = this.props;
166168
return <SvgImage data={source} {...others}/>;
167-
}
169+
};
168170

169-
renderRegularImage() {
171+
renderErrorImage = () => {
172+
const {
173+
style,
174+
cover,
175+
modifiers
176+
} = this.props;
177+
const {margins} = modifiers;
178+
179+
return (
180+
<View
181+
style={[
182+
margins,
183+
style,
184+
styles.errorImageContainer,
185+
cover && styles.coverImage
186+
]}
187+
>
188+
{this.renderImage(true)}
189+
</View>
190+
);
191+
};
192+
193+
renderImage = (useImageInsideContainer: boolean) => {
170194
const {error} = this.state;
171195
const source = error ? this.getVerifiedSource(this.props.errorSource) : this.getImageSource();
172196
const {
@@ -185,6 +209,7 @@ class Image extends PureComponent<Props, State> {
185209
const shouldFlipRTL = supportRTL && Constants.isRTL;
186210
const ImageView = this.shouldUseImageBackground() ? ImageBackground : RNImage;
187211
const {margins} = modifiers;
212+
const resizeMode = useImageInsideContainer ? 'contain' : undefined;
188213

189214
return (
190215
// @ts-ignore
@@ -195,9 +220,11 @@ class Image extends PureComponent<Props, State> {
195220
cover && styles.coverImage,
196221
this.isGif() && styles.gifImage,
197222
aspectRatio && {aspectRatio},
198-
margins,
199-
style
223+
!useImageInsideContainer && margins,
224+
style,
225+
useImageInsideContainer && styles.shrink
200226
]}
227+
resizeMode={resizeMode}
201228
accessible={false}
202229
accessibilityRole={'image'}
203230
{...others}
@@ -214,6 +241,15 @@ class Image extends PureComponent<Props, State> {
214241
)}
215242
</ImageView>
216243
);
244+
};
245+
246+
renderRegularImage() {
247+
const {error} = this.state;
248+
if (error) {
249+
return this.renderErrorImage();
250+
} else {
251+
return this.renderImage(false);
252+
}
217253
}
218254

219255
render() {
@@ -237,6 +273,13 @@ const styles = StyleSheet.create({
237273
},
238274
gifImage: {
239275
overflow: 'hidden'
276+
},
277+
errorImageContainer: {
278+
backgroundColor: Colors.grey70,
279+
zIndex: -1
280+
},
281+
shrink: {
282+
flexShrink: 1
240283
}
241284
});
242285

0 commit comments

Comments
 (0)