Skip to content

Commit 65f20d5

Browse files
authored
feat: imagepreview 适配 taro (jdf2e#326)
* chore: 优化样式引入 imagepreview * feat: imagepreview 适配 taro * fix: 保留 useReady hook
1 parent cfaba7c commit 65f20d5

File tree

8 files changed

+46
-78
lines changed

8 files changed

+46
-78
lines changed

src/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@
956956
"desc": "图片预览",
957957
"sort": 23,
958958
"show": true,
959+
"taro": true,
959960
"author": "yangjinjun3"
960961
}
961962
]

src/packages/imagepreview/demo.taro.tsx

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,6 @@ const images = [
2525
},
2626
]
2727

28-
const videos = [
29-
{
30-
source: {
31-
src: 'https://storage.jd.com/about/big-final.mp4?Expires=3730193075&AccessKey=3LoYX1dQWa6ZXzQl&Signature=ViMFjz%2BOkBxS%2FY1rjtUVqbopbJI%3D',
32-
type: 'video/mp4',
33-
},
34-
options: {
35-
muted: true,
36-
controls: true,
37-
},
38-
},
39-
{
40-
source: {
41-
src: 'https://storage.jd.com/about/big-final.mp4?Expires=3730193075&AccessKey=3LoYX1dQWa6ZXzQl&Signature=ViMFjz%2BOkBxS%2FY1rjtUVqbopbJI%3D',
42-
type: 'video/mp4',
43-
},
44-
options: {
45-
muted: true,
46-
controls: true,
47-
},
48-
},
49-
]
50-
5128
const ImagePreviewDemo = () => {
5229
const [translated] = useTranslate<T>({
5330
'zh-CN': {
@@ -69,7 +46,6 @@ const ImagePreviewDemo = () => {
6946
const [showPreview1, setShowPreview1] = useState(false)
7047
const [showPreview2, setShowPreview2] = useState(false)
7148
const [showPreview3, setShowPreview3] = useState(false)
72-
const [showPreview4, setShowPreview4] = useState(false)
7349

7450
const showFn1 = () => {
7551
setShowPreview1(true)
@@ -83,10 +59,6 @@ const ImagePreviewDemo = () => {
8359
setShowPreview3(true)
8460
}
8561

86-
const showFn4 = () => {
87-
setShowPreview4(true)
88-
}
89-
9062
const hideFn1 = () => {
9163
setShowPreview1(false)
9264
}
@@ -99,10 +71,6 @@ const ImagePreviewDemo = () => {
9971
setShowPreview3(false)
10072
}
10173

102-
const hideFn4 = () => {
103-
setShowPreview4(false)
104-
}
105-
10674
return (
10775
<>
10876
<div className="demo">
@@ -126,14 +94,6 @@ const ImagePreviewDemo = () => {
12694
onClose={hideFn3}
12795
/>
12896
<Cell title={translated.withPagination} isLink onClick={showFn3} />
129-
<h2>{translated.withVideos}</h2>
130-
<ImagePreview
131-
images={images}
132-
videos={videos}
133-
show={showPreview4}
134-
onClose={hideFn4}
135-
/>
136-
<Cell title={translated.withVideos} isLink onClick={showFn4} />
13797
</div>
13898
</>
13999
)

src/packages/imagepreview/imagepreview.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
@import '../popup/popup.scss';
2+
@import '../swiper/swiper.scss';
3+
@import '../video/video.scss';
14
.nut-imagepreview {
25
width: 100%;
36

src/packages/imagepreview/imagepreview.taro.tsx

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import React, {
77
TouchEvent,
88
} from 'react'
99
import Popup from '@/packages/popup/index.taro'
10-
import Video from '@/packages/video/index.taro'
1110
import Swiper from '@/packages/swiper/index.taro'
1211
import SwiperItem from '@/packages/swiperitem/index.taro'
1312

@@ -257,24 +256,19 @@ export const ImagePreview: FunctionComponent<Partial<ImagePreviewProps>> = (
257256
paginationColor={paginationColor}
258257
paginationVisible={paginationVisible}
259258
>
260-
{videos &&
261-
videos.length > 0 &&
262-
videos.map((item, index) => {
263-
return (
264-
<SwiperItem key={index}>
265-
<Video source={item.source} options={item.options} />
266-
</SwiperItem>
267-
)
268-
})}
269-
{images &&
270-
images.length > 0 &&
271-
images.map((item, index) => {
272-
return (
273-
<SwiperItem key={index}>
274-
<img src={item.src} alt="" className="nut-imagepreview-img" />
275-
</SwiperItem>
276-
)
277-
})}
259+
{images && images.length > 0
260+
? images.map((item, index) => {
261+
return (
262+
<SwiperItem key={index}>
263+
<img
264+
src={item.src}
265+
alt=""
266+
className="nut-imagepreview-img"
267+
/>
268+
</SwiperItem>
269+
)
270+
})
271+
: []}
278272
</Swiper>
279273
</div>
280274
<div className="nut-imagepreview-index">

src/packages/imagepreview/imagepreview.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,30 @@ export const ImagePreview: FunctionComponent<Partial<ImagePreviewProps>> = (
257257
paginationColor={paginationColor}
258258
paginationVisible={paginationVisible}
259259
>
260-
{videos &&
261-
videos.length > 0 &&
262-
videos.map((item, index) => {
263-
return (
264-
<SwiperItem key={index}>
265-
<Video source={item.source} options={item.options} />
266-
</SwiperItem>
267-
)
268-
})}
269-
{images &&
270-
images.length > 0 &&
271-
images.map((item, index) => {
272-
return (
273-
<SwiperItem key={index}>
274-
<img src={item.src} alt="" className="nut-imagepreview-img" />
275-
</SwiperItem>
276-
)
277-
})}
260+
{(videos && videos.length > 0
261+
? videos.map((item, index) => {
262+
return (
263+
<SwiperItem key={index}>
264+
<Video source={item.source} options={item.options} />
265+
</SwiperItem>
266+
)
267+
})
268+
: []
269+
).concat(
270+
images && images.length > 0
271+
? images.map((item, index) => {
272+
return (
273+
<SwiperItem key={index}>
274+
<img
275+
src={item.src}
276+
alt=""
277+
className="nut-imagepreview-img"
278+
/>
279+
</SwiperItem>
280+
)
281+
})
282+
: []
283+
)}
278284
</Swiper>
279285
</div>
280286
<div className="nut-imagepreview-index">

src/packages/swiper/swiper.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import '../swiperitem/swiperitem.scss';
12
.nut-swiper {
23
position: relative;
34
z-index: 1;

src/packages/swiper/swiper.taro.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ export const Swiper = React.forwardRef<
458458
autoplay()
459459
}, [children])
460460
useEffect(() => {
461-
init()
461+
nextTick(() => {
462+
init()
463+
})
462464
}, [propSwiper.initPage])
463465
useEffect(() => {
464466
return () => {

src/sites/mobile-taro/src/app.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const subPackages = [
7474
'pages/progress/index',
7575
'pages/audio/index',
7676
'pages/animatingnumbers/index',
77+
'pages/imagepreview/index',
7778
],
7879
},
7980
{

0 commit comments

Comments
 (0)