Skip to content
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

feat: support onLoad, onError and fallbackSource #3

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const NativeBlastedImage = NativeModules.BlastedImage

const BlastedImageView = requireNativeComponent('BlastedImageView');

const BlastedImage = ({ source, width, height, style, resizeMode, isBackground, children }) => {
const BlastedImage = ({ source, width, onLoad, onError, fallbackSource, height, style, resizeMode, isBackground, children }) => {
const [error, setError] = useState(false);

if (!source || (!source.uri && typeof source !== 'number')) {
Expand All @@ -48,9 +48,11 @@ const BlastedImage = ({ source, width, height, style, resizeMode, isBackground,
const fetchImage = async () => {
try {
await loadImage(source.uri, source.headers);
onLoad?.();
} catch (err) {
setError(true);
console.error(err);
onError?.(err);
}
};

Expand Down Expand Up @@ -84,7 +86,7 @@ const BlastedImage = ({ source, width, height, style, resizeMode, isBackground,
console.log("For maximum performance, BlastedImage does not support width defined as a percentage");
return;
}

if (typeof height === 'string' && height.includes('%')) {
console.log("For maximum performance, BlastedImage does not support height defined as a percentage");
return;
Expand All @@ -109,31 +111,35 @@ const BlastedImage = ({ source, width, height, style, resizeMode, isBackground,
alignItems:'center',
width: adjustedWidth,
height: adjustedHeight,
};
};

return (
<View style={!isBackground ? viewStyle : null}>
{isBackground ? (
<View style={viewStyle}>
{renderImageContent(error, source, adjustedHeight, adjustedWidth, resizeMode)}
{renderImageContent(error, source, fallbackSource, adjustedHeight, adjustedWidth, resizeMode)}
</View>
) : (
renderImageContent(error, source, adjustedHeight, adjustedWidth, resizeMode)
renderImageContent(error, source, fallbackSource, adjustedHeight, adjustedWidth, resizeMode)
)}
{isBackground && <View style={childrenStyle}>{children}</View>}
</View>
);
};

function renderImageContent(error, source, adjustedHeight, adjustedWidth, resizeMode) {
function renderImageContent(error, source, fallbackSource, adjustedHeight, adjustedWidth, resizeMode) {
if (error) {
return (
<Image
source={require('./assets/image-error.png')}
style={{ width: adjustedHeight, height: adjustedHeight }}
resizeMode={resizeMode}
/>
);
if (fallbackSource) {
return (
<Image
source={fallbackSource}
style={{ width: adjustedHeight, height: adjustedHeight }}
resizeMode={resizeMode}
/>
);
} else {
return null;
}
} else if (typeof source === 'number') {
return (
<Image
Expand Down