Skip to content

Commit 1e7d44c

Browse files
fix(swiper): 修改初始值时不生效问题 (#3293)
* fix(swiper): 修改初始值时不生效问题 * fix(swiper): 修改初始值时不生效问题 * fix(swiper): 修订图片预览使用 swiper 时的属性值,及类型 * fix: review * fix: lock 文件更新 --------- Co-authored-by: oasis <[email protected]>
1 parent f819320 commit 1e7d44c

File tree

9 files changed

+3173
-3036
lines changed

9 files changed

+3173
-3036
lines changed

packages/nutui-taro-demo/package.json

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,23 @@
6464
"@nutui/touch-emulator": "^1.0.0",
6565
"@nutui/lottie-miniprogram": "^1.0.1",
6666
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.11",
67-
"@tarojs/components": "4.0.12",
68-
"@tarojs/helper": "4.0.12",
69-
"@tarojs/plugin-framework-react": "4.0.12",
70-
"@tarojs/plugin-html": "4.0.12",
71-
"@tarojs/plugin-platform-alipay": "4.0.12",
72-
"@tarojs/plugin-platform-h5": "4.0.12",
73-
"@tarojs/plugin-platform-jd": "4.0.12",
74-
"@tarojs/plugin-platform-qq": "4.0.12",
75-
"@tarojs/plugin-platform-swan": "4.0.12",
76-
"@tarojs/plugin-platform-tt": "4.0.12",
77-
"@tarojs/plugin-platform-weapp": "4.0.12",
78-
"@tarojs/react": "4.0.12",
79-
"@tarojs/router": "4.0.12",
80-
"@tarojs/runtime": "4.0.12",
81-
"@tarojs/shared": "4.0.12",
82-
"@tarojs/taro": "4.0.12",
83-
"@tarojs/taro-h5": "4.0.12",
67+
"@tarojs/components": "4.1.3",
68+
"@tarojs/helper": "4.1.3",
69+
"@tarojs/plugin-framework-react": "4.1.3",
70+
"@tarojs/plugin-html": "4.1.3",
71+
"@tarojs/plugin-platform-alipay": "4.1.3",
72+
"@tarojs/plugin-platform-h5": "4.1.3",
73+
"@tarojs/plugin-platform-jd": "4.1.3",
74+
"@tarojs/plugin-platform-qq": "4.1.3",
75+
"@tarojs/plugin-platform-swan": "4.1.3",
76+
"@tarojs/plugin-platform-tt": "4.1.3",
77+
"@tarojs/plugin-platform-weapp": "4.1.3",
78+
"@tarojs/react": "4.1.3",
79+
"@tarojs/router": "4.1.3",
80+
"@tarojs/runtime": "4.1.3",
81+
"@tarojs/shared": "4.1.3",
82+
"@tarojs/taro": "4.1.3",
83+
"@tarojs/taro-h5": "4.1.3",
8484
"babel-plugin-import": "^1.13.8",
8585
"lodash.isequal": "^4.5.0",
8686
"lodash.kebabcase": "^4.1.1",
@@ -92,10 +92,10 @@
9292
},
9393
"devDependencies": {
9494
"@babel/core": "^7.23.9",
95-
"@tarojs/cli": "4.0.12",
96-
"@tarojs/plugin-platform-harmony-ets": "4.0.12",
97-
"@tarojs/vite-runner": "4.0.12",
98-
"@tarojs/webpack5-runner": "4.0.12",
95+
"@tarojs/cli": "4.1.3",
96+
"@tarojs/plugin-platform-harmony-ets": "4.1.3",
97+
"@tarojs/vite-runner": "4.1.3",
98+
"@tarojs/webpack5-runner": "4.1.3",
9999
"@types/react": "^18.2.57",
100100
"@types/react-dom": "^18.2.19",
101101
"@types/react-router-dom": "^5.3.3",
@@ -105,9 +105,9 @@
105105
"@types/webpack-env": "^1.18.4",
106106
"@typescript-eslint/eslint-plugin": "^7.0.2",
107107
"@typescript-eslint/parser": "^7.0.2",
108-
"babel-preset-taro": "4.0.12",
108+
"babel-preset-taro": "4.1.3",
109109
"eslint": "^8.56.0",
110-
"eslint-config-taro": "4.0.12",
110+
"eslint-config-taro": "4.1.3",
111111
"eslint-plugin-import": "^2.29.1",
112112
"eslint-plugin-react": "^7.33.2",
113113
"eslint-plugin-react-hooks": "^4.6.0",

pnpm-lock.yaml

Lines changed: 3112 additions & 2994 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/packages/imagepreview/demos/taro/demo2.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const Demo2 = () => {
3636
))}
3737
</Cell>
3838
<ImagePreview
39-
autoPlay
4039
images={images}
4140
visible={showPreview}
4241
defaultValue={init}

src/packages/imagepreview/imagepreview.taro.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
PreviewVideoOption,
2323
TaroImagePreviewProps,
2424
} from '@/types'
25+
import { mergeProps } from '@/utils'
2526

2627
interface Store {
2728
scale: number
@@ -46,7 +47,7 @@ const defaultProps = {
4647
showMenuByLongpress: false,
4748
onChange: () => {},
4849
onClose: () => {},
49-
} as TaroImagePreviewProps
50+
} as unknown as TaroImagePreviewProps
5051
export const ImagePreview: FunctionComponent<Partial<TaroImagePreviewProps>> = (
5152
props
5253
) => {
@@ -62,13 +63,15 @@ export const ImagePreview: FunctionComponent<Partial<TaroImagePreviewProps>> = (
6263
pagination,
6364
indicator,
6465
autoPlay,
66+
loop,
6567
closeOnContentClick,
6668
closeIcon,
6769
closeIconPosition,
6870
showMenuByLongpress,
6971
onClose,
7072
onChange,
71-
} = { ...defaultProps, ...props }
73+
...rest
74+
} = mergeProps(defaultProps, props)
7275
const classPrefix = 'nut-imagepreview'
7376
const ref = useRef<HTMLDivElement | null>(null)
7477
const [innerNo, setInnerNo] = usePropsValue<number>({
@@ -223,17 +226,17 @@ export const ImagePreview: FunctionComponent<Partial<TaroImagePreviewProps>> = (
223226
onTouchCancel={onTouchEnd}
224227
>
225228
<Swiper
226-
autoPlay={autoPlay}
229+
autoplay={autoPlay}
230+
loop={loop}
231+
direction="horizontal"
227232
className={`${classPrefix}-swiper`}
228-
loop
229233
height="100%"
230234
style={{
231235
display: showPop ? 'block' : 'none',
232236
'--nutui-indicator-color': indicatorColor,
233237
width: '100%',
234238
height: '100%',
235239
}}
236-
direction="horizontal"
237240
onChange={(e) => slideChangeEnd(e.detail.current)}
238241
defaultValue={innerNo && (innerNo > maxNo ? maxNo - 1 : innerNo - 1)}
239242
indicator={indicator}

src/packages/imagepreview/imagepreview.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
PreviewVideoOption,
2020
WebImagePreviewProps,
2121
} from '@/types'
22+
import { mergeProps } from '@/utils'
2223

2324
interface Store {
2425
scale: number
@@ -42,7 +43,7 @@ const defaultProps = {
4243
closeIconPosition: 'top-right',
4344
onChange: () => {},
4445
onClose: () => {},
45-
} as WebImagePreviewProps
46+
} as unknown as WebImagePreviewProps
4647
export const ImagePreview: FunctionComponent<Partial<WebImagePreviewProps>> = (
4748
props
4849
) => {
@@ -63,7 +64,8 @@ export const ImagePreview: FunctionComponent<Partial<WebImagePreviewProps>> = (
6364
closeIconPosition,
6465
onClose,
6566
onChange,
66-
} = { ...defaultProps, ...props }
67+
...rest
68+
} = mergeProps(defaultProps, props)
6769
const classPrefix = 'nut-imagepreview'
6870
const ref = useRef<HTMLDivElement | null>(null)
6971
const [innerNo, setInnerNo] = usePropsValue<number>({
@@ -218,17 +220,15 @@ export const ImagePreview: FunctionComponent<Partial<WebImagePreviewProps>> = (
218220
>
219221
{showPop && (
220222
<Swiper
221-
autoPlay={!!duration}
222-
duration={duration}
223+
autoplay={!!duration}
223224
className={`${classPrefix}-swiper`}
224-
loop
225225
style={{
226226
'--nutui-indicator-color': indicatorColor,
227227
}}
228-
direction="horizontal"
229228
onChange={(page) => slideChangeEnd(page)}
230229
defaultValue={innerNo > maxNo ? maxNo - 1 : innerNo - 1}
231230
indicator={indicator}
231+
{...rest}
232232
>
233233
{[
234234
...videos.map((item) => ({ type: 'video', data: item })),

src/packages/swiper/index.taro.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { TaroSwiperProps } from '@/types'
55

66
export type { TaroSwiperProps as SwiperProps } from '@/types'
77
type CompoundedComponent = React.ForwardRefExoticComponent<
8-
Partial<TaroSwiperProps> &
9-
Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> &
10-
React.RefAttributes<any>
8+
Partial<TaroSwiperProps> & React.RefAttributes<any>
119
> & {
1210
Item: typeof SwiperItem
1311
}

src/packages/swiper/swiper.taro.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { Children, useImperativeHandle, useMemo, useState } from 'react'
1+
import React, {
2+
Children,
3+
useImperativeHandle,
4+
useMemo,
5+
useState,
6+
useEffect,
7+
} from 'react'
28
import {
39
Swiper as TaroSwiper,
410
SwiperItem as TSwiperItem,
@@ -47,6 +53,7 @@ export const Swiper = React.forwardRef(
4753
...defaultProps,
4854
...props,
4955
}
56+
5057
const [innerValue, setInnerValue] = useState(current || defaultValue)
5158
const childrenCount = useMemo(() => {
5259
let c = 0
@@ -55,6 +62,11 @@ export const Swiper = React.forwardRef(
5562
})
5663
return c
5764
}, [children])
65+
66+
useEffect(() => {
67+
setInnerValue(current || defaultValue)
68+
}, [defaultValue, current])
69+
5870
const renderIndicator = () => {
5971
if (React.isValidElement(indicator)) return indicator
6072
if (indicator || indicatorDots) {
@@ -76,11 +88,13 @@ export const Swiper = React.forwardRef(
7688
}
7789
return null
7890
}
91+
7992
const handleOnChange: CommonEventFunction<
8093
TSwiperProps.onChangeEventDetail
8194
> = (value) => {
8295
setInnerValue(value.detail.current)
8396
}
97+
8498
useImperativeHandle(ref, () => ({
8599
to: (value: number) => {
86100
setInnerValue(value)
@@ -104,6 +118,7 @@ export const Swiper = React.forwardRef(
104118
}
105119
},
106120
}))
121+
107122
return (
108123
<View
109124
className={classNames(classPrefix, className)}

src/types/spec/imagepreview/h5.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { BaseImagePreview } from './base'
2+
import { WebSwiperProps } from '@/types'
23

3-
export interface WebImagePreviewProps extends BaseImagePreview {}
4+
type props = BaseImagePreview & Omit<WebSwiperProps, 'autoPlay' | 'indicator'>
5+
export interface WebImagePreviewProps extends Omit<props, 'autoplay'> {}

src/types/spec/imagepreview/taro.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { BaseImagePreview } from './base'
2+
import { TaroSwiperProps } from '@/types'
23

3-
export interface TaroImagePreviewProps
4-
extends Omit<BaseImagePreview, 'autoPlay'> {
4+
type props = Omit<BaseImagePreview, 'autoPlay'> &
5+
Omit<TaroSwiperProps, 'autoPlay' | 'autoplay' | 'indicator'>
6+
export interface TaroImagePreviewProps extends props {
57
autoPlay: boolean
68
showMenuByLongpress: boolean
79
}

0 commit comments

Comments
 (0)