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