From d2923672fc47114da3c007eb4675ca7f80684ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AA=97=E4=BD=A0=E6=98=AF=E5=B0=8F=E7=8C=AB=E5=92=AA?= Date: Mon, 1 Feb 2021 14:59:48 +0800 Subject: [PATCH] feat: support PreviewGroup controlled (#65) * feat: support PreviewGroup controlled * improve if preview re show * rename defaultShowIndex to defaultCurrent and add doc * rename defaultCurrent to current * use useMemo hook to cache previewUrlsKeys * improve calc current key * remove useMemo --- README.md | 7 +++ examples/controlledWithGroup.tsx | 37 ++++++++++++ src/PreviewGroup.tsx | 38 ++++++++++++- .../__snapshots__/previewGroup.test.tsx.snap | 56 +++++++++++++++++++ tests/previewGroup.test.tsx | 23 ++++++++ 5 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 examples/controlledWithGroup.tsx create mode 100644 tests/__snapshots__/previewGroup.test.tsx.snap 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`] = ` +
+