-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/popup
- Loading branch information
Showing
74 changed files
with
5,829 additions
and
849 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule _common
updated
14 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,132 @@ | ||
import React, { FC, useCallback, useMemo, useRef, useState } from 'react'; | ||
import ClassNames from 'classnames'; | ||
import { CloseIcon, EllipsisIcon } from 'tdesign-icons-react'; | ||
import { useInViewport } from 'ahooks'; | ||
import useConfig from '../_util/useConfig'; | ||
import React, { Fragment, useRef, useMemo, useState, useEffect } from 'react'; | ||
import type { SyntheticEvent } from 'react'; | ||
import classNames from 'classnames'; | ||
import { CloseIcon as TdCloseIcon } from 'tdesign-icons-react'; | ||
import Loading from '../loading'; | ||
import observe from '../_common/js/utils/observe'; | ||
import { StyledProps } from '../common'; | ||
import { TdImageProps } from './type'; | ||
import { imageDefaultProps } from './defaultProps'; | ||
import parseTNode from '../_util/parseTNode'; | ||
import useDefaultProps from '../hooks/useDefaultProps'; | ||
import { usePrefixClass } from '../hooks/useClass'; | ||
|
||
export interface ImageProps extends TdImageProps, Omit<React.ImgHTMLAttributes<HTMLImageElement>, 'loading' | 'onError' | 'onLoad'> {} | ||
export interface ImageProps extends TdImageProps, StyledProps {} | ||
|
||
const Image: FC<ImageProps> = React.memo((props) => { | ||
const Image: React.FC<ImageProps> = (props) => { | ||
const { | ||
className, | ||
style, | ||
src, | ||
alt, | ||
fit = 'fill', | ||
onLoad, | ||
loading, | ||
onError, | ||
error, | ||
fallback, | ||
fit, | ||
lazy, | ||
shape = 'round', | ||
loading, | ||
shape, | ||
position, | ||
error, | ||
className, | ||
style, | ||
...others | ||
} = props; | ||
|
||
const [isLoad, setIsLoad] = useState(true); | ||
const [isError, setIsError] = useState(false); | ||
const ref = useRef<HTMLImageElement>(null); | ||
const hasLoad = useRef<boolean>(false); | ||
referrerpolicy, | ||
srcset, | ||
onError, | ||
onLoad, | ||
} = useDefaultProps<ImageProps>(props, imageDefaultProps); | ||
|
||
// 统一配置信息 | ||
const { classPrefix } = useConfig(); | ||
const imageClass = usePrefixClass('image'); | ||
const imageRef = useRef<HTMLDivElement>(null); | ||
|
||
// 观察元素是否在可见区域 | ||
const [isInViewport] = useInViewport(ref); | ||
const [imageSrc, setImageSrc] = useState(src); | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
const [isError, setIsError] = useState(false); | ||
const [shouldLoad, setShouldLoad] = useState(!lazy); | ||
|
||
const prefix = useMemo(() => `${classPrefix}-image`, [classPrefix]); | ||
const rootClasses = classNames( | ||
{ | ||
[`${imageClass}`]: true, | ||
[`${imageClass}--${shape}`]: true, | ||
}, | ||
className, | ||
); | ||
|
||
// Loading Element | ||
const LoadingStatus = useMemo(() => { | ||
if (!isLoad) return null; | ||
|
||
return loading || <EllipsisIcon />; | ||
}, [isLoad, loading]); | ||
if (!(isError || !shouldLoad) && !isLoaded) return loading || <Loading theme="dots" inheritColor={true} />; | ||
}, [isError, shouldLoad, isLoaded, loading]); | ||
|
||
// Loading Failed Element | ||
const FailedStatus = useMemo(() => { | ||
if (!isError) return null; | ||
|
||
return error || <CloseIcon />; | ||
if (isError) return error || <TdCloseIcon size="22px" />; | ||
}, [isError, error]); | ||
|
||
// Image Src | ||
const imgSrc = useMemo(() => { | ||
if (!lazy || hasLoad.current) return src; | ||
|
||
if (isInViewport) return src; | ||
|
||
return ''; | ||
}, [src, lazy, isInViewport]); | ||
const handleLoadImage = () => { | ||
setShouldLoad(true); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!lazy || !imageRef?.current) { | ||
return; | ||
} | ||
|
||
const handleUnObserve = (element) => { | ||
const observer = observe(element, null, handleLoadImage, 0); | ||
return () => { | ||
observer && observer.unobserve(element); | ||
}; | ||
}; | ||
|
||
return handleUnObserve(imageRef.current); | ||
}, [lazy, imageRef]); | ||
|
||
// 图片加载完成回调 | ||
const handleLoad = (e: SyntheticEvent<HTMLImageElement>) => { | ||
setIsLoaded(true); | ||
onLoad?.({ e }); | ||
}; | ||
|
||
// 图片加载失败回调 | ||
const handleError = (e: SyntheticEvent<HTMLImageElement>) => { | ||
setIsError(true); | ||
if (fallback) { | ||
setImageSrc(fallback); | ||
} | ||
onError?.({ e }); | ||
}; | ||
|
||
const renderMask = () => | ||
LoadingStatus || FailedStatus ? ( | ||
<div className={`${imageClass}__mask`}>{parseTNode(LoadingStatus || FailedStatus)}</div> | ||
) : null; | ||
|
||
const renderImage = () => ( | ||
<img | ||
className={classNames(`${imageClass}__img`, `${imageClass}--fit-${fit}`, `${imageClass}--position-${position}`)} | ||
src={imageSrc} | ||
alt={alt} | ||
referrerPolicy={referrerpolicy} | ||
onLoad={handleLoad} | ||
onError={handleError} | ||
/> | ||
); | ||
|
||
// Get ClassName By Prefix | ||
const getClass = useCallback((cls: string) => `${prefix}__${cls}`, [prefix]); | ||
const renderImageSrcset = () => ( | ||
<picture> | ||
{Object.entries(srcset).map(([type, url]) => ( | ||
<source key={url} type={type} srcSet={url} /> | ||
))} | ||
{src && renderImage()} | ||
</picture> | ||
); | ||
|
||
return ( | ||
<div className={ClassNames(prefix, `${prefix}--${shape}`, { [className]: className })} ref={ref}> | ||
{LoadingStatus || FailedStatus ? <div className={getClass('status')}>{LoadingStatus || FailedStatus}</div> : null} | ||
<img | ||
{...others} | ||
className={getClass('img')} | ||
src={imgSrc} | ||
alt={alt} | ||
style={{ objectFit: fit, objectPosition: position, width: 'inherit', height: 'inherit', ...style }} | ||
onLoad={() => { | ||
hasLoad.current = true; | ||
setIsLoad(false); | ||
setIsError(false); | ||
onLoad && onLoad(); | ||
}} | ||
onError={() => { | ||
if (imgSrc) { | ||
hasLoad.current = true; | ||
setIsLoad(false); | ||
setIsError(true); | ||
onError && onError(); | ||
} | ||
}} | ||
/> | ||
<div ref={imageRef} className={rootClasses} style={style}> | ||
{renderMask()} | ||
{!isError && shouldLoad && ( | ||
<Fragment>{srcset && Object.keys(srcset).length ? renderImageSrcset() : renderImage()}</Fragment> | ||
)} | ||
</div> | ||
); | ||
}); | ||
}; | ||
|
||
Image.displayName = 'Image'; | ||
|
||
export default Image; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react'; | ||
import { Image } from 'tdesign-mobile-react'; | ||
|
||
const imageSrc = 'https://tdesign.gtimg.com/demo/demo-image-1.png'; | ||
|
||
export default function BaseImage() { | ||
return ( | ||
<div className="image-example"> | ||
<div className="image-example-title">不同填充模式的图片</div> | ||
<div className="image-example-desc">提供 fill、contain、cover、none、scale-down 5 种填充类型。</div> | ||
<div className="image-group"> | ||
<div className="image-demo"> | ||
<p className="image-demo-tip">fill</p> | ||
<Image | ||
className="image-container" | ||
style={{ width: '72px', height: '72px' }} | ||
fit="fill" | ||
src={imageSrc} | ||
></Image> | ||
</div> | ||
<div className="image-demo"> | ||
<p className="image-demo-tip">contain</p> | ||
<Image | ||
className="image-container" | ||
style={{ width: '72px', height: '72px' }} | ||
fit="contain" | ||
src={imageSrc} | ||
></Image> | ||
</div> | ||
<div className="image-demo"> | ||
<p className="image-demo-tip">cover</p> | ||
<Image | ||
className="image-container" | ||
style={{ width: '72px', height: '72px' }} | ||
fit="cover" | ||
src={imageSrc} | ||
></Image> | ||
</div> | ||
<div className="image-demo"> | ||
<p className="image-demo-tip">none</p> | ||
<Image | ||
className="image-container" | ||
style={{ width: '72px', height: '72px' }} | ||
fit="none" | ||
src={imageSrc} | ||
></Image> | ||
</div> | ||
<div className="image-demo"> | ||
<p className="image-demo-tip">scale-down</p> | ||
<Image | ||
className="image-container" | ||
style={{ width: '72px', height: '72px' }} | ||
fit="scale-down" | ||
src={imageSrc} | ||
></Image> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import TDemoBlock from '../../../site/mobile/components/DemoBlock'; | ||
import TDemoHeader from '../../../site/mobile/components/DemoHeader'; | ||
import BaseImage from './base'; | ||
import PositionImage from './position'; | ||
import ShapeImage from './shape'; | ||
import StatusImage from './status'; | ||
|
||
export default function ImageDemo() { | ||
return ( | ||
<> | ||
<TDemoHeader title="Image 图片" summary="用于展示图片素材" /> | ||
<TDemoBlock title="01 组件类型" padding={true}> | ||
<BaseImage /> | ||
<PositionImage /> | ||
<ShapeImage /> | ||
</TDemoBlock> | ||
<TDemoBlock title="02 组件状态"> | ||
<StatusImage /> | ||
</TDemoBlock> | ||
</> | ||
); | ||
} |
Oops, something went wrong.