xxf_react 0.3.9 → 0.4.0
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.
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { ImageProps } from "next/image";
|
|
2
|
-
import React from "react";
|
|
2
|
+
import React, { ReactNode } from "react";
|
|
3
3
|
export interface XImageProps extends ImageProps {
|
|
4
|
-
/** 图片加载失败时显示的备用图片 URL */
|
|
5
|
-
fallback?: string;
|
|
4
|
+
/** 图片加载失败时显示的备用图片 URL 或自定义组件 */
|
|
5
|
+
fallback?: string | ReactNode;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
* 增强的
|
|
8
|
+
* 增强的 Image 组件,支持加载失败时显示备用图片或自定义组件
|
|
9
9
|
* @param src - 图片地址
|
|
10
|
-
* @param fallback - 加载失败时显示的备用图片 URL
|
|
10
|
+
* @param fallback - 加载失败时显示的备用图片 URL 或 ReactNode
|
|
11
11
|
* @param onError - 加载失败回调
|
|
12
12
|
* @param className - 样式类名
|
|
13
|
-
* @param props - 其他 Next.js
|
|
13
|
+
* @param props - 其他 Next.js Image 支持的属性
|
|
14
14
|
*/
|
|
15
15
|
export declare function XImage({ src, fallback, onError, ...props }: XImageProps): React.JSX.Element;
|
|
16
16
|
export default XImage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"XImage.d.ts","sourceRoot":"","sources":["../../../src/layout/image/XImage.tsx"],"names":[],"mappings":"AAEA,OAAc,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAC7C,OAAO,
|
|
1
|
+
{"version":3,"file":"XImage.d.ts","sourceRoot":"","sources":["../../../src/layout/image/XImage.tsx"],"names":[],"mappings":"AAEA,OAAc,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,EAAsB,SAAS,EAAC,MAAM,OAAO,CAAC;AAE5D,MAAM,WAAW,WAAY,SAAQ,UAAU;IAC3C,gCAAgC;IAChC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACjC;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,EACI,GAAG,EACH,QAAQ,EACR,OAAO,EACP,GAAG,KAAK,EACX,EAAE,WAAW,qBAkDpC;AAED,eAAe,MAAM,CAAC"}
|
|
@@ -2,25 +2,44 @@
|
|
|
2
2
|
import Image from "next/image";
|
|
3
3
|
import React, { useState, useEffect } from "react";
|
|
4
4
|
/**
|
|
5
|
-
* 增强的
|
|
5
|
+
* 增强的 Image 组件,支持加载失败时显示备用图片或自定义组件
|
|
6
6
|
* @param src - 图片地址
|
|
7
|
-
* @param fallback - 加载失败时显示的备用图片 URL
|
|
7
|
+
* @param fallback - 加载失败时显示的备用图片 URL 或 ReactNode
|
|
8
8
|
* @param onError - 加载失败回调
|
|
9
9
|
* @param className - 样式类名
|
|
10
|
-
* @param props - 其他 Next.js
|
|
10
|
+
* @param props - 其他 Next.js Image 支持的属性
|
|
11
11
|
*/
|
|
12
12
|
export function XImage({ src, fallback, onError, ...props }) {
|
|
13
13
|
const [imgSrc, setImgSrc] = useState(src);
|
|
14
|
-
|
|
14
|
+
const [hasError, setHasError] = useState(false);
|
|
15
|
+
// src 变化时重置状态
|
|
15
16
|
useEffect(() => {
|
|
16
17
|
setImgSrc(src);
|
|
18
|
+
setHasError(false);
|
|
17
19
|
}, [src]);
|
|
18
20
|
const handleError = (e) => {
|
|
19
|
-
if (fallback &&
|
|
20
|
-
|
|
21
|
+
if (fallback && !hasError) {
|
|
22
|
+
setHasError(true);
|
|
23
|
+
// 如果 fallback 是字符串(URL),则切换图片源
|
|
24
|
+
if (typeof fallback === "string") {
|
|
25
|
+
setImgSrc(fallback);
|
|
26
|
+
return; // fallback 切换时不触发 onError,等 fallback 也失败再触发
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
onError === null || onError === void 0 ? void 0 : onError(e);
|
|
23
30
|
};
|
|
31
|
+
// fallback 是 ReactNode 且已出错,用容器包裹并应用样式属性
|
|
32
|
+
if (hasError && fallback && typeof fallback !== "string") {
|
|
33
|
+
const width = typeof props.width === "number" ? props.width : props.width ? Number(props.width) : undefined;
|
|
34
|
+
const height = typeof props.height === "number" ? props.height : props.height ? Number(props.height) : undefined;
|
|
35
|
+
return (React.createElement("div", { className: props.className, style: {
|
|
36
|
+
width,
|
|
37
|
+
height,
|
|
38
|
+
position: props.fill ? "absolute" : undefined,
|
|
39
|
+
inset: props.fill ? 0 : undefined,
|
|
40
|
+
...props.style,
|
|
41
|
+
} }, fallback));
|
|
42
|
+
}
|
|
24
43
|
return (React.createElement(Image, { ...props, src: imgSrc, onError: handleError }));
|
|
25
44
|
}
|
|
26
45
|
export default XImage;
|