Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface ImagePreviewType
) => React.ReactNode;
}

export type SemanticName = 'actions' | 'mask';

export interface ImageProps
extends Omit<React.ImgHTMLAttributes<HTMLImageElement>, 'placeholder' | 'onClick'> {
// Original
Expand All @@ -67,6 +69,8 @@ export interface ImageProps
onPreviewClose?: (value: boolean, prevValue: boolean) => void;
onClick?: (e: React.MouseEvent<HTMLDivElement>) => void;
onError?: (e: React.SyntheticEvent<HTMLImageElement, Event>) => void;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
}

interface CompoundedComponent<P> extends React.FC<P> {
Expand All @@ -92,7 +96,8 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
wrapperClassName,
wrapperStyle,
rootClassName,

classNames: imageClassNames,
styles,
...otherProps
} = props;

Expand Down Expand Up @@ -222,9 +227,10 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
{/* Preview Click Mask */}
{previewMask && canPreview && (
<div
className={cn(`${prefixCls}-mask`, maskClassName)}
className={cn(`${prefixCls}-mask`, maskClassName, imageClassNames?.mask)}
style={{
display: style?.display === 'none' ? 'none' : undefined,
...styles?.mask,
}}
>
{previewMask}
Expand Down Expand Up @@ -252,6 +258,8 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
imageRender={imageRender}
imgCommonProps={imgCommonProps}
toolbarRender={toolbarRender}
classNames={imageClassNames}
styles={styles}
{...dialogProps}
/>
)}
Expand Down
12 changes: 10 additions & 2 deletions src/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import CSSMotion from 'rc-motion';
import KeyCode from 'rc-util/lib/KeyCode';
import * as React from 'react';
import { useContext } from 'react';
import type { ImgInfo } from './Image';
import type { ImgInfo, SemanticName } from './Image';
import type { PreviewProps, ToolbarRenderInfoType } from './Preview';
import { PreviewGroupContext } from './context';
import type { TransformType } from './hooks/useImageTransform';
import classNames from 'classnames';

type OperationType =
| 'prev'
Expand Down Expand Up @@ -61,6 +62,8 @@ interface OperationsProps
) => React.ReactNode;
zIndex?: number;
image?: ImgInfo;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
}

const Operations: React.FC<OperationsProps> = props => {
Expand Down Expand Up @@ -93,6 +96,8 @@ const Operations: React.FC<OperationsProps> = props => {
toolbarRender,
zIndex,
image,
classNames: imageClassNames,
styles,
} = props;
const groupContext = useContext(PreviewGroupContext);
const { rotateLeft, rotateRight, zoomIn, zoomOut, close, left, right, flipX, flipY } = icons;
Expand Down Expand Up @@ -195,7 +200,10 @@ const Operations: React.FC<OperationsProps> = props => {
});

const toolbarNode = (
<div className={`${prefixCls}-operations`}>
<div
className={classNames(`${prefixCls}-operations`, imageClassNames?.actions)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

诶,actions 原来是 Preview 的?我还以为指的是 Image hover 是那个 👁 预览 ……

style={styles?.actions}
>
{flipYNode}
{flipXNode}
{rotateLeftNode}
Expand Down
4 changes: 4 additions & 0 deletions src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ const Preview: React.FC<PreviewProps> = props => {
toolbarRender,
onTransform,
onChange,
classNames: imageClassNames,
styles,
Comment on lines +137 to +138
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

完善 PreviewProps 接口的类型定义

当前的类型定义不够严格,建议使用与 Image 组件相同的类型定义。

-  classNames: imageClassNames,
-  styles,
+  classNames?: Partial<Record<SemanticName, string>>;
+  styles?: Partial<Record<SemanticName, React.CSSProperties>>;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
classNames: imageClassNames,
styles,
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;

...restProps
} = props;

Expand Down Expand Up @@ -339,6 +341,8 @@ const Preview: React.FC<PreviewProps> = props => {
onReset={onReset}
zIndex={restProps.zIndex !== undefined ? restProps.zIndex + 1 : undefined}
image={image}
classNames={imageClassNames}
styles={styles}
/>
</>
);
Expand Down
19 changes: 19 additions & 0 deletions tests/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,23 @@ describe('Basic', () => {
const operationsElement = baseElement.querySelector('.rc-image-preview-operations-wrapper');
expect(operationsElement).toHaveStyle({ zIndex: 10000 });
});
it('support classnames and styles', () => {
const customClassnames = {
actions: 'custom-actions',
};
const customStyles = {
actions: { backgroundColor: 'blue' },
};
const { baseElement } = render(
<Image
styles={customStyles}
classNames={customClassnames}
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
preview={{ zIndex: 9999, visible: true }}
/>,
);
const actionsElement = baseElement.querySelector('.rc-image-preview-operations');
expect(actionsElement).toHaveClass('custom-actions');
expect(actionsElement).toHaveStyle({ backgroundColor: 'blue' });
});
});
20 changes: 20 additions & 0 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1030,4 +1030,24 @@ describe('Preview', () => {
expect(previewImg).not.toHaveAttribute('width');
expect(previewImg).not.toHaveAttribute('height');
});
it('support classnames and styles', () => {
const customClassnames = {
mask: 'custom-mask',
};
const customStyles = {
mask: { color: 'red' },
};
render(
<Image
styles={customStyles}
classNames={customClassnames}
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
preview={{
mask: 'Bamboo Is Light',
}}
/>,
);
expect(document.querySelector('.rc-image-mask')).toHaveClass('custom-mask');
expect(document.querySelector('.rc-image-mask')).toHaveStyle({ color: 'red' });
});
});