Skip to content

Added background view to error image to prevent it from being stretched #1623

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 10 commits into from
Nov 3, 2021
Merged
Binary file added demo/src/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/assets/images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions generatedTypes/src/commons/modifiers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export declare function extractModifierProps(props: Dictionary<any>): _.Dictiona
* TODO:
* @deprecated switch to Modifiers#extractComponentProps
*/
export declare function extractOwnProps(props: Dictionary<any>, ignoreProps: string[]): _.Omit<_.Dictionary<any>, string>;
export declare function extractComponentProps(component: any, props: Dictionary<any>, ignoreProps?: string[]): _.Omit<_.Dictionary<any>, string>;
export declare function extractOwnProps(props: Dictionary<any>, ignoreProps: string[]): _.Omit<Partial<Dictionary<any>>, string>;
export declare function extractComponentProps(component: any, props: Dictionary<any>, ignoreProps?: string[]): _.Omit<Partial<Dictionary<any>>, string>;
export declare function getThemeProps(props?: any, context?: any): any;
export declare function generateModifiersStyle(options: {
color: boolean;
Expand Down
2 changes: 2 additions & 0 deletions generatedTypes/src/components/image/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ declare class Image extends PureComponent<Props, State> {
getImageSource(): any;
onError: (event: NativeSyntheticEvent<ImageErrorEventData>) => void;
renderSvg: () => JSX.Element;
renderErrorImage: () => JSX.Element;
renderImage: (useImageInsideContainer: boolean) => JSX.Element;
renderRegularImage(): JSX.Element;
render(): JSX.Element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ exports[`Button container size should have no padding of button is an icon butto
},
undefined,
],
false,
]
}
/>
Expand Down Expand Up @@ -1722,6 +1723,7 @@ exports[`Button icon should apply color on icon 1`] = `
},
undefined,
],
false,
]
}
/>
Expand Down Expand Up @@ -1777,6 +1779,7 @@ exports[`Button icon should apply color on icon 2`] = `
},
undefined,
],
false,
]
}
/>
Expand Down Expand Up @@ -1835,6 +1838,7 @@ exports[`Button icon should include custom iconStyle provided as a prop 1`] = `
"tintColor": "red",
},
],
false,
]
}
/>
Expand Down Expand Up @@ -1892,6 +1896,7 @@ exports[`Button icon should return icon style according to different variations
},
undefined,
],
false,
]
}
/>
Expand Down Expand Up @@ -1947,6 +1952,7 @@ exports[`Button icon should return icon style according to different variations
},
undefined,
],
false,
]
}
/>
Expand Down Expand Up @@ -2002,6 +2008,7 @@ exports[`Button icon should return icon style according to different variations
},
undefined,
],
false,
]
}
/>
Expand Down Expand Up @@ -2984,6 +2991,7 @@ exports[`Button labelColor should return undefined color if this is an icon butt
},
undefined,
],
false,
]
}
/>
Expand Down
53 changes: 48 additions & 5 deletions src/components/image/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {asBaseComponent, ForwardRefInjectedProps, BaseComponentInjectedProps, Ma
import Assets from '../../assets';
import Overlay, {OverlayTypeType, OverlayIntensityType} from '../overlay';
import SvgImage from './SvgImage';
import View from '../view';
import {Colors} from '../../style';


export type ImageProps = RNImageProps & MarginModifiers & {
Expand Down Expand Up @@ -159,14 +161,36 @@ class Image extends PureComponent<Props, State> {
this.setState({error: true});
this.props.onError?.(event);
}
}
};

renderSvg = () => {
const {source, ...others} = this.props;
return <SvgImage data={source} {...others}/>;
}
};

renderRegularImage() {
renderErrorImage = () => {
const {
style,
cover,
modifiers
} = this.props;
const {margins} = modifiers;

return (
<View
style={[
margins,
style,
styles.errorImageContainer,
cover && styles.coverImage
]}
>
{this.renderImage(true)}
</View>
);
};

renderImage = (useImageInsideContainer: boolean) => {
const {error} = this.state;
const source = error ? this.getVerifiedSource(this.props.errorSource) : this.getImageSource();
const {
Expand All @@ -185,6 +209,7 @@ class Image extends PureComponent<Props, State> {
const shouldFlipRTL = supportRTL && Constants.isRTL;
const ImageView = this.shouldUseImageBackground() ? ImageBackground : RNImage;
const {margins} = modifiers;
const resizeMode = useImageInsideContainer ? 'contain' : undefined;

return (
// @ts-ignore
Expand All @@ -195,9 +220,11 @@ class Image extends PureComponent<Props, State> {
cover && styles.coverImage,
this.isGif() && styles.gifImage,
aspectRatio && {aspectRatio},
margins,
style
!useImageInsideContainer && margins,
style,
useImageInsideContainer && styles.shrink
]}
resizeMode={resizeMode}
accessible={false}
accessibilityRole={'image'}
{...others}
Expand All @@ -214,6 +241,15 @@ class Image extends PureComponent<Props, State> {
)}
</ImageView>
);
};

renderRegularImage() {
const {error} = this.state;
if (error) {
return this.renderErrorImage();
} else {
return this.renderImage(false);
}
}

render() {
Expand All @@ -237,6 +273,13 @@ const styles = StyleSheet.create({
},
gifImage: {
overflow: 'hidden'
},
errorImageContainer: {
backgroundColor: Colors.grey70,
zIndex: -1
},
shrink: {
flexShrink: 1
}
});

Expand Down