Skip to content

Commit

Permalink
feat: reset preview prop to Dialog (#76)
Browse files Browse the repository at this point in the history
* feat: reset preview prop to Dialog

* code style

* add omit prop
  • Loading branch information
shaodahong authored Feb 20, 2021
1 parent 2854949 commit b904611
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import useMergedState from 'rc-util/lib/hooks/useMergedState';
import { GetContainer } from 'rc-util/lib/PortalWrapper';
import Preview, { PreviewProps } from './Preview';
import PreviewGroup, { context } from './PreviewGroup';
import { IDialogPropTypes } from 'rc-dialog/lib/IDialogPropTypes';

export interface ImagePreviewType {
export interface ImagePreviewType
extends Omit<
IDialogPropTypes,
'mask' | 'visible' | 'closable' | 'prefixCls' | 'onClose' | 'afterClose' | 'wrapClassName'
> {
src?: string;
visible?: boolean;
onVisibleChange?: (value: boolean, prevValue: boolean) => void;
Expand Down Expand Up @@ -81,6 +86,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = ({
mask: previewMask,
maskClassName,
icons,
...dialogProps
}: ImagePreviewType = typeof preview === 'object' ? preview : {};
const src = previewSrc ?? imgSrc;
const isControlled = previewVisible !== undefined;
Expand Down Expand Up @@ -255,6 +261,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = ({
alt={alt}
getContainer={getPreviewContainer}
icons={icons}
{...dialogProps}
/>
)}
</>
Expand Down
2 changes: 1 addition & 1 deletion src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ const Preview: React.FC<PreviewProps> = props => {

return (
<Dialog
{...restProps}
transitionName="zoom"
maskTransitionName="fade"
closable={false}
Expand All @@ -244,6 +243,7 @@ const Preview: React.FC<PreviewProps> = props => {
afterClose={onAfterClose}
visible={visible}
wrapClassName={wrapClassName}
{...restProps}
>
<ul className={`${prefixCls}-operations`}>
{tools.map(({ icon, onClick, type, disabled }) => (
Expand Down
4 changes: 3 additions & 1 deletion src/PreviewGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ImagePreviewType } from './Image';
import Preview, { PreviewProps } from './Preview';

export interface PreviewGroupPreview
extends Pick<ImagePreviewType, 'visible' | 'onVisibleChange' | 'getContainer'> {
extends Omit<ImagePreviewType, 'icons' | 'mask' | 'maskClassName'> {
/**
* If Preview the show img index
* @default 0
Expand Down Expand Up @@ -54,6 +54,7 @@ const Group: React.FC<GroupConsumerProps> = ({
onVisibleChange: onPreviewVisibleChange = undefined,
getContainer = undefined,
current: currentIndex = 0,
...dialogProps
} = typeof preview === 'object' ? preview : {};
const [previewUrls, setPreviewUrls] = useState<Map<number, string>>(new Map());
const [current, setCurrent] = useState<number>();
Expand Down Expand Up @@ -127,6 +128,7 @@ const Group: React.FC<GroupConsumerProps> = ({
src={previewUrls.get(current)}
icons={icons}
getContainer={getContainer}
{...dialogProps}
/>
</Provider>
);
Expand Down
31 changes: 31 additions & 0 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,35 @@ describe('Preview', () => {
expect(wrapper.find('.rc-image-preview').get(0)).toBeTruthy();
expect(wrapper.find('.rc-image-preview-img').prop('src')).toBe(previewSrc);
});

it('Customize preview props', () => {
const src = 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png';
const wrapper = mount(
<Image
src={src}
preview={{
visible: true,
transitionName: 'abc',
maskTransitionName: 'def',
}}
/>,
);

expect(wrapper.find('Preview').prop('transitionName')).toBe('abc');
expect(wrapper.find('Preview').prop('maskTransitionName')).toBe('def');
});

it('Customize Group preview props', () => {
const src = 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png';
const wrapper = mount(
<Image.PreviewGroup
preview={{ visible: true, transitionName: 'abc', maskTransitionName: 'def' }}
>
<Image src={src} />
</Image.PreviewGroup>,
);

expect(wrapper.find('Preview').prop('transitionName')).toBe('abc');
expect(wrapper.find('Preview').prop('maskTransitionName')).toBe('def');
});
});

1 comment on commit b904611

@vercel
Copy link

@vercel vercel bot commented on b904611 Feb 20, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.