File tree 4 files changed +36
-16
lines changed
packages/overdrive/lib/components
4 files changed +36
-16
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @autoguru/overdrive ' : minor
3
+ ---
4
+
5
+ SimpleImage component gets error fallback option
Original file line number Diff line number Diff line change 1
1
import type { IconType } from '@autoguru/icons' ;
2
- import { useNullCheck } from '../../hooks/useNullCheck' ;
3
2
import type { FunctionComponent , ReactElement , SVGAttributes } from 'react' ;
4
3
import * as React from 'react' ;
5
4
import { cloneElement } from 'react' ;
6
5
6
+ import { useNullCheck } from '../../hooks/useNullCheck' ;
7
7
import { resolveResponsiveStyle } from '../../utils/resolveResponsiveProps' ;
8
8
import { ResponsiveProp } from '../../utils/responsiveProps.css' ;
9
9
import type { BoxStyleProps } from '../Box' ;
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ const iconOptions = {
27
27
PlusIcon,
28
28
StarIcon,
29
29
CheckIcon,
30
- empty : null
30
+ empty : null ,
31
31
} ;
32
32
33
33
export default {
Original file line number Diff line number Diff line change 1
1
import * as React from 'react' ;
2
- import { FunctionComponent } from 'react' ;
2
+ import { FunctionComponent , ReactElement , useState } from 'react' ;
3
3
4
4
export interface Props
5
5
extends Partial <
@@ -36,6 +36,8 @@ export interface Props
36
36
height ?: string ;
37
37
38
38
className ?: string ;
39
+
40
+ fallbackComponent ?: ReactElement ; // Add this line
39
41
}
40
42
41
43
export const SimpleImage : FunctionComponent < Props > = ( {
@@ -44,17 +46,30 @@ export const SimpleImage: FunctionComponent<Props> = ({
44
46
className = '' ,
45
47
src,
46
48
srcSet,
49
+ fallbackComponent, // Add this line
47
50
...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
+ } ;
You can’t perform that action at this time.
0 commit comments