diff --git a/README.md b/README.md index 273ae3d1..ab7a64fe 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,13 @@ ReactDOM.render( ), document.getElementById('root')); ``` +### API + +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| preview | boolean \| {visible: boolean,onVisibleChange:function(value, prevValue),getContainer: string \| HTMLElement \| (() => HTMLElement) \| false } | true | Whether to show preview | +| current | number | 0 | If Preview the show img index | + ## Example http://localhost:8003/examples/ diff --git a/examples/controlledWithGroup.tsx b/examples/controlledWithGroup.tsx new file mode 100644 index 00000000..b2e3b4a5 --- /dev/null +++ b/examples/controlledWithGroup.tsx @@ -0,0 +1,37 @@ +/* eslint-disable global-require */ +import * as React from 'react'; +import Image from '../src'; +import '../assets/index.less'; + +export default function Base() { + const [visible, setVisible] = React.useState(false); + return ( +
+
+ +
+ { + setVisible(value); + }, + current: 1, + }} + > + + + +
+ ); +} diff --git a/src/PreviewGroup.tsx b/src/PreviewGroup.tsx index 6da69b67..842c4531 100644 --- a/src/PreviewGroup.tsx +++ b/src/PreviewGroup.tsx @@ -1,10 +1,22 @@ import * as React from 'react'; import { useState } from 'react'; +import useMergedState from 'rc-util/lib/hooks/useMergedState'; +import { ImagePreviewType } from './Image'; import Preview, { PreviewProps } from './Preview'; +export interface PreviewGroupPreview + extends Pick { + /** + * If Preview the show img index + * @default 0 + */ + current?: number; +} + export interface GroupConsumerProps { previewPrefixCls?: string; icons?: PreviewProps['icons']; + preview?: boolean | PreviewGroupPreview; } export interface GroupConsumerValue extends GroupConsumerProps { @@ -35,11 +47,24 @@ const Group: React.FC = ({ previewPrefixCls = 'rc-image-preview', children, icons = {}, + preview, }) => { + const { + visible: previewVisible = undefined, + onVisibleChange: onPreviewVisibleChange = undefined, + getContainer = undefined, + current: currentIndex = 0, + } = typeof preview === 'object' ? preview : {}; const [previewUrls, setPreviewUrls] = useState>(new Map()); const [current, setCurrent] = useState(); - const [isShowPreview, setShowPreview] = useState(false); + const [isShowPreview, setShowPreview] = useMergedState(!!previewVisible, { + value: previewVisible, + onChange: onPreviewVisibleChange, + }); const [mousePosition, setMousePosition] = useState(null); + const isControlled = previewVisible !== undefined; + const previewUrlsKeys = Array.from(previewUrls.keys()); + const currentControlledKey = previewUrlsKeys[currentIndex]; const registerImage = (id: number, url: string) => { const unRegister = () => { @@ -69,6 +94,16 @@ const Group: React.FC = ({ setMousePosition(null); }; + React.useEffect(() => { + setCurrent(currentControlledKey); + }, [currentControlledKey]); + + React.useEffect(() => { + if (!isShowPreview && isControlled) { + setCurrent(currentControlledKey); + } + }, [currentControlledKey, isControlled, isShowPreview]); + return ( = ({ mousePosition={mousePosition} src={previewUrls.get(current)} icons={icons} + getContainer={getContainer} /> ); diff --git a/tests/__snapshots__/previewGroup.test.tsx.snap b/tests/__snapshots__/previewGroup.test.tsx.snap new file mode 100644 index 00000000..088251df --- /dev/null +++ b/tests/__snapshots__/previewGroup.test.tsx.snap @@ -0,0 +1,56 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Preview With Controlled 1`] = ` +
+