@dropi/react-native-design-system 0.2.26 → 0.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -4,8 +4,6 @@ type Src = string | number;
|
|
|
4
4
|
interface CustomImageProps extends Omit<ImageProps, 'source' | 'placeholder'> {
|
|
5
5
|
source: Src;
|
|
6
6
|
fallbackSource?: Src;
|
|
7
|
-
placeholderSource?: Src;
|
|
8
|
-
retryCount?: number;
|
|
9
7
|
}
|
|
10
8
|
export declare const CustomImage: React.FC<CustomImageProps>;
|
|
11
9
|
export {};
|
|
@@ -17,41 +17,28 @@ const normalize = src => {
|
|
|
17
17
|
const CustomImage = ({
|
|
18
18
|
source,
|
|
19
19
|
fallbackSource,
|
|
20
|
-
placeholderSource,
|
|
21
|
-
retryCount = 1,
|
|
22
20
|
contentFit = 'cover',
|
|
23
21
|
...rest
|
|
24
22
|
}) => {
|
|
25
|
-
const [
|
|
26
|
-
const [attempts, setAttempts] = (0, _react.useState)(0);
|
|
27
|
-
const [usedFallback, setUsedFallback] = (0, _react.useState)(false);
|
|
23
|
+
const [hasError, setHasError] = (0, _react.useState)(false);
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
setUsedFallback(false);
|
|
34
|
-
}, [source]);
|
|
35
|
-
const handleError = (0, _react.useCallback)(() => {
|
|
36
|
-
/* retry first */
|
|
37
|
-
if (attempts < retryCount) {
|
|
38
|
-
setAttempts(a => a + 1);
|
|
39
|
-
setCurrentSource(source);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
25
|
+
// If source is empty or there's an error, use fallback
|
|
26
|
+
const displaySource = !source || hasError ? fallbackSource : source;
|
|
27
|
+
const normalizedSource = normalize(displaySource);
|
|
28
|
+
const normalizedPlaceholder = normalize(fallbackSource);
|
|
42
29
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
}, [attempts, retryCount, fallbackSource, usedFallback, source]);
|
|
30
|
+
// If no valid source, don't render anything
|
|
31
|
+
if (!normalizedSource) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
49
34
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_expoImage.Image, {
|
|
50
35
|
...rest,
|
|
51
|
-
source:
|
|
52
|
-
|
|
36
|
+
source: normalizedSource,
|
|
37
|
+
...(normalizedPlaceholder && !hasError && {
|
|
38
|
+
placeholder: normalizedPlaceholder
|
|
39
|
+
}),
|
|
53
40
|
contentFit: contentFit,
|
|
54
|
-
onError:
|
|
41
|
+
onError: () => setHasError(true)
|
|
55
42
|
});
|
|
56
43
|
};
|
|
57
44
|
exports.CustomImage = CustomImage;
|