@mpxjs/webpack-plugin 2.9.62 → 2.9.65

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