@mpxjs/webpack-plugin 2.9.62 → 2.9.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/lib/index.js +1 -3
  2. package/lib/platform/style/wx/index.js +67 -53
  3. package/lib/react/processStyles.js +1 -0
  4. package/lib/react/processTemplate.js +2 -3
  5. package/lib/react/style-helper.js +12 -7
  6. package/lib/runtime/components/react/context.ts +9 -7
  7. package/lib/runtime/components/react/dist/context.js +1 -0
  8. package/lib/runtime/components/react/dist/getInnerListeners.js +12 -1
  9. package/lib/runtime/components/react/dist/mpx-button.jsx +52 -74
  10. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +19 -18
  11. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +28 -41
  12. package/lib/runtime/components/react/dist/mpx-form.jsx +16 -14
  13. package/lib/runtime/components/react/dist/mpx-icon.jsx +14 -17
  14. package/lib/runtime/components/react/dist/mpx-image/index.jsx +34 -33
  15. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +3 -1
  16. package/lib/runtime/components/react/dist/mpx-input.jsx +35 -31
  17. package/lib/runtime/components/react/dist/mpx-label.jsx +29 -37
  18. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +13 -18
  19. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +8 -8
  20. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +9 -9
  21. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +7 -4
  22. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +11 -7
  23. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +1 -1
  24. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +18 -18
  25. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +102 -10
  26. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +147 -53
  27. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +19 -18
  28. package/lib/runtime/components/react/dist/mpx-radio.jsx +28 -43
  29. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +8 -4
  30. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +33 -26
  31. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +139 -74
  32. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +14 -6
  33. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +19 -11
  34. package/lib/runtime/components/react/dist/mpx-switch.jsx +17 -14
  35. package/lib/runtime/components/react/dist/mpx-text.jsx +19 -35
  36. package/lib/runtime/components/react/dist/mpx-textarea.jsx +1 -1
  37. package/lib/runtime/components/react/dist/mpx-view.jsx +284 -209
  38. package/lib/runtime/components/react/dist/mpx-web-view.jsx +8 -5
  39. package/lib/runtime/components/react/dist/parser.js +218 -0
  40. package/lib/runtime/components/react/dist/utils.jsx +433 -0
  41. package/lib/runtime/components/react/getInnerListeners.ts +18 -8
  42. package/lib/runtime/components/react/mpx-button.tsx +81 -91
  43. package/lib/runtime/components/react/mpx-checkbox-group.tsx +48 -43
  44. package/lib/runtime/components/react/mpx-checkbox.tsx +52 -63
  45. package/lib/runtime/components/react/mpx-form.tsx +49 -21
  46. package/lib/runtime/components/react/mpx-icon.tsx +30 -27
  47. package/lib/runtime/components/react/mpx-image/index.tsx +52 -46
  48. package/lib/runtime/components/react/mpx-image/svg.tsx +5 -3
  49. package/lib/runtime/components/react/mpx-input.tsx +58 -38
  50. package/lib/runtime/components/react/mpx-label.tsx +54 -59
  51. package/lib/runtime/components/react/mpx-movable-area.tsx +38 -24
  52. package/lib/runtime/components/react/mpx-movable-view.tsx +27 -28
  53. package/lib/runtime/components/react/mpx-navigator.tsx +2 -2
  54. package/lib/runtime/components/react/mpx-picker/date.tsx +2 -3
  55. package/lib/runtime/components/react/mpx-picker/index.tsx +10 -10
  56. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +15 -12
  57. package/lib/runtime/components/react/mpx-picker/region.tsx +21 -18
  58. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -6
  59. package/lib/runtime/components/react/mpx-picker/time.tsx +25 -29
  60. package/lib/runtime/components/react/mpx-picker/type.ts +1 -1
  61. package/lib/runtime/components/react/mpx-picker-view-column.tsx +148 -20
  62. package/lib/runtime/components/react/mpx-picker-view.tsx +179 -63
  63. package/lib/runtime/components/react/mpx-radio-group.tsx +50 -47
  64. package/lib/runtime/components/react/mpx-radio.tsx +56 -72
  65. package/lib/runtime/components/react/mpx-root-portal.tsx +10 -8
  66. package/lib/runtime/components/react/mpx-scroll-view.tsx +133 -103
  67. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +174 -96
  68. package/lib/runtime/components/react/mpx-swiper/index.tsx +18 -9
  69. package/lib/runtime/components/react/mpx-swiper/type.ts +16 -5
  70. package/lib/runtime/components/react/mpx-swiper-item.tsx +46 -13
  71. package/lib/runtime/components/react/mpx-switch.tsx +44 -23
  72. package/lib/runtime/components/react/mpx-text.tsx +37 -45
  73. package/lib/runtime/components/react/mpx-textarea.tsx +1 -1
  74. package/lib/runtime/components/react/mpx-view.tsx +388 -240
  75. package/lib/runtime/components/react/mpx-web-view.tsx +19 -20
  76. package/lib/runtime/components/react/parser.ts +245 -0
  77. package/lib/runtime/components/react/types/common.ts +4 -4
  78. package/lib/runtime/components/react/types/global.d.ts +14 -2
  79. package/lib/runtime/components/react/useNodesRef.ts +1 -2
  80. package/lib/runtime/components/react/utils.tsx +505 -0
  81. package/lib/template-compiler/compiler.js +28 -20
  82. package/lib/template-compiler/gen-node-react.js +1 -3
  83. package/lib/web/processStyles.js +2 -5
  84. package/package.json +6 -4
  85. package/lib/runtime/components/react/dist/utils.js +0 -148
  86. package/lib/runtime/components/react/utils.ts +0 -170
@@ -1,11 +1,12 @@
1
1
  /**
2
2
  * swiper 实现
3
3
  */
4
- import { Animated, View, ScrollView, Dimensions, NativeSyntheticEvent, NativeScrollEvent, NativeScrollPoint, Platform } from 'react-native'
4
+ import { Animated, View, ScrollView, Dimensions, NativeSyntheticEvent, NativeScrollEvent, NativeScrollPoint, Platform, LayoutChangeEvent } from 'react-native'
5
5
  import { JSX, forwardRef, useState, useRef, useEffect, ReactNode } from 'react'
6
6
  import { CarouseProps, CarouseState } from './type'
7
7
  import { getCustomEvent } from '../getInnerListeners'
8
8
  import useNodesRef, { HandlerRef } from '../useNodesRef' // 引入辅助函数
9
+ import { useTransformStyle, splitStyle, splitProps, useLayout, wrapChildren } from '../utils'
9
10
 
10
11
  /**
11
12
  * 默认的Style类型
@@ -15,10 +16,10 @@ const styles: { [key: string]: Object } = {
15
16
  backgroundColor: 'transparent'
16
17
  },
17
18
  container_x: {
18
- position: 'relative',
19
+ position: 'relative'
19
20
  },
20
21
  container_y: {
21
- position: 'relative',
22
+ position: 'relative'
22
23
  },
23
24
  pagination_x: {
24
25
  position: 'absolute',
@@ -28,7 +29,7 @@ const styles: { [key: string]: Object } = {
28
29
  flexDirection: 'row',
29
30
  flex: 1,
30
31
  justifyContent: 'center',
31
- alignItems: 'center',
32
+ alignItems: 'center'
32
33
  },
33
34
 
34
35
  pagination_y: {
@@ -39,21 +40,44 @@ const styles: { [key: string]: Object } = {
39
40
  flexDirection: 'column',
40
41
  flex: 1,
41
42
  justifyContent: 'center',
42
- alignItems: 'center',
43
+ alignItems: 'center'
43
44
  }
44
45
  }
45
46
 
46
-
47
- const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>((props , ref): JSX.Element => {
47
+ const _Carouse = forwardRef<HandlerRef<ScrollView & View, CarouseProps>, CarouseProps>((props, ref): JSX.Element => {
48
48
  // 默认取水平方向的width
49
49
  const { width } = Dimensions.get('window')
50
- const { styleObj } = props
50
+ const {
51
+ style,
52
+ previousMargin = 0,
53
+ nextMargin = 0,
54
+ enableVar,
55
+ externalVarContext,
56
+ parentFontSize,
57
+ parentWidth,
58
+ parentHeight
59
+ } = props
60
+ // 计算transfrom之类的
61
+ const {
62
+ normalStyle,
63
+ hasVarDec,
64
+ varContextRef,
65
+ hasSelfPercent,
66
+ setWidth,
67
+ setHeight
68
+ } = useTransformStyle(style, {
69
+ enableVar,
70
+ externalVarContext,
71
+ parentFontSize,
72
+ parentWidth,
73
+ parentHeight
74
+ })
75
+ const { textStyle, innerStyle } = splitStyle(normalStyle)
76
+ const { textProps } = splitProps(props)
51
77
  const newChild = Array.isArray(props.children) ? props.children.filter(child => child) : props.children
52
78
  const totalElements = Array.isArray(newChild) ? newChild.length : (newChild ? 1 : 0)
53
- // const defaultHeight = (styleObj?.height || 150) - previousMargin - nextMargin
54
- // const defaultWidth = (styleObj?.width || width || 375) - previousMargin - nextMargin
55
- const defaultHeight = (styleObj?.height || 150)
56
- const defaultWidth = (styleObj?.width || width || 375)
79
+ const defaultHeight = (normalStyle?.height || 150)
80
+ const defaultWidth = (normalStyle?.width || width || 375)
57
81
  const dir = props.horizontal === false ? 'y' : 'x'
58
82
  // state的offset默认值
59
83
  // const initIndex = props.circular ? props.current + 1: (props.current || 0)
@@ -61,16 +85,17 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
61
85
  const initIndex = props.current || 0
62
86
  // 这里要排除超过元素个数的设置
63
87
  const initOffsetIndex = initIndex + (props.circular && totalElements > 1 ? 1 : 0)
64
- // const defaultX = (defaultWidth * initOffsetIndex + previousMargin) || 0
65
- // const defaultY = (defaultHeight * initOffsetIndex + previousMargin) || 0
66
88
  const defaultX = (defaultWidth * initOffsetIndex) || 0
67
89
  const defaultY = (defaultHeight * initOffsetIndex) || 0
68
90
  // 内部存储上一次的offset值
69
91
  const autoplayTimerRef = useRef<ReturnType <typeof setTimeout> | null>(null)
70
- const { nodeRef: scrollViewRef } = useNodesRef<ScrollView, CarouseProps>(props, ref, {
71
- })
72
- // 存储layout布局信息
73
- const layoutRef = useRef({})
92
+ const { nodeRef: scrollViewRef } = useNodesRef<ScrollView & View, CarouseProps>(props, ref, {})
93
+ const {
94
+ // 存储layout布局信息
95
+ layoutRef,
96
+ layoutProps,
97
+ layoutStyle
98
+ } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: scrollViewRef, onLayout: onWrapperLayout })
74
99
  // 内部存储上一次的偏移量
75
100
  const internalsRef = useRef({
76
101
  offset: {
@@ -82,18 +107,17 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
82
107
  const isDragRef = useRef(false)
83
108
  const [state, setState] = useState({
84
109
  children: newChild,
85
- width: defaultWidth || 375,
86
- height: defaultHeight,
110
+ width: dir === 'x' && typeof defaultWidth === 'number' ? defaultWidth - previousMargin - nextMargin : defaultWidth,
111
+ height: dir === 'y' && typeof defaultHeight === 'number' ? defaultHeight - previousMargin - nextMargin : defaultHeight,
87
112
  // 真正的游标索引, 从0开始
88
113
  index: initIndex,
89
114
  total: totalElements,
90
115
  offset: {
91
116
  x: dir === 'x' ? defaultX : 0,
92
- y: dir === 'y' ? defaultY: 0
117
+ y: dir === 'y' ? defaultY : 0
93
118
  },
94
119
  dir
95
- } as CarouseState);
96
-
120
+ } as CarouseState)
97
121
  /**
98
122
  * @desc: 开启下一次自动轮播
99
123
  */
@@ -109,39 +133,68 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
109
133
  if (props.autoplay) {
110
134
  createAutoPlay()
111
135
  }
112
- }, [props.autoplay, props.current, state.index, state.width, state.height]);
136
+ }, [props.autoplay, props.current, state.index, state.width, state.height])
113
137
 
138
+ useEffect(() => {
139
+ // 确认这个是变化的props变化的时候才执行,还是初始化的时候就执行
140
+ if (!props.autoplay && props.current !== state.index) {
141
+ const initIndex = props.current || 0
142
+ // 这里要排除超过元素个数的设置
143
+ const initOffsetIndex = initIndex + (props.circular && totalElements > 1 ? 1 : 0)
144
+ const defaultX = (defaultWidth * initOffsetIndex) || 0
145
+ const offset = {
146
+ x: dir === 'x' ? defaultX : 0,
147
+ y: dir === 'y' ? defaultY : 0
148
+ }
149
+ state.offset = offset
150
+ internalsRef.current.offset = offset
151
+ setState((preState) => {
152
+ return {
153
+ ...preState,
154
+ offset
155
+ }
156
+ })
157
+ }
158
+ }, [props.current])
114
159
  /**
115
160
  * @desc: 更新状态: index和offset, 并响应索引变化的事件
116
161
  * scrollViewOffset: 移动到的目标位置
117
162
  */
118
163
  function updateIndex (scrollViewOffset: NativeScrollPoint, useIndex = false) {
119
164
  const { nextIndex, nextOffset } = getNextConfig(scrollViewOffset)
120
- internalsRef.current.offset = nextOffset
165
+ updateState(nextIndex, nextOffset)
166
+ // 更新完状态之后, 开启新的loop
167
+ }
168
+
169
+ /**
170
+ * 更新索引状态
171
+ */
172
+ function updateState (index: number, offset: { x: number, y: number}) {
173
+ internalsRef.current.offset = offset
121
174
  setState((preState) => {
122
- const newState = {
175
+ const newState = {
123
176
  ...preState,
124
- index: nextIndex,
177
+ index: index,
125
178
  // offset用来指示当前scrollView的偏移量
126
- offset: nextOffset,
179
+ offset: offset
127
180
  }
128
181
  return newState
129
182
  })
130
183
  internalsRef.current.isScrolling = false
131
184
  // getCustomEvent
132
- const eventData = getCustomEvent('change', {}, { detail: {current: nextIndex, source: 'touch' }, layoutRef: layoutRef })
185
+ const eventData = getCustomEvent('change', {}, { detail: { current: index, source: 'touch' }, layoutRef: layoutRef })
133
186
  props.bindchange && props.bindchange(eventData)
134
- // 更新完状态之后, 开启新的loop
135
187
  }
136
188
 
137
189
  /**
138
190
  * @desc: 获取下一个位置的索引、scrollView的contentOffset、scrollTo到的offset
139
191
  * @desc: 包括正循环、反向循环、不循环
140
- * 其中循环模式为了实现无缝链接, 会将结合contentOffset, 和 scrollTo的offset, 先scrollTo一个位置的坐标, 然后通过updateIndex设置真正的index和内容的offset,视觉上是无缝
192
+ * 其中循环模式为了实现无缝链接, 会将结合contentOffset, 和 scrollTo的offset,
193
+ * 先scrollTo一个位置的坐标, 然后通过updateIndex设置真正的index和内容的offset,视觉上是无缝
141
194
  */
142
195
  function getNextConfig (scrollViewOffset: NativeScrollPoint) {
143
196
  const step = state.dir === 'x' ? state.width : state.height
144
- let currentOffset = state.offset
197
+ const currentOffset = state.offset
145
198
  let nextIndex = state.index + 1
146
199
  let nextOffset = currentOffset
147
200
  // autoMoveOffset scrollView 滚动到前后增加的位置
@@ -163,17 +216,17 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
163
216
  autoMoveOffset = Object.assign({}, currentOffset, { [state.dir]: 0 })
164
217
  nextIndex = state.total - 1
165
218
  // 反向: 数组最后一个index
166
- nextOffset = Object.assign({}, currentOffset, { [state.dir]: step * state.total }),
219
+ nextOffset = Object.assign({}, currentOffset, { [state.dir]: step * state.total })
167
220
  isAutoEnd = true
168
221
  } else {
169
222
  // 反向非最后一个
170
- nextOffset = Object.assign({}, currentOffset, { [state.dir]: ( nextIndex + 1 ) * step })
223
+ nextOffset = Object.assign({}, currentOffset, { [state.dir]: (nextIndex + 1) * step })
171
224
  }
172
225
  } else {
173
226
  if (nextIndex > state.total - 1) {
174
- autoMoveOffset = Object.assign({}, currentOffset, { [state.dir]: step * ( nextIndex + 1 )})
227
+ autoMoveOffset = Object.assign({}, currentOffset, { [state.dir]: step * (nextIndex + 1) })
175
228
  nextIndex = 0
176
- nextOffset = Object.assign({}, currentOffset, { [state.dir]: step }),
229
+ nextOffset = Object.assign({}, currentOffset, { [state.dir]: step })
177
230
  isAutoEnd = true
178
231
  } else {
179
232
  // nextIndex = nextIndex,
@@ -182,8 +235,11 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
182
235
  }
183
236
  }
184
237
  return {
238
+ // 下一个要滚动到的实际元素的索引
185
239
  nextIndex,
240
+ // 下一个要滚动到实际元素的offset
186
241
  nextOffset,
242
+ // scrollTo一个位置的坐标, 虚拟元素的位置
187
243
  autoMoveOffset,
188
244
  isAutoEnd
189
245
  }
@@ -206,40 +262,34 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
206
262
  // scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: true })
207
263
  if (Platform.OS === 'ios') {
208
264
  if (!isAutoEnd) {
209
- scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: true })
265
+ scrollViewRef.current?.scrollTo({ x: nextOffset.x, y: nextOffset.y, animated: true })
210
266
  } else {
211
267
  if (state.dir === 'x') {
212
- scrollViewRef.current?.scrollTo({ x: autoMoveOffset['x'], y: autoMoveOffset['x'], animated: true })
268
+ scrollViewRef.current?.scrollTo({ x: autoMoveOffset.x, y: autoMoveOffset.x, animated: true })
213
269
  } else {
214
- scrollViewRef.current?.scrollTo({ x: autoMoveOffset['y'], y: autoMoveOffset['y'], animated: true })
270
+ scrollViewRef.current?.scrollTo({ x: autoMoveOffset.y, y: autoMoveOffset.y, animated: true })
215
271
  }
216
272
  }
217
273
  } else {
218
274
  if (!isAutoEnd) {
219
- scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: true })
275
+ scrollViewRef.current?.scrollTo({ x: nextOffset.x, y: nextOffset.y, animated: true })
220
276
  onScrollEnd({
221
277
  nativeEvent: {
222
- // @ts-ignore
223
- x: +nextOffset['x'],
224
- y: +nextOffset['y']
278
+ contentOffset: {
279
+ x: +nextOffset.x,
280
+ y: +nextOffset.y
281
+ }
225
282
  }
226
- })
283
+ } as NativeSyntheticEvent<NativeScrollEvent>)
227
284
  } else {
228
- setTimeout(() => {
229
- onScrollEnd({
230
- nativeEvent: {
231
- // @ts-ignore
232
- x: 0,
233
- y: 0
234
- }
235
- })
236
- }, 10)
285
+ // 安卓无法实现视觉的无缝连接, 只能回到真正的位置, 且安卓调用scrollTo不能触发onMomentumScrollEnd,还未找到为啥
237
286
  if (state.dir === 'x') {
238
287
  scrollViewRef.current?.scrollTo({ x: step, y: step, animated: true })
239
- // scrollViewRef.current?.scrollTo({ x: autoMoveOffset['x'], y: autoMoveOffset['x'], animated: true })
288
+ // scrollViewRef.current?.scrollTo({ x: autoMoveOffset.x, y: autoMoveOffset.y, animated: true })
240
289
  } else {
241
- scrollViewRef.current?.scrollTo({ x: autoMoveOffset['y'], y: autoMoveOffset['y'], animated: true })
290
+ scrollViewRef.current?.scrollTo({ x: autoMoveOffset.x, y: step, animated: true })
242
291
  }
292
+ updateState(0, nextOffset)
243
293
  }
244
294
  }
245
295
  }
@@ -274,16 +324,6 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
274
324
  const internalOffset = internalsRef.current.offset
275
325
  const previousOffset = props.horizontal ? internalOffset.x : internalOffset.y
276
326
  const moveOffset = props.horizontal ? contentOffset.x : contentOffset.y
277
- // const diff = moveOffset - previousOffset
278
- /*
279
- if (diff > 0 && state.index + 1 >= total) {
280
- const { nextOffset } = getNextConfig(contentOffset)
281
- // scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: false })
282
- } else if ( diff < 0 && state.index -1 < 0) {
283
- const { nextOffset } = getNextConfig(contentOffset)
284
- // scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: false })
285
- }
286
- */
287
327
  if (previousOffset === moveOffset && (index === 0 || index === total - 1)) {
288
328
  internalsRef.current.isScrolling = false
289
329
  }
@@ -292,9 +332,13 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
292
332
  /**
293
333
  * @desc: 水平方向时,获取元素的布局,更新, 其中如果传递100%时需要依赖measure计算元算的宽高
294
334
  */
295
- function onWrapperLayout () {
335
+ function onWrapperLayout (e: LayoutChangeEvent) {
336
+ if (hasSelfPercent) {
337
+ const { width, height } = e?.nativeEvent?.layout || {}
338
+ setWidth(width || 0)
339
+ setHeight(height || 0)
340
+ }
296
341
  if (props.enableOffset) {
297
- // @ts-ignore
298
342
  scrollViewRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
299
343
  layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
300
344
  const isWDiff = state.width !== width
@@ -304,12 +348,14 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
304
348
  width: isWDiff ? width : state.width,
305
349
  height: isHDiff ? height : state.height
306
350
  }
307
- let correctOffset = Object.assign({}, state.offset, {
351
+ const attr = state.dir === 'x' ? 'width' : 'height'
352
+ changeState[attr] = changeState[attr] - previousMargin - nextMargin
353
+ const correctOffset = Object.assign({}, state.offset, {
308
354
  [state.dir]: initOffsetIndex * (state.dir === 'x' ? changeState.width : changeState.height)
309
355
  })
310
356
  state.offset = correctOffset
311
- state.width = width
312
- state.height = height
357
+ state.width = changeState.width
358
+ state.height = changeState.height
313
359
  setState((preState) => {
314
360
  return {
315
361
  ...preState,
@@ -318,18 +364,38 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
318
364
  height: changeState.height
319
365
  }
320
366
  })
321
- scrollViewRef.current?.scrollTo({ x: correctOffset['x'], y: correctOffset['y'], animated: false })
367
+ scrollViewRef.current?.scrollTo({ x: correctOffset.x, y: correctOffset.y, animated: false })
322
368
  }
323
369
  props.getInnerLayout && props.getInnerLayout(layoutRef)
324
370
  })
325
371
  }
326
372
  }
327
373
 
374
+ function getOffset (): Array<number> {
375
+ const step = state.dir === 'x' ? state.width : state.height
376
+ if (!step || Number.isNaN(+step)) return []
377
+ const offsetArray = []
378
+ if (previousMargin) {
379
+ offsetArray.push(0)
380
+ for (let i = 1; i < totalElements; i++) {
381
+ offsetArray.push(i * step - previousMargin)
382
+ }
383
+ } else {
384
+ for (let i = 0; i < totalElements; i++) {
385
+ offsetArray.push(i * step)
386
+ }
387
+ }
388
+ return offsetArray
389
+ }
390
+
328
391
  function renderScrollView (pages: ReactNode) {
329
- let scrollElementProps = {
392
+ const offsetsArray = getOffset()
393
+ const scrollElementProps = {
330
394
  ref: scrollViewRef,
331
395
  horizontal: props.horizontal,
332
- pagingEnabled: true,
396
+ pagingEnabled: false,
397
+ snapToOffsets: offsetsArray,
398
+ decelerationRate: 0.99, // 'fast'
333
399
  showsHorizontalScrollIndicator: false,
334
400
  showsVerticalScrollIndicator: false,
335
401
  bounces: false,
@@ -355,8 +421,8 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
355
421
 
356
422
  function renderPagination () {
357
423
  if (state.total <= 1) return null
358
- let dots: Array<ReactNode> = []
359
- const activeDotStyle = [ {
424
+ const dots: Array<ReactNode> = []
425
+ const activeDotStyle = [{
360
426
  backgroundColor: props.activeDotColor || '#007aff',
361
427
  width: 8,
362
428
  height: 8,
@@ -392,54 +458,66 @@ const _Carouse = forwardRef<HandlerRef<ScrollView, CarouseProps>, CarouseProps>(
392
458
  </View>
393
459
  )
394
460
  }
395
-
461
+
396
462
  function renderPages () {
397
463
  const { width, height, total, children } = state
398
464
  const { circular } = props
399
465
  const pageStyle = { width: width, height: height }
466
+ // 设置了previousMargin或者nextMargin,
467
+ // 1. 元素的宽度是减去这两个数目之和
468
+ // 2. previousMargin设置marginLeft正值, nextmargin设置marginRight负值
469
+ // 3. 第一个元素设置previousMargin 和 nextMargin, 最后一个元素
400
470
  if (total > 1 && Array.isArray(children)) {
401
471
  let arrElements: (Array<ReactNode>) = []
402
472
  // pages = ["2", "0", "1", "2", "0"]
403
- let pages = Array.isArray(children) && Object.keys(children) || []
473
+ const pages = Array.isArray(children) ? Object.keys(children) : []
404
474
  /* 无限循环的时候 */
405
475
  if (circular) {
406
476
  pages.unshift(total - 1 + '')
407
477
  pages.push('0')
408
478
  }
409
479
  arrElements = pages.map((page, i) => {
410
- return (
411
- <View style={[pageStyle, styles.slide]} key={ 'page' + i}>
412
- {children[+page]}
413
- </View>
414
- )
480
+ const extraStyle = {} as {
481
+ [key: string]: any
482
+ }
483
+ if (i === 0 && dir === 'x' && typeof width === 'number') {
484
+ previousMargin && (extraStyle.marginLeft = previousMargin)
485
+ } else if (i === pages.length - 1 && typeof width === 'number') {
486
+ nextMargin && (extraStyle.marginRight = nextMargin)
487
+ }
488
+ // return (<View style={[pageStyle, styles.slide, extraStyle]} key={ 'page' + i}>{children[+page]}</View>)
489
+ return (<View style={[pageStyle, styles.slide, extraStyle]} key={ 'page' + i}>
490
+ {wrapChildren(
491
+ {
492
+ children: children[+page]
493
+ },
494
+ {
495
+ hasVarDec,
496
+ varContext: varContextRef.current,
497
+ textStyle,
498
+ textProps
499
+ }
500
+ )}
501
+ </View>)
415
502
  })
416
503
  return arrElements
417
504
  } else {
418
- return (
505
+ const realElement = (
419
506
  <View style={pageStyle} key={0}>
420
507
  {children}
421
508
  </View>
422
509
  )
510
+ return realElement
423
511
  }
424
512
  }
425
513
 
426
514
  const pages: Array<ReactNode> | ReactNode = renderPages()
427
- const strStyle: string = 'container_' + state.dir
428
- const eventProps = props.innerProps || {}
429
- const layoutStyle = dir === 'x' ? { width: defaultWidth, height: defaultHeight } : { width: defaultWidth }
430
-
431
- return (<View style={[layoutStyle]}>
432
- <View
433
- style={[styles[strStyle], layoutStyle]}
434
- {...eventProps}
435
- onLayout={onWrapperLayout}>
515
+ return (<View style={[normalStyle, innerStyle, layoutStyle]} {...layoutProps}>
436
516
  {renderScrollView(pages)}
437
- </View>
438
- <View>{props.showsPagination && renderPagination()}</View>
517
+ {props.showsPagination && renderPagination()}
439
518
  </View>)
440
-
441
519
  })
442
520
 
443
- _Carouse.displayName = '_Carouse';
521
+ _Carouse.displayName = '_Carouse'
444
522
 
445
523
  export default _Carouse
@@ -1,6 +1,6 @@
1
1
  import { ScrollView } from 'react-native'
2
2
  import { JSX, MutableRefObject, forwardRef, useRef } from 'react'
3
- import { default as Carouse } from './carouse'
3
+ import Carouse from './carouse'
4
4
  import { SwiperProps } from './type'
5
5
  import useInnerProps from '../getInnerListeners'
6
6
  import useNodesRef, { HandlerRef } from '../useNodesRef' // 引入辅助函数
@@ -22,7 +22,7 @@ import useNodesRef, { HandlerRef } from '../useNodesRef' // 引入辅助函数
22
22
  */
23
23
  const _SwiperWrapper = forwardRef<HandlerRef<ScrollView, SwiperProps>, SwiperProps>((props: SwiperProps, ref): JSX.Element => {
24
24
  const { children } = props
25
- let innerLayout = useRef({})
25
+ const innerLayout = useRef({})
26
26
  const swiperProp = {
27
27
  circular: props.circular || false,
28
28
  current: props.current || 0,
@@ -30,18 +30,22 @@ const _SwiperWrapper = forwardRef<HandlerRef<ScrollView, SwiperProps>, SwiperPro
30
30
  duration: props.duration || 500,
31
31
  interval: props.interval || 5000,
32
32
  showsPagination: props['indicator-dots'],
33
- dotColor: props['indicator-color'] || "rgba(0, 0, 0, .3)",
33
+ dotColor: props['indicator-color'] || 'rgba(0, 0, 0, .3)',
34
34
  activeDotColor: props['indicator-active-color'] || '#000000',
35
35
  horizontal: props.vertical !== undefined ? !props.vertical : true,
36
- styleObj: props.style || {},
37
36
  previousMargin: props['previous-margin'] ? parseInt(props['previous-margin']) : 0,
38
37
  nextMargin: props['next-margin'] ? parseInt(props['next-margin']) : 0,
39
38
  enableOffset: props['enable-offset'] || true,
39
+ enableVar: props['enable-var'] || false,
40
+ parentFontSize: props['parent-font-size'],
41
+ parentWidth: props['parent-width'],
42
+ parentHeight: props['parent-height'],
43
+ style: props.style || {},
44
+ externalVarContext: props['external-var-context'],
40
45
  bindchange: props.bindchange,
41
46
  easingFunction: props['easing-function'] || 'default'
42
47
  }
43
- const { nodeRef } = useNodesRef<ScrollView, SwiperProps>(props, ref, {
44
- })
48
+ const { nodeRef } = useNodesRef<ScrollView, SwiperProps>(props, ref, {})
45
49
  const innerProps = useInnerProps(props, {
46
50
  ref: nodeRef
47
51
  }, [
@@ -49,19 +53,24 @@ const _SwiperWrapper = forwardRef<HandlerRef<ScrollView, SwiperProps>, SwiperPro
49
53
  'indicator-color',
50
54
  'indicator-active-color',
51
55
  'previous-margin',
52
- 'next-margin'
56
+ 'vertical',
57
+ 'previous-margin',
58
+ 'next-margin',
59
+ 'easing-function'
53
60
  ], { layoutRef: innerLayout })
61
+
54
62
  const getInnerLayout = (layout: MutableRefObject<{}>) => {
55
63
  innerLayout.current = layout.current
56
64
  }
65
+
57
66
  return <Carouse
58
67
  getInnerLayout={getInnerLayout}
59
68
  innerProps={innerProps}
69
+ {...innerProps}
60
70
  {...swiperProp}>
61
71
  {children}
62
72
  </Carouse>
63
-
64
73
  })
65
- _SwiperWrapper.displayName = 'mpx-swiper';
74
+ _SwiperWrapper.displayName = 'mpx-swiper'
66
75
 
67
76
  export default _SwiperWrapper
@@ -14,11 +14,18 @@ export interface SwiperProps {
14
14
  'indicator-color'?: string;
15
15
  'indicator-active-color'?: string;
16
16
  vertical?: boolean;
17
- style?: Object;
17
+ style: {
18
+ [key: string]: any
19
+ };
18
20
  'easing-function'?: EaseType;
19
21
  'previous-margin'?: string;
20
22
  'next-margin'?: string;
21
23
  'enable-offset'?: boolean;
24
+ 'enable-var': boolean;
25
+ 'parent-font-size'?: number;
26
+ 'parent-width'?: number;
27
+ 'parent-height'?: number;
28
+ 'external-var-context'?: Record<string, any>;
22
29
  bindchange?: (event: NativeSyntheticEvent<TouchEvent> | unknown) => void;
23
30
  }
24
31
 
@@ -37,13 +44,17 @@ export interface CarouseProps {
37
44
  previousMargin?: number;
38
45
  nextMargin?: number;
39
46
  enableOffset?: boolean;
47
+ parentFontSize?: number;
48
+ parentWidth?: number;
49
+ parentHeight?: number;
40
50
  bindchange?: (event: NativeSyntheticEvent<TouchEvent> | unknown) => void;
41
- getInnerLayout: Function,
51
+ getInnerLayout: Function;
42
52
  innerProps: Object;
43
- styleObj: {
44
- height?: number;
45
- width?: number
53
+ style: {
54
+ [key: string]: any
46
55
  };
56
+ enableVar: boolean;
57
+ externalVarContext?: Record<string, any>;
47
58
  }
48
59
 
49
60
  export interface CarouseState {