File size: 3,963 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import * as React from 'react';
import EyeOutlined from '@ant-design/icons/EyeOutlined';
import classNames from 'classnames';
import RcImage from 'rc-image';
import type { ImagePreviewType, ImageProps as RcImageProps } from 'rc-image';
import { useZIndex } from '../_util/hooks/useZIndex';
import { getTransitionName } from '../_util/motion';
import { devUseWarning } from '../_util/warning';
import { useComponentConfig } from '../config-provider/context';
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
import { useLocale } from '../locale';
import PreviewGroup, { icons } from './PreviewGroup';
import useStyle from './style';
export interface CompositionImage<P> extends React.FC<P> {
PreviewGroup: typeof PreviewGroup;
}
type Replace<T, K extends keyof T, V> = Partial<Omit<T, K> & { [P in K]: V }>;
interface PreviewType extends Omit<ImagePreviewType, 'destroyOnClose'> {
/** @deprecated Please use destroyOnHidden instead */
destroyOnClose?: boolean;
/**
* @since 5.25.0
*/
destroyOnHidden?: boolean;
}
type ImageProps = Replace<RcImageProps, 'preview', boolean | PreviewType>;
const Image: CompositionImage<ImageProps> = (props) => {
const {
prefixCls: customizePrefixCls,
preview,
className,
rootClassName,
style,
...otherProps
} = props;
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('Image');
warning.deprecated(
!(preview && typeof preview === 'object' && 'destroyOnClose' in preview),
'destroyOnClose',
'destroyOnHidden',
);
}
const {
getPrefixCls,
getPopupContainer: getContextPopupContainer,
className: contextClassName,
style: contextStyle,
preview: contextPreview,
} = useComponentConfig('image');
const [imageLocale] = useLocale('Image');
const prefixCls = getPrefixCls('image', customizePrefixCls);
const rootPrefixCls = getPrefixCls();
// Style
const rootCls = useCSSVarCls(prefixCls);
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
const mergedRootClassName = classNames(rootClassName, hashId, cssVarCls, rootCls);
const mergedClassName = classNames(className, hashId, contextClassName);
const [zIndex] = useZIndex(
'ImagePreview',
typeof preview === 'object' ? preview.zIndex : undefined,
);
const mergedPreview = React.useMemo<RcImageProps['preview']>(() => {
if (preview === false) {
return preview;
}
const _preview = typeof preview === 'object' ? preview : {};
const {
getContainer,
closeIcon,
rootClassName,
destroyOnClose,
destroyOnHidden,
...restPreviewProps
} = _preview;
return {
mask: (
<div className={`${prefixCls}-mask-info`}>
<EyeOutlined />
{imageLocale?.preview}
</div>
),
icons,
...restPreviewProps,
// TODO: In the future, destroyOnClose in rc-image needs to be upgrade to destroyOnHidden
destroyOnClose: destroyOnHidden ?? destroyOnClose,
rootClassName: classNames(mergedRootClassName, rootClassName),
getContainer: getContainer ?? getContextPopupContainer,
transitionName: getTransitionName(rootPrefixCls, 'zoom', _preview.transitionName),
maskTransitionName: getTransitionName(rootPrefixCls, 'fade', _preview.maskTransitionName),
zIndex,
closeIcon: closeIcon ?? contextPreview?.closeIcon,
};
}, [preview, imageLocale, contextPreview?.closeIcon]);
const mergedStyle: React.CSSProperties = { ...contextStyle, ...style };
return wrapCSSVar(
<RcImage
prefixCls={prefixCls}
preview={mergedPreview}
rootClassName={mergedRootClassName}
className={mergedClassName}
style={mergedStyle}
{...otherProps}
/>,
);
};
export type { ImageProps };
Image.PreviewGroup = PreviewGroup;
if (process.env.NODE_ENV !== 'production') {
Image.displayName = 'Image';
}
export default Image;
|