Skip to content

Commit 3a42294

Browse files
committed
feat: Adds SimpleImage component error fallback option
1 parent 27905e7 commit 3a42294

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

.changeset/gentle-bobcats-taste.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@autoguru/overdrive': minor
3+
---
4+
5+
SimpleImage component gets error fallback option

packages/overdrive/lib/components/Icon/Icon.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { IconType } from '@autoguru/icons';
2-
import { useNullCheck } from '../../hooks/useNullCheck';
32
import type { FunctionComponent, ReactElement, SVGAttributes } from 'react';
43
import * as React from 'react';
54
import { cloneElement } from 'react';
65

6+
import { useNullCheck } from '../../hooks/useNullCheck';
77
import { resolveResponsiveStyle } from '../../utils/resolveResponsiveProps';
88
import { ResponsiveProp } from '../../utils/responsiveProps.css';
99
import type { BoxStyleProps } from '../Box';

packages/overdrive/lib/components/Icon/stories.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const iconOptions = {
2727
PlusIcon,
2828
StarIcon,
2929
CheckIcon,
30-
empty: null
30+
empty: null,
3131
};
3232

3333
export default {

packages/overdrive/lib/components/Image/SimpleImage.tsx

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { FunctionComponent } from 'react';
2+
import { FunctionComponent, ReactElement, useState } from 'react';
33

44
export interface Props
55
extends Partial<
@@ -36,6 +36,8 @@ export interface Props
3636
height?: string;
3737

3838
className?: string;
39+
40+
fallbackComponent?: ReactElement; // Add this line
3941
}
4042

4143
export const SimpleImage: FunctionComponent<Props> = ({
@@ -44,17 +46,30 @@ export const SimpleImage: FunctionComponent<Props> = ({
4446
className = '',
4547
src,
4648
srcSet,
49+
fallbackComponent, // Add this line
4750
...imgProps
48-
}) => (
49-
// @ts-ignore
50-
<img
51-
loading={eager ? 'eager' : 'lazy'}
52-
decoding={syncDecoding ? 'sync' : 'async'}
53-
className={className}
54-
srcSet={srcSet}
55-
src={src}
56-
{...imgProps}
57-
/>
58-
);
59-
60-
export default SimpleImage;
51+
}) => {
52+
const [hasError, setHasError] = useState(false);
53+
54+
const handleError = () => {
55+
setHasError(true);
56+
};
57+
58+
if (hasError && fallbackComponent) {
59+
// Render fallback component
60+
return fallbackComponent;
61+
}
62+
63+
return (
64+
/*@ts-ignore*/
65+
<img
66+
loading={eager ? 'eager' : 'lazy'}
67+
decoding={syncDecoding ? 'sync' : 'async'}
68+
className={className}
69+
srcSet={srcSet}
70+
src={src}
71+
onError={handleError}
72+
{...imgProps}
73+
/>
74+
);
75+
};

0 commit comments

Comments
 (0)