2020-12-07 17:08:48 +08:00
|
|
|
import EyeOutlined from '@ant-design/icons/EyeOutlined';
|
2022-03-22 14:11:30 +08:00
|
|
|
import classNames from 'classnames';
|
2022-11-09 12:28:04 +08:00
|
|
|
import RcImage, { type ImageProps } from 'rc-image';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2020-10-12 10:59:09 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-09 11:47:02 +08:00
|
|
|
import defaultLocale from '../locale/en_US';
|
2021-02-20 22:24:44 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
2022-03-22 14:11:30 +08:00
|
|
|
// CSSINJS
|
2022-06-09 11:47:02 +08:00
|
|
|
import PreviewGroup, { icons } from './PreviewGroup';
|
2022-03-22 14:11:30 +08:00
|
|
|
import useStyle from './style';
|
2020-08-22 22:02:20 +08:00
|
|
|
|
2020-11-30 15:54:54 +08:00
|
|
|
export interface CompositionImage<P> extends React.FC<P> {
|
|
|
|
PreviewGroup: typeof PreviewGroup;
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:08:48 +08:00
|
|
|
const Image: CompositionImage<ImageProps> = ({
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
preview,
|
2022-03-22 14:11:30 +08:00
|
|
|
rootClassName,
|
2020-12-07 17:08:48 +08:00
|
|
|
...otherProps
|
|
|
|
}) => {
|
2022-06-11 13:54:50 +08:00
|
|
|
const {
|
|
|
|
getPrefixCls,
|
|
|
|
locale: contextLocale = defaultLocale,
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
2022-12-06 21:09:59 +08:00
|
|
|
} = React.useContext(ConfigContext);
|
2022-06-11 13:54:50 +08:00
|
|
|
|
2020-08-22 22:02:20 +08:00
|
|
|
const prefixCls = getPrefixCls('image', customizePrefixCls);
|
2021-02-20 22:24:44 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-08-22 22:02:20 +08:00
|
|
|
|
2020-12-07 17:08:48 +08:00
|
|
|
const imageLocale = contextLocale.Image || defaultLocale.Image;
|
2022-03-22 14:11:30 +08:00
|
|
|
// Style
|
2022-04-08 18:05:50 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2020-12-07 17:08:48 +08:00
|
|
|
|
2022-03-22 14:11:30 +08:00
|
|
|
const mergedRootClassName = classNames(rootClassName, hashId);
|
2020-12-07 17:08:48 +08:00
|
|
|
const mergedPreview = React.useMemo(() => {
|
|
|
|
if (preview === false) {
|
|
|
|
return preview;
|
|
|
|
}
|
2021-02-20 22:24:44 +08:00
|
|
|
const _preview = typeof preview === 'object' ? preview : {};
|
2022-06-11 13:54:50 +08:00
|
|
|
const { getContainer, ...restPreviewProps } = _preview;
|
2020-12-07 17:08:48 +08:00
|
|
|
return {
|
|
|
|
mask: (
|
|
|
|
<div className={`${prefixCls}-mask-info`}>
|
|
|
|
<EyeOutlined />
|
|
|
|
{imageLocale?.preview}
|
|
|
|
</div>
|
|
|
|
),
|
2021-01-08 10:14:01 +08:00
|
|
|
icons,
|
2022-06-11 13:54:50 +08:00
|
|
|
...restPreviewProps,
|
|
|
|
getContainer: getContainer || getContextPopupContainer,
|
2021-02-20 22:24:44 +08:00
|
|
|
transitionName: getTransitionName(rootPrefixCls, 'zoom', _preview.transitionName),
|
|
|
|
maskTransitionName: getTransitionName(rootPrefixCls, 'fade', _preview.maskTransitionName),
|
2020-12-07 17:08:48 +08:00
|
|
|
};
|
|
|
|
}, [preview, imageLocale]);
|
|
|
|
|
2022-03-22 14:11:30 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<RcImage
|
|
|
|
prefixCls={`${prefixCls}`}
|
|
|
|
preview={mergedPreview}
|
|
|
|
rootClassName={mergedRootClassName}
|
|
|
|
{...otherProps}
|
|
|
|
/>,
|
|
|
|
);
|
2020-08-22 22:02:20 +08:00
|
|
|
};
|
|
|
|
|
2020-11-18 13:48:30 +08:00
|
|
|
export { ImageProps };
|
|
|
|
|
2020-11-30 15:54:54 +08:00
|
|
|
Image.PreviewGroup = PreviewGroup;
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Image.displayName = 'Image';
|
|
|
|
}
|
|
|
|
|
2020-08-22 22:02:20 +08:00
|
|
|
export default Image;
|