@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
@@ -5,6 +5,7 @@ import { Animated, View, Dimensions, Platform } from 'react-native';
5
5
  import { forwardRef, useState, useRef, useEffect } from 'react';
6
6
  import { getCustomEvent } from '../getInnerListeners';
7
7
  import useNodesRef from '../useNodesRef'; // 引入辅助函数
8
+ import { useTransformStyle, splitStyle, splitProps, useLayout, wrapChildren } from '../utils';
8
9
  /**
9
10
  * 默认的Style类型
10
11
  */
@@ -13,10 +14,10 @@ const styles = {
13
14
  backgroundColor: 'transparent'
14
15
  },
15
16
  container_x: {
16
- position: 'relative',
17
+ position: 'relative'
17
18
  },
18
19
  container_y: {
19
- position: 'relative',
20
+ position: 'relative'
20
21
  },
21
22
  pagination_x: {
22
23
  position: 'absolute',
@@ -26,7 +27,7 @@ const styles = {
26
27
  flexDirection: 'row',
27
28
  flex: 1,
28
29
  justifyContent: 'center',
29
- alignItems: 'center',
30
+ alignItems: 'center'
30
31
  },
31
32
  pagination_y: {
32
33
  position: 'absolute',
@@ -36,19 +37,27 @@ const styles = {
36
37
  flexDirection: 'column',
37
38
  flex: 1,
38
39
  justifyContent: 'center',
39
- alignItems: 'center',
40
+ alignItems: 'center'
40
41
  }
41
42
  };
42
43
  const _Carouse = forwardRef((props, ref) => {
43
44
  // 默认取水平方向的width
44
45
  const { width } = Dimensions.get('window');
45
- const { styleObj } = props;
46
+ const { style, previousMargin = 0, nextMargin = 0, enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight } = props;
47
+ // 计算transfrom之类的
48
+ const { normalStyle, hasVarDec, varContextRef, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, {
49
+ enableVar,
50
+ externalVarContext,
51
+ parentFontSize,
52
+ parentWidth,
53
+ parentHeight
54
+ });
55
+ const { textStyle, innerStyle } = splitStyle(normalStyle);
56
+ const { textProps } = splitProps(props);
46
57
  const newChild = Array.isArray(props.children) ? props.children.filter(child => child) : props.children;
47
58
  const totalElements = Array.isArray(newChild) ? newChild.length : (newChild ? 1 : 0);
48
- // const defaultHeight = (styleObj?.height || 150) - previousMargin - nextMargin
49
- // const defaultWidth = (styleObj?.width || width || 375) - previousMargin - nextMargin
50
- const defaultHeight = (styleObj?.height || 150);
51
- const defaultWidth = (styleObj?.width || width || 375);
59
+ const defaultHeight = (normalStyle?.height || 150);
60
+ const defaultWidth = (normalStyle?.width || width || 375);
52
61
  const dir = props.horizontal === false ? 'y' : 'x';
53
62
  // state的offset默认值
54
63
  // const initIndex = props.circular ? props.current + 1: (props.current || 0)
@@ -56,15 +65,15 @@ const _Carouse = forwardRef((props, ref) => {
56
65
  const initIndex = props.current || 0;
57
66
  // 这里要排除超过元素个数的设置
58
67
  const initOffsetIndex = initIndex + (props.circular && totalElements > 1 ? 1 : 0);
59
- // const defaultX = (defaultWidth * initOffsetIndex + previousMargin) || 0
60
- // const defaultY = (defaultHeight * initOffsetIndex + previousMargin) || 0
61
68
  const defaultX = (defaultWidth * initOffsetIndex) || 0;
62
69
  const defaultY = (defaultHeight * initOffsetIndex) || 0;
63
70
  // 内部存储上一次的offset值
64
71
  const autoplayTimerRef = useRef(null);
65
- const { nodeRef: scrollViewRef } = useNodesRef(props, ref, {});
72
+ const scrollViewRef = useRef(null);
73
+ useNodesRef(props, ref, scrollViewRef, {});
74
+ const {
66
75
  // 存储layout布局信息
67
- const layoutRef = useRef({});
76
+ layoutRef, layoutProps, layoutStyle } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: scrollViewRef, onLayout: onWrapperLayout });
68
77
  // 内部存储上一次的偏移量
69
78
  const internalsRef = useRef({
70
79
  offset: {
@@ -76,8 +85,8 @@ const _Carouse = forwardRef((props, ref) => {
76
85
  const isDragRef = useRef(false);
77
86
  const [state, setState] = useState({
78
87
  children: newChild,
79
- width: defaultWidth || 375,
80
- height: defaultHeight,
88
+ width: dir === 'x' && typeof defaultWidth === 'number' ? defaultWidth - previousMargin - nextMargin : defaultWidth,
89
+ height: dir === 'y' && typeof defaultHeight === 'number' ? defaultHeight - previousMargin - nextMargin : defaultHeight,
81
90
  // 真正的游标索引, 从0开始
82
91
  index: initIndex,
83
92
  total: totalElements,
@@ -102,36 +111,64 @@ const _Carouse = forwardRef((props, ref) => {
102
111
  createAutoPlay();
103
112
  }
104
113
  }, [props.autoplay, props.current, state.index, state.width, state.height]);
114
+ useEffect(() => {
115
+ // 确认这个是变化的props变化的时候才执行,还是初始化的时候就执行
116
+ if (!props.autoplay && props.current !== state.index) {
117
+ const initIndex = props.current || 0;
118
+ // 这里要排除超过元素个数的设置
119
+ const initOffsetIndex = initIndex + (props.circular && totalElements > 1 ? 1 : 0);
120
+ const defaultX = (defaultWidth * initOffsetIndex) || 0;
121
+ const offset = {
122
+ x: dir === 'x' ? defaultX : 0,
123
+ y: dir === 'y' ? defaultY : 0
124
+ };
125
+ state.offset = offset;
126
+ internalsRef.current.offset = offset;
127
+ setState((preState) => {
128
+ return {
129
+ ...preState,
130
+ offset
131
+ };
132
+ });
133
+ }
134
+ }, [props.current]);
105
135
  /**
106
136
  * @desc: 更新状态: index和offset, 并响应索引变化的事件
107
137
  * scrollViewOffset: 移动到的目标位置
108
138
  */
109
139
  function updateIndex(scrollViewOffset, useIndex = false) {
110
140
  const { nextIndex, nextOffset } = getNextConfig(scrollViewOffset);
111
- internalsRef.current.offset = nextOffset;
141
+ updateState(nextIndex, nextOffset);
142
+ // 更新完状态之后, 开启新的loop
143
+ }
144
+ /**
145
+ * 更新索引状态
146
+ */
147
+ function updateState(index, offset) {
148
+ internalsRef.current.offset = offset;
112
149
  setState((preState) => {
113
150
  const newState = {
114
151
  ...preState,
115
- index: nextIndex,
152
+ index: index,
116
153
  // offset用来指示当前scrollView的偏移量
117
- offset: nextOffset,
154
+ offset: offset
118
155
  };
119
156
  return newState;
120
157
  });
121
158
  internalsRef.current.isScrolling = false;
122
159
  // getCustomEvent
123
- const eventData = getCustomEvent('change', {}, { detail: { current: nextIndex, source: 'touch' }, layoutRef: layoutRef });
160
+ const eventData = getCustomEvent('change', {}, { detail: { current: index, source: 'touch' }, layoutRef: layoutRef });
124
161
  props.bindchange && props.bindchange(eventData);
125
- // 更新完状态之后, 开启新的loop
126
162
  }
127
163
  /**
128
164
  * @desc: 获取下一个位置的索引、scrollView的contentOffset、scrollTo到的offset
129
165
  * @desc: 包括正循环、反向循环、不循环
130
- * 其中循环模式为了实现无缝链接, 会将结合contentOffset, 和 scrollTo的offset, 先scrollTo一个位置的坐标, 然后通过updateIndex设置真正的index和内容的offset,视觉上是无缝
166
+ * 其中循环模式为了实现无缝链接, 会将结合contentOffset, 和 scrollTo的offset,
167
+ * 先scrollTo一个位置的坐标, 然后通过updateIndex设置真正的index和内容的offset,视觉上是无缝
131
168
  */
132
169
  function getNextConfig(scrollViewOffset) {
133
170
  const step = state.dir === 'x' ? state.width : state.height;
134
- let currentOffset = state.offset;
171
+ const currentOffset = state.offset;
135
172
  let nextIndex = state.index + 1;
136
173
  let nextOffset = currentOffset;
137
174
  // autoMoveOffset scrollView 滚动到前后增加的位置
@@ -154,8 +191,8 @@ const _Carouse = forwardRef((props, ref) => {
154
191
  autoMoveOffset = Object.assign({}, currentOffset, { [state.dir]: 0 });
155
192
  nextIndex = state.total - 1;
156
193
  // 反向: 数组最后一个index
157
- nextOffset = Object.assign({}, currentOffset, { [state.dir]: step * state.total }),
158
- isAutoEnd = true;
194
+ nextOffset = Object.assign({}, currentOffset, { [state.dir]: step * state.total });
195
+ isAutoEnd = true;
159
196
  }
160
197
  else {
161
198
  // 反向非最后一个
@@ -166,8 +203,8 @@ const _Carouse = forwardRef((props, ref) => {
166
203
  if (nextIndex > state.total - 1) {
167
204
  autoMoveOffset = Object.assign({}, currentOffset, { [state.dir]: step * (nextIndex + 1) });
168
205
  nextIndex = 0;
169
- nextOffset = Object.assign({}, currentOffset, { [state.dir]: step }),
170
- isAutoEnd = true;
206
+ nextOffset = Object.assign({}, currentOffset, { [state.dir]: step });
207
+ isAutoEnd = true;
171
208
  }
172
209
  else {
173
210
  // nextIndex = nextIndex,
@@ -176,8 +213,11 @@ const _Carouse = forwardRef((props, ref) => {
176
213
  }
177
214
  }
178
215
  return {
216
+ // 下一个要滚动到的实际元素的索引
179
217
  nextIndex,
218
+ // 下一个要滚动到实际元素的offset
180
219
  nextOffset,
220
+ // scrollTo一个位置的坐标, 虚拟元素的位置
181
221
  autoMoveOffset,
182
222
  isAutoEnd
183
223
  };
@@ -199,45 +239,39 @@ const _Carouse = forwardRef((props, ref) => {
199
239
  // scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: true })
200
240
  if (Platform.OS === 'ios') {
201
241
  if (!isAutoEnd) {
202
- scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: true });
242
+ scrollViewRef.current?.scrollTo({ x: nextOffset.x, y: nextOffset.y, animated: true });
203
243
  }
204
244
  else {
205
245
  if (state.dir === 'x') {
206
- scrollViewRef.current?.scrollTo({ x: autoMoveOffset['x'], y: autoMoveOffset['x'], animated: true });
246
+ scrollViewRef.current?.scrollTo({ x: autoMoveOffset.x, y: autoMoveOffset.x, animated: true });
207
247
  }
208
248
  else {
209
- scrollViewRef.current?.scrollTo({ x: autoMoveOffset['y'], y: autoMoveOffset['y'], animated: true });
249
+ scrollViewRef.current?.scrollTo({ x: autoMoveOffset.y, y: autoMoveOffset.y, animated: true });
210
250
  }
211
251
  }
212
252
  }
213
253
  else {
214
254
  if (!isAutoEnd) {
215
- scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: true });
255
+ scrollViewRef.current?.scrollTo({ x: nextOffset.x, y: nextOffset.y, animated: true });
216
256
  onScrollEnd({
217
257
  nativeEvent: {
218
- // @ts-ignore
219
- x: +nextOffset['x'],
220
- y: +nextOffset['y']
258
+ contentOffset: {
259
+ x: +nextOffset.x,
260
+ y: +nextOffset.y
261
+ }
221
262
  }
222
263
  });
223
264
  }
224
265
  else {
225
- setTimeout(() => {
226
- onScrollEnd({
227
- nativeEvent: {
228
- // @ts-ignore
229
- x: 0,
230
- y: 0
231
- }
232
- });
233
- }, 10);
266
+ // 安卓无法实现视觉的无缝连接, 只能回到真正的位置, 且安卓调用scrollTo不能触发onMomentumScrollEnd,还未找到为啥
234
267
  if (state.dir === 'x') {
235
268
  scrollViewRef.current?.scrollTo({ x: step, y: step, animated: true });
236
- // scrollViewRef.current?.scrollTo({ x: autoMoveOffset['x'], y: autoMoveOffset['x'], animated: true })
269
+ // scrollViewRef.current?.scrollTo({ x: autoMoveOffset.x, y: autoMoveOffset.y, animated: true })
237
270
  }
238
271
  else {
239
- scrollViewRef.current?.scrollTo({ x: autoMoveOffset['y'], y: autoMoveOffset['y'], animated: true });
272
+ scrollViewRef.current?.scrollTo({ x: autoMoveOffset.x, y: step, animated: true });
240
273
  }
274
+ updateState(0, nextOffset);
241
275
  }
242
276
  }
243
277
  }
@@ -269,16 +303,6 @@ const _Carouse = forwardRef((props, ref) => {
269
303
  const internalOffset = internalsRef.current.offset;
270
304
  const previousOffset = props.horizontal ? internalOffset.x : internalOffset.y;
271
305
  const moveOffset = props.horizontal ? contentOffset.x : contentOffset.y;
272
- // const diff = moveOffset - previousOffset
273
- /*
274
- if (diff > 0 && state.index + 1 >= total) {
275
- const { nextOffset } = getNextConfig(contentOffset)
276
- // scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: false })
277
- } else if ( diff < 0 && state.index -1 < 0) {
278
- const { nextOffset } = getNextConfig(contentOffset)
279
- // scrollViewRef.current?.scrollTo({ x: nextOffset['x'], y: nextOffset['y'], animated: false })
280
- }
281
- */
282
306
  if (previousOffset === moveOffset && (index === 0 || index === total - 1)) {
283
307
  internalsRef.current.isScrolling = false;
284
308
  }
@@ -286,9 +310,13 @@ const _Carouse = forwardRef((props, ref) => {
286
310
  /**
287
311
  * @desc: 水平方向时,获取元素的布局,更新, 其中如果传递100%时需要依赖measure计算元算的宽高
288
312
  */
289
- function onWrapperLayout() {
313
+ function onWrapperLayout(e) {
314
+ if (hasSelfPercent) {
315
+ const { width, height } = e?.nativeEvent?.layout || {};
316
+ setWidth(width || 0);
317
+ setHeight(height || 0);
318
+ }
290
319
  if (props.enableOffset) {
291
- // @ts-ignore
292
320
  scrollViewRef.current?.measure((x, y, width, height, offsetLeft, offsetTop) => {
293
321
  layoutRef.current = { x, y, width, height, offsetLeft, offsetTop };
294
322
  const isWDiff = state.width !== width;
@@ -298,12 +326,14 @@ const _Carouse = forwardRef((props, ref) => {
298
326
  width: isWDiff ? width : state.width,
299
327
  height: isHDiff ? height : state.height
300
328
  };
301
- let correctOffset = Object.assign({}, state.offset, {
329
+ const attr = state.dir === 'x' ? 'width' : 'height';
330
+ changeState[attr] = changeState[attr] - previousMargin - nextMargin;
331
+ const correctOffset = Object.assign({}, state.offset, {
302
332
  [state.dir]: initOffsetIndex * (state.dir === 'x' ? changeState.width : changeState.height)
303
333
  });
304
334
  state.offset = correctOffset;
305
- state.width = width;
306
- state.height = height;
335
+ state.width = changeState.width;
336
+ state.height = changeState.height;
307
337
  setState((preState) => {
308
338
  return {
309
339
  ...preState,
@@ -312,17 +342,38 @@ const _Carouse = forwardRef((props, ref) => {
312
342
  height: changeState.height
313
343
  };
314
344
  });
315
- scrollViewRef.current?.scrollTo({ x: correctOffset['x'], y: correctOffset['y'], animated: false });
345
+ scrollViewRef.current?.scrollTo({ x: correctOffset.x, y: correctOffset.y, animated: false });
316
346
  }
317
347
  props.getInnerLayout && props.getInnerLayout(layoutRef);
318
348
  });
319
349
  }
320
350
  }
351
+ function getOffset() {
352
+ const step = state.dir === 'x' ? state.width : state.height;
353
+ if (!step || Number.isNaN(+step))
354
+ return [];
355
+ const offsetArray = [];
356
+ if (previousMargin) {
357
+ offsetArray.push(0);
358
+ for (let i = 1; i < totalElements; i++) {
359
+ offsetArray.push(i * step - previousMargin);
360
+ }
361
+ }
362
+ else {
363
+ for (let i = 0; i < totalElements; i++) {
364
+ offsetArray.push(i * step);
365
+ }
366
+ }
367
+ return offsetArray;
368
+ }
321
369
  function renderScrollView(pages) {
322
- let scrollElementProps = {
370
+ const offsetsArray = getOffset();
371
+ const scrollElementProps = {
323
372
  ref: scrollViewRef,
324
373
  horizontal: props.horizontal,
325
- pagingEnabled: true,
374
+ pagingEnabled: false,
375
+ snapToOffsets: offsetsArray,
376
+ decelerationRate: 0.99,
326
377
  showsHorizontalScrollIndicator: false,
327
378
  showsVerticalScrollIndicator: false,
328
379
  bounces: false,
@@ -338,7 +389,7 @@ const _Carouse = forwardRef((props, ref) => {
338
389
  function renderPagination() {
339
390
  if (state.total <= 1)
340
391
  return null;
341
- let dots = [];
392
+ const dots = [];
342
393
  const activeDotStyle = [{
343
394
  backgroundColor: props.activeDotColor || '#007aff',
344
395
  width: 8,
@@ -375,37 +426,52 @@ const _Carouse = forwardRef((props, ref) => {
375
426
  const { width, height, total, children } = state;
376
427
  const { circular } = props;
377
428
  const pageStyle = { width: width, height: height };
429
+ // 设置了previousMargin或者nextMargin,
430
+ // 1. 元素的宽度是减去这两个数目之和
431
+ // 2. previousMargin设置marginLeft正值, nextmargin设置marginRight负值
432
+ // 3. 第一个元素设置previousMargin 和 nextMargin, 最后一个元素
378
433
  if (total > 1 && Array.isArray(children)) {
379
434
  let arrElements = [];
380
435
  // pages = ["2", "0", "1", "2", "0"]
381
- let pages = Array.isArray(children) && Object.keys(children) || [];
436
+ const pages = Array.isArray(children) ? Object.keys(children) : [];
382
437
  /* 无限循环的时候 */
383
438
  if (circular) {
384
439
  pages.unshift(total - 1 + '');
385
440
  pages.push('0');
386
441
  }
387
442
  arrElements = pages.map((page, i) => {
388
- return (<View style={[pageStyle, styles.slide]} key={'page' + i}>
389
- {children[+page]}
390
- </View>);
443
+ const extraStyle = {};
444
+ if (i === 0 && dir === 'x' && typeof width === 'number') {
445
+ previousMargin && (extraStyle.marginLeft = previousMargin);
446
+ }
447
+ else if (i === pages.length - 1 && typeof width === 'number') {
448
+ nextMargin && (extraStyle.marginRight = nextMargin);
449
+ }
450
+ // return (<View style={[pageStyle, styles.slide, extraStyle]} key={ 'page' + i}>{children[+page]}</View>)
451
+ return (<View style={[pageStyle, styles.slide, extraStyle]} key={'page' + i}>
452
+ {wrapChildren({
453
+ children: children[+page]
454
+ }, {
455
+ hasVarDec,
456
+ varContext: varContextRef.current,
457
+ textStyle,
458
+ textProps
459
+ })}
460
+ </View>);
391
461
  });
392
462
  return arrElements;
393
463
  }
394
464
  else {
395
- return (<View style={pageStyle} key={0}>
465
+ const realElement = (<View style={pageStyle} key={0}>
396
466
  {children}
397
467
  </View>);
468
+ return realElement;
398
469
  }
399
470
  }
400
471
  const pages = renderPages();
401
- const strStyle = 'container_' + state.dir;
402
- const eventProps = props.innerProps || {};
403
- const layoutStyle = dir === 'x' ? { width: defaultWidth, height: defaultHeight } : { width: defaultWidth };
404
- return (<View style={[layoutStyle]}>
405
- <View style={[styles[strStyle], layoutStyle]} {...eventProps} onLayout={onWrapperLayout}>
472
+ return (<View style={[normalStyle, innerStyle, layoutStyle]} {...layoutProps}>
406
473
  {renderScrollView(pages)}
407
- </View>
408
- <View>{props.showsPagination && renderPagination()}</View>
474
+ {props.showsPagination && renderPagination()}
409
475
  </View>);
410
476
  });
411
477
  _Carouse.displayName = '_Carouse';
@@ -1,5 +1,5 @@
1
1
  import { forwardRef, useRef } from 'react';
2
- import { default as Carouse } from './carouse';
2
+ import Carouse from './carouse';
3
3
  import useInnerProps from '../getInnerListeners';
4
4
  import useNodesRef from '../useNodesRef'; // 引入辅助函数
5
5
  /**
@@ -20,7 +20,7 @@ import useNodesRef from '../useNodesRef'; // 引入辅助函数
20
20
  */
21
21
  const _SwiperWrapper = forwardRef((props, ref) => {
22
22
  const { children } = props;
23
- let innerLayout = useRef({});
23
+ const innerLayout = useRef({});
24
24
  const swiperProp = {
25
25
  circular: props.circular || false,
26
26
  current: props.current || 0,
@@ -28,17 +28,23 @@ const _SwiperWrapper = forwardRef((props, ref) => {
28
28
  duration: props.duration || 500,
29
29
  interval: props.interval || 5000,
30
30
  showsPagination: props['indicator-dots'],
31
- dotColor: props['indicator-color'] || "rgba(0, 0, 0, .3)",
31
+ dotColor: props['indicator-color'] || 'rgba(0, 0, 0, .3)',
32
32
  activeDotColor: props['indicator-active-color'] || '#000000',
33
33
  horizontal: props.vertical !== undefined ? !props.vertical : true,
34
- styleObj: props.style || {},
35
34
  previousMargin: props['previous-margin'] ? parseInt(props['previous-margin']) : 0,
36
35
  nextMargin: props['next-margin'] ? parseInt(props['next-margin']) : 0,
37
36
  enableOffset: props['enable-offset'] || true,
37
+ enableVar: props['enable-var'] || false,
38
+ parentFontSize: props['parent-font-size'],
39
+ parentWidth: props['parent-width'],
40
+ parentHeight: props['parent-height'],
41
+ style: props.style || {},
42
+ externalVarContext: props['external-var-context'],
38
43
  bindchange: props.bindchange,
39
44
  easingFunction: props['easing-function'] || 'default'
40
45
  };
41
- const { nodeRef } = useNodesRef(props, ref, {});
46
+ const nodeRef = useRef(null);
47
+ useNodesRef(props, ref, nodeRef, {});
42
48
  const innerProps = useInnerProps(props, {
43
49
  ref: nodeRef
44
50
  }, [
@@ -46,12 +52,15 @@ const _SwiperWrapper = forwardRef((props, ref) => {
46
52
  'indicator-color',
47
53
  'indicator-active-color',
48
54
  'previous-margin',
49
- 'next-margin'
55
+ 'vertical',
56
+ 'previous-margin',
57
+ 'next-margin',
58
+ 'easing-function'
50
59
  ], { layoutRef: innerLayout });
51
60
  const getInnerLayout = (layout) => {
52
61
  innerLayout.current = layout.current;
53
62
  };
54
- return <Carouse getInnerLayout={getInnerLayout} innerProps={innerProps} {...swiperProp}>
63
+ return <Carouse getInnerLayout={getInnerLayout} innerProps={innerProps} {...innerProps} {...swiperProp}>
55
64
  {children}
56
65
  </Carouse>;
57
66
  });
@@ -2,23 +2,32 @@ import { View } from 'react-native';
2
2
  import { forwardRef, useRef } from 'react';
3
3
  import useInnerProps from './getInnerListeners';
4
4
  import useNodesRef from './useNodesRef'; // 引入辅助函数
5
+ import { useTransformStyle, splitStyle, splitProps, wrapChildren, useLayout } from './utils';
5
6
  const _SwiperItem = forwardRef((props, ref) => {
6
- const { children, 'enable-offset': enableOffset, style } = props;
7
- const layoutRef = useRef({});
8
- const { nodeRef } = useNodesRef(props, ref, {});
9
- const onLayout = () => {
10
- nodeRef.current?.measure((x, y, width, height, offsetLeft, offsetTop) => {
11
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop };
12
- });
13
- };
7
+ const { 'enable-offset': enableOffset, 'enable-var': enableVar, 'external-var-context': externalVarContext, style } = props;
8
+ const { textProps } = splitProps(props);
9
+ const nodeRef = useRef(null);
10
+ useNodesRef(props, ref, nodeRef, {});
11
+ const { normalStyle, hasVarDec, varContextRef, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, { enableVar, externalVarContext });
12
+ const { textStyle, innerStyle } = splitStyle(normalStyle);
13
+ const {
14
+ // 存储layout布局信息
15
+ layoutRef, layoutProps, layoutStyle } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: nodeRef });
14
16
  const innerProps = useInnerProps(props, {
15
- ...(enableOffset ? { onLayout } : {}),
17
+ style: { ...innerStyle, ...layoutStyle },
18
+ ref: nodeRef,
19
+ ...layoutProps
16
20
  }, [
17
21
  'children',
18
22
  'enable-offset'
19
23
  ], { layoutRef });
20
- return (<View ref={nodeRef} data-itemId={props['item-id']} style={[style]} {...innerProps}>
21
- {children}
24
+ return (<View data-itemId={props['item-id']} {...innerProps}>
25
+ {wrapChildren(props, {
26
+ hasVarDec,
27
+ varContext: varContextRef.current,
28
+ textStyle,
29
+ textProps
30
+ })}
22
31
  </View>);
23
32
  });
24
33
  _SwiperItem.displayName = 'mpx-swiper-item';
@@ -6,25 +6,34 @@
6
6
  */
7
7
  import { Switch } from 'react-native';
8
8
  import { useRef, useEffect, forwardRef, useState, useContext } from 'react';
9
+ import { warn } from '@mpxjs/utils';
9
10
  import useNodesRef from './useNodesRef'; // 引入辅助函数
10
11
  import useInnerProps, { getCustomEvent } from './getInnerListeners';
11
12
  import CheckBox from './mpx-checkbox';
12
13
  import { FormContext } from './context';
13
- import { throwReactWarning } from './utils';
14
+ import { useTransformStyle, useLayout } from './utils';
14
15
  const _Switch = forwardRef((props, ref) => {
15
- const { style = {}, checked = false, type = 'switch', disabled = false, color = '#04BE02', 'enable-offset': enableOffset, bindchange, catchchange, } = props;
16
+ const { style = {}, checked = false, type = 'switch', disabled = false, color = '#04BE02', 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight, bindchange, catchchange } = props;
16
17
  const [isChecked, setIsChecked] = useState(checked);
17
- const layoutRef = useRef({});
18
18
  const changeHandler = bindchange || catchchange;
19
19
  let formValuesMap;
20
20
  const formContext = useContext(FormContext);
21
21
  if (formContext) {
22
22
  formValuesMap = formContext.formValuesMap;
23
23
  }
24
+ const { normalStyle, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, {
25
+ enableVar,
26
+ externalVarContext,
27
+ parentFontSize,
28
+ parentWidth,
29
+ parentHeight
30
+ });
24
31
  useEffect(() => {
25
32
  setIsChecked(checked);
26
33
  }, [checked]);
27
- const { nodeRef } = useNodesRef(props, ref);
34
+ const nodeRef = useRef(null);
35
+ useNodesRef(props, ref, nodeRef);
36
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef });
28
37
  const onChange = (evt, { checked } = {}) => {
29
38
  if (type === 'switch') {
30
39
  setIsChecked(evt);
@@ -43,23 +52,18 @@ const _Switch = forwardRef((props, ref) => {
43
52
  };
44
53
  if (formValuesMap) {
45
54
  if (!props.name) {
46
- throwReactWarning('[Mpx runtime warn]: If a form component is used, the name attribute is required.');
55
+ warn('If a form component is used, the name attribute is required.');
47
56
  }
48
57
  else {
49
58
  formValuesMap.set(props.name, { getValue, resetValue });
50
59
  }
51
60
  }
52
- const onLayout = () => {
53
- nodeRef.current?.measure?.((x, y, width, height, offsetLeft, offsetTop) => {
54
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop };
55
- });
56
- };
57
61
  const innerProps = useInnerProps(props, {
58
62
  ref: nodeRef,
59
- ...enableOffset ? { onLayout } : {},
63
+ style: { ...normalStyle, ...layoutStyle },
64
+ ...layoutProps,
60
65
  ...!disabled ? { [type === 'switch' ? 'onValueChange' : '_onChange']: onChange } : {}
61
66
  }, [
62
- 'style',
63
67
  'checked',
64
68
  'disabled',
65
69
  'type',
@@ -68,9 +72,9 @@ const _Switch = forwardRef((props, ref) => {
68
72
  layoutRef
69
73
  });
70
74
  if (type === 'checkbox') {
71
- return <CheckBox {...innerProps} color={color} style={style} checked={isChecked}/>;
75
+ return <CheckBox {...innerProps} color={color} style={normalStyle} checked={isChecked}/>;
72
76
  }
73
- return (<Switch {...innerProps} style={style} value={isChecked} trackColor={{ false: "#FFF", true: color }} thumbColor={isChecked ? "#FFF" : "#f4f3f4"} ios_backgroundColor="#FFF"/>);
77
+ return <Switch {...innerProps} style={normalStyle} value={isChecked} trackColor={{ false: '#FFF', true: color }} thumbColor={isChecked ? '#FFF' : '#f4f3f4'} ios_backgroundColor="#FFF"/>;
74
78
  });
75
79
  _Switch.displayName = 'mpx-switch';
76
80
  export default _Switch;