@mpxjs/webpack-plugin 2.9.70-alpha.0 → 2.9.71

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 (126) hide show
  1. package/LICENSE +433 -0
  2. package/README.md +1 -1
  3. package/lib/config.js +0 -14
  4. package/lib/dependencies/ResolveDependency.js +0 -5
  5. package/lib/index.js +7 -38
  6. package/lib/json-compiler/helper.js +3 -3
  7. package/lib/loader.js +0 -53
  8. package/lib/parser.js +1 -1
  9. package/lib/platform/json/wx/index.js +21 -8
  10. package/lib/platform/style/wx/index.js +51 -54
  11. package/lib/platform/template/wx/component-config/button.js +2 -14
  12. package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
  13. package/lib/platform/template/wx/component-config/image.js +0 -4
  14. package/lib/platform/template/wx/component-config/index.js +1 -1
  15. package/lib/platform/template/wx/component-config/input.js +0 -4
  16. package/lib/platform/template/wx/component-config/movable-view.js +8 -1
  17. package/lib/platform/template/wx/component-config/picker-view.js +1 -5
  18. package/lib/platform/template/wx/component-config/rich-text.js +6 -2
  19. package/lib/platform/template/wx/component-config/scroll-view.js +1 -5
  20. package/lib/platform/template/wx/component-config/switch.js +0 -4
  21. package/lib/platform/template/wx/component-config/text.js +0 -4
  22. package/lib/platform/template/wx/component-config/textarea.js +0 -5
  23. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  24. package/lib/platform/template/wx/component-config/view.js +0 -4
  25. package/lib/platform/template/wx/index.js +1 -131
  26. package/lib/resolve-loader.js +1 -4
  27. package/lib/runtime/components/react/context.ts +8 -0
  28. package/lib/runtime/components/react/dist/context.js +2 -0
  29. package/lib/runtime/components/react/dist/getInnerListeners.js +39 -37
  30. package/lib/runtime/components/react/dist/mpx-button.jsx +16 -44
  31. package/lib/runtime/components/react/dist/mpx-canvas/html.js +2 -4
  32. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +5 -1
  33. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +93 -58
  34. package/lib/runtime/components/react/dist/mpx-navigator.jsx +1 -1
  35. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +10 -14
  36. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +94 -81
  37. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +19 -16
  38. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +10 -11
  39. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +11 -4
  40. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +31 -8
  41. package/lib/runtime/components/react/dist/mpx-swiper.jsx +435 -371
  42. package/lib/runtime/components/react/dist/mpx-view.jsx +18 -53
  43. package/lib/runtime/components/react/dist/pickerFaces.js +3 -8
  44. package/lib/runtime/components/react/dist/pickerVIewContext.js +5 -0
  45. package/lib/runtime/components/react/dist/{pickerViewOverlay.jsx → pickerViewIndicator.jsx} +6 -6
  46. package/lib/runtime/components/react/dist/pickerViewMask.jsx +2 -2
  47. package/lib/runtime/components/react/dist/useAnimationHooks.js +27 -10
  48. package/lib/runtime/components/react/dist/utils.jsx +162 -70
  49. package/lib/runtime/components/react/event.config.ts +25 -26
  50. package/lib/runtime/components/react/getInnerListeners.ts +236 -182
  51. package/lib/runtime/components/react/mpx-button.tsx +27 -69
  52. package/lib/runtime/components/react/mpx-canvas/html.ts +2 -4
  53. package/lib/runtime/components/react/mpx-canvas/index.tsx +44 -46
  54. package/lib/runtime/components/react/mpx-checkbox-group.tsx +15 -13
  55. package/lib/runtime/components/react/mpx-checkbox.tsx +20 -21
  56. package/lib/runtime/components/react/mpx-form.tsx +15 -20
  57. package/lib/runtime/components/react/mpx-icon.tsx +2 -2
  58. package/lib/runtime/components/react/mpx-image.tsx +87 -47
  59. package/lib/runtime/components/react/mpx-input.tsx +24 -32
  60. package/lib/runtime/components/react/mpx-label.tsx +12 -14
  61. package/lib/runtime/components/react/mpx-movable-area.tsx +10 -16
  62. package/lib/runtime/components/react/mpx-movable-view.tsx +133 -92
  63. package/lib/runtime/components/react/mpx-navigator.tsx +3 -9
  64. package/lib/runtime/components/react/mpx-picker-view-column-item.tsx +76 -0
  65. package/lib/runtime/components/react/mpx-picker-view-column.tsx +206 -183
  66. package/lib/runtime/components/react/mpx-picker-view.tsx +49 -48
  67. package/lib/runtime/components/react/mpx-radio-group.tsx +13 -15
  68. package/lib/runtime/components/react/mpx-radio.tsx +19 -25
  69. package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
  70. package/lib/runtime/components/react/mpx-rich-text/index.tsx +115 -0
  71. package/lib/runtime/components/react/mpx-root-portal.tsx +3 -5
  72. package/lib/runtime/components/react/mpx-scroll-view.tsx +53 -43
  73. package/lib/runtime/components/react/mpx-swiper-item.tsx +45 -11
  74. package/lib/runtime/components/react/mpx-swiper.tsx +742 -0
  75. package/lib/runtime/components/react/mpx-switch.tsx +19 -15
  76. package/lib/runtime/components/react/mpx-text.tsx +8 -16
  77. package/lib/runtime/components/react/mpx-textarea.tsx +11 -10
  78. package/lib/runtime/components/react/mpx-view.tsx +28 -77
  79. package/lib/runtime/components/react/mpx-web-view.tsx +94 -59
  80. package/lib/runtime/components/react/pickerFaces.ts +10 -7
  81. package/lib/runtime/components/react/pickerVIewContext.ts +27 -0
  82. package/lib/runtime/components/react/pickerViewIndicator.tsx +34 -0
  83. package/lib/runtime/components/react/pickerViewMask.tsx +30 -0
  84. package/lib/runtime/components/react/types/{getInnerListeners.ts → getInnerListeners.d.ts} +4 -5
  85. package/lib/runtime/components/react/types/global.d.ts +12 -1
  86. package/lib/runtime/components/react/useAnimationHooks.ts +60 -15
  87. package/lib/runtime/components/react/utils.tsx +175 -71
  88. package/lib/runtime/components/web/mpx-checkbox.vue +1 -1
  89. package/lib/runtime/components/web/mpx-picker-view-column.vue +9 -4
  90. package/lib/runtime/components/web/mpx-web-view.vue +34 -20
  91. package/lib/runtime/optionProcessor.js +0 -22
  92. package/lib/style-compiler/index.js +1 -1
  93. package/lib/style-compiler/plugins/scope-id.js +30 -2
  94. package/lib/template-compiler/compiler.js +91 -39
  95. package/lib/utils/env.js +1 -6
  96. package/lib/utils/pre-process-json.js +9 -5
  97. package/lib/wxss/loader.js +15 -2
  98. package/package.json +4 -7
  99. package/lib/dependencies/AddEntryDependency.js +0 -24
  100. package/lib/runtime/components/react/dist/types/common.js +0 -1
  101. package/lib/runtime/components/react/dist/types/getInnerListeners.js +0 -1
  102. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +0 -527
  103. package/lib/runtime/components/react/mpx-swiper/index.tsx +0 -80
  104. package/lib/runtime/components/react/mpx-swiper/type.ts +0 -87
  105. package/lib/runtime/components/react/pickerOverlay.tsx +0 -32
  106. package/lib/runtime/components/tenon/getInnerListeners.js +0 -334
  107. package/lib/runtime/components/tenon/tenon-button.vue +0 -309
  108. package/lib/runtime/components/tenon/tenon-image.vue +0 -66
  109. package/lib/runtime/components/tenon/tenon-input.vue +0 -171
  110. package/lib/runtime/components/tenon/tenon-rich-text.vue +0 -26
  111. package/lib/runtime/components/tenon/tenon-scroll-view.vue +0 -127
  112. package/lib/runtime/components/tenon/tenon-switch.vue +0 -96
  113. package/lib/runtime/components/tenon/tenon-text.vue +0 -70
  114. package/lib/runtime/components/tenon/tenon-textarea.vue +0 -86
  115. package/lib/runtime/components/tenon/tenon-view.vue +0 -93
  116. package/lib/runtime/components/web/event.js +0 -105
  117. package/lib/runtime/optionProcessor.tenon.js +0 -84
  118. package/lib/style-compiler/plugins/hm.js +0 -20
  119. package/lib/tenon/index.js +0 -117
  120. package/lib/tenon/processJSON.js +0 -352
  121. package/lib/tenon/processScript.js +0 -203
  122. package/lib/tenon/processStyles.js +0 -21
  123. package/lib/tenon/processTemplate.js +0 -126
  124. package/lib/tenon/script-helper.js +0 -223
  125. package/lib/utils/get-relative-path.js +0 -25
  126. /package/lib/runtime/components/react/types/{common.ts → common.d.ts} +0 -0
@@ -11,7 +11,7 @@
11
11
  * ✘ scale-min
12
12
  * ✘ scale-max
13
13
  * ✘ scale-value
14
- * animation
14
+ * animation
15
15
  * ✔ bindchange
16
16
  * ✘ bindscale
17
17
  * ✔ htouchmove
@@ -19,12 +19,13 @@
19
19
  */
20
20
  import { useEffect, forwardRef, useContext, useCallback, useRef, useMemo, createElement } from 'react';
21
21
  import { StyleSheet } from 'react-native';
22
- import { getCustomEvent } from './getInnerListeners';
22
+ import useInnerProps, { getCustomEvent } from './getInnerListeners';
23
23
  import useNodesRef from './useNodesRef';
24
24
  import { MovableAreaContext } from './context';
25
- import { useTransformStyle, splitProps, splitStyle, HIDDEN_STYLE, wrapChildren, flatGesture, extendObject } from './utils';
25
+ import { useTransformStyle, splitProps, splitStyle, HIDDEN_STYLE, wrapChildren, flatGesture, extendObject, omit } from './utils';
26
26
  import { GestureDetector, Gesture } from 'react-native-gesture-handler';
27
27
  import Animated, { useSharedValue, useAnimatedStyle, withDecay, runOnJS, runOnUI, useAnimatedReaction, withSpring } from 'react-native-reanimated';
28
+ import { collectDataset, noop } from '@mpxjs/utils';
28
29
  const styles = StyleSheet.create({
29
30
  container: {
30
31
  position: 'absolute',
@@ -233,8 +234,7 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
233
234
  });
234
235
  props.onLayout && props.onLayout(e);
235
236
  };
236
- const extendEvent = useCallback((e) => {
237
- 'worklet';
237
+ const extendEvent = useCallback((e, obj) => {
238
238
  const touchArr = [e.changedTouches, e.allTouches];
239
239
  touchArr.forEach(touches => {
240
240
  touches && touches.forEach((item) => {
@@ -242,45 +242,65 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
242
242
  item.pageY = item.absoluteY;
243
243
  });
244
244
  });
245
- e.touches = e.allTouches;
245
+ Object.assign(e, {
246
+ touches: e.allTouches,
247
+ detail: {
248
+ x: e.changedTouches[0].absoluteX,
249
+ y: e.changedTouches[0].absoluteY
250
+ },
251
+ currentTarget: {
252
+ id: props.id || '',
253
+ dataset: collectDataset(props),
254
+ offsetLeft: 0,
255
+ offsetTop: 0
256
+ }
257
+ }, obj);
246
258
  }, []);
259
+ const triggerStartOnJS = ({ e }) => {
260
+ extendEvent(e);
261
+ bindtouchstart && bindtouchstart(e);
262
+ catchtouchstart && catchtouchstart(e);
263
+ };
264
+ const triggerMoveOnJS = ({ e, hasTouchmove, hasCatchTouchmove, touchEvent }) => {
265
+ extendEvent(e);
266
+ if (hasTouchmove) {
267
+ if (touchEvent === 'htouchmove') {
268
+ bindhtouchmove && bindhtouchmove(e);
269
+ }
270
+ else if (touchEvent === 'vtouchmove') {
271
+ bindvtouchmove && bindvtouchmove(e);
272
+ }
273
+ bindtouchmove && bindtouchmove(e);
274
+ }
275
+ if (hasCatchTouchmove) {
276
+ if (touchEvent === 'htouchmove') {
277
+ catchhtouchmove && catchhtouchmove(e);
278
+ }
279
+ else if (touchEvent === 'vtouchmove') {
280
+ catchvtouchmove && catchvtouchmove(e);
281
+ }
282
+ catchtouchmove && catchtouchmove(e);
283
+ }
284
+ };
285
+ const triggerEndOnJS = ({ e }) => {
286
+ extendEvent(e);
287
+ bindtouchend && bindtouchend(e);
288
+ catchtouchend && catchtouchend(e);
289
+ };
247
290
  const gesture = useMemo(() => {
248
- const handleTriggerStart = (e) => {
249
- 'worklet';
250
- extendEvent(e);
251
- bindtouchstart && runOnJS(bindtouchstart)(e);
252
- catchtouchstart && runOnJS(catchtouchstart)(e);
253
- };
254
291
  const handleTriggerMove = (e) => {
255
292
  'worklet';
256
- extendEvent(e);
257
293
  const hasTouchmove = !!bindhtouchmove || !!bindvtouchmove || !!bindtouchmove;
258
294
  const hasCatchTouchmove = !!catchhtouchmove || !!catchvtouchmove || !!catchtouchmove;
259
- if (hasTouchmove) {
260
- if (touchEvent.value === 'htouchmove') {
261
- bindhtouchmove && runOnJS(bindhtouchmove)(e);
262
- }
263
- else if (touchEvent.value === 'vtouchmove') {
264
- bindvtouchmove && runOnJS(bindvtouchmove)(e);
265
- }
266
- bindtouchmove && runOnJS(bindtouchmove)(e);
267
- }
268
- if (hasCatchTouchmove) {
269
- if (touchEvent.value === 'htouchmove') {
270
- catchhtouchmove && runOnJS(catchhtouchmove)(e);
271
- }
272
- else if (touchEvent.value === 'vtouchmove') {
273
- catchvtouchmove && runOnJS(catchvtouchmove)(e);
274
- }
275
- catchtouchmove && runOnJS(catchtouchmove)(e);
295
+ if (hasTouchmove || hasCatchTouchmove) {
296
+ runOnJS(triggerMoveOnJS)({
297
+ e,
298
+ touchEvent: touchEvent.value,
299
+ hasTouchmove,
300
+ hasCatchTouchmove
301
+ });
276
302
  }
277
303
  };
278
- const handleTriggerEnd = (e) => {
279
- 'worklet';
280
- extendEvent(e);
281
- bindtouchend && runOnJS(bindtouchend)(e);
282
- catchtouchend && runOnJS(catchtouchend)(e);
283
- };
284
304
  const gesturePan = Gesture.Pan()
285
305
  .onTouchesDown((e) => {
286
306
  'worklet';
@@ -290,12 +310,14 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
290
310
  x: changedTouches.x,
291
311
  y: changedTouches.y
292
312
  };
293
- handleTriggerStart(e);
313
+ if (bindtouchstart || catchtouchstart) {
314
+ runOnJS(triggerStartOnJS)({ e });
315
+ }
294
316
  })
295
317
  .onTouchesMove((e) => {
296
318
  'worklet';
297
- isMoving.value = true;
298
319
  const changedTouches = e.changedTouches[0] || { x: 0, y: 0 };
320
+ isMoving.value = true;
299
321
  if (isFirstTouch.value) {
300
322
  touchEvent.value = Math.abs(changedTouches.x - startPosition.value.x) > Math.abs(changedTouches.y - startPosition.value.y) ? 'htouchmove' : 'vtouchmove';
301
323
  isFirstTouch.value = false;
@@ -330,7 +352,9 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
330
352
  'worklet';
331
353
  isFirstTouch.value = true;
332
354
  isMoving.value = false;
333
- handleTriggerEnd(e);
355
+ if (bindtouchend || catchtouchend) {
356
+ runOnJS(triggerEndOnJS)({ e });
357
+ }
334
358
  if (disabled)
335
359
  return;
336
360
  if (!inertia) {
@@ -355,9 +379,9 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
355
379
  })
356
380
  .onFinalize((e) => {
357
381
  'worklet';
382
+ isMoving.value = false;
358
383
  if (!inertia || disabled || !animation)
359
384
  return;
360
- isMoving.value = false;
361
385
  if (direction === 'horizontal' || direction === 'all') {
362
386
  xInertialMotion.value = true;
363
387
  offsetX.value = withDecay({
@@ -396,31 +420,42 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
396
420
  ]
397
421
  };
398
422
  });
399
- const injectCatchEvent = (props) => {
400
- const eventHandlers = {};
401
- const catchEventList = [
402
- { name: 'onTouchStart', value: ['catchtouchstart'] },
403
- { name: 'onTouchMove', value: ['catchtouchmove', 'catchvtouchmove', 'catchhtouchmove'] },
404
- { name: 'onTouchEnd', value: ['catchtouchend'] }
423
+ const rewriteCatchEvent = () => {
424
+ const handlers = {};
425
+ const events = [
426
+ { type: 'touchstart' },
427
+ { type: 'touchmove', alias: ['vtouchmove', 'htouchmove'] },
428
+ { type: 'touchend' }
405
429
  ];
406
- catchEventList.forEach(event => {
407
- event.value.forEach(name => {
408
- if (props[name] && !eventHandlers[event.name]) {
409
- eventHandlers[event.name] = (e) => {
410
- e.stopPropagation();
411
- };
412
- }
413
- });
430
+ events.forEach(({ type, alias = [] }) => {
431
+ const hasCatchEvent = props[`catch${type}`] ||
432
+ alias.some(name => props[`catch${name}`]);
433
+ if (hasCatchEvent)
434
+ handlers[`catch${type}`] = noop;
414
435
  });
415
- return eventHandlers;
436
+ return handlers;
416
437
  };
417
- const catchEventHandlers = injectCatchEvent(props);
418
438
  const layoutStyle = !hasLayoutRef.current && hasSelfPercent ? HIDDEN_STYLE : {};
419
- return createElement(GestureDetector, { gesture: gesture }, createElement(Animated.View, extendObject({
439
+ // bind 相关 touch 事件直接由 gesture 触发,无须重复挂载
440
+ // catch 相关 touch 事件需要重写并通过 useInnerProps 注入阻止冒泡逻辑
441
+ const filterProps = omit(props, [
442
+ 'bindtouchstart',
443
+ 'bindtouchmove',
444
+ 'bindvtouchmove',
445
+ 'bindhtouchmove',
446
+ 'bindtouchend',
447
+ 'catchtouchstart',
448
+ 'catchtouchmove',
449
+ 'catchvtouchmove',
450
+ 'catchhtouchmove',
451
+ 'catchtouchend'
452
+ ]);
453
+ const innerProps = useInnerProps(filterProps, extendObject({
420
454
  ref: nodeRef,
421
455
  onLayout: onLayout,
422
456
  style: [innerStyle, animatedStyles, layoutStyle]
423
- }, catchEventHandlers), wrapChildren(props, {
457
+ }, rewriteCatchEvent()));
458
+ return createElement(GestureDetector, { gesture: gesture }, createElement(Animated.View, innerProps, wrapChildren(props, {
424
459
  hasVarDec,
425
460
  varContext: varContextRef.current,
426
461
  textStyle,
@@ -3,7 +3,7 @@ import useInnerProps from './getInnerListeners';
3
3
  import { redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy';
4
4
  import MpxView from './mpx-view';
5
5
  const _Navigator = forwardRef((props, ref) => {
6
- const { children, 'open-type': openType, url, delta } = props;
6
+ const { children, 'open-type': openType, url = '', delta } = props;
7
7
  const handleClick = useCallback(() => {
8
8
  switch (openType) {
9
9
  case 'navigateBack':
@@ -1,9 +1,10 @@
1
1
  import React, { useEffect } from 'react';
2
2
  import Reanimated, { Extrapolation, interpolate, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
3
- import { wrapChildren, extendObject } from './utils';
3
+ import { extendObject } from './utils';
4
4
  import { createFaces } from './pickerFaces';
5
- import { usePickerViewColumnAnimationContext } from './pickerVIewContext';
6
- const _PickerViewColumnItem = ({ item, index, itemHeight, itemWidth, textStyleFromParent, textStyle, hasVarDec, varContext, textProps, visibleCount, onItemLayout }) => {
5
+ import { usePickerViewColumnAnimationContext, usePickerViewStyleContext } from './pickerVIewContext';
6
+ const PickerViewColumnItem = ({ item, index, itemHeight, itemWidth = '100%', textStyle, textProps, visibleCount, onItemLayout }) => {
7
+ const textStyleFromAncestor = usePickerViewStyleContext();
7
8
  const offsetYShared = usePickerViewColumnAnimationContext();
8
9
  const facesShared = useSharedValue(createFaces(itemHeight, visibleCount));
9
10
  useEffect(() => {
@@ -14,8 +15,8 @@ const _PickerViewColumnItem = ({ item, index, itemHeight, itemWidth, textStyleFr
14
15
  return {
15
16
  opacity: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.opacity), Extrapolation.CLAMP),
16
17
  transform: [
17
- { rotateX: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.deg), Extrapolation.CLAMP) + 'deg' },
18
18
  { translateY: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.offsetY), Extrapolation.EXTEND) },
19
+ { rotateX: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.deg), Extrapolation.CLAMP) + 'deg' },
19
20
  { scale: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.scale), Extrapolation.EXTEND) }
20
21
  ]
21
22
  };
@@ -23,17 +24,12 @@ const _PickerViewColumnItem = ({ item, index, itemHeight, itemWidth, textStyleFr
23
24
  const strKey = `picker-column-item-${index}`;
24
25
  const restProps = index === 0 ? { onLayout: onItemLayout } : {};
25
26
  const itemProps = extendObject({
26
- style: extendObject({ height: itemHeight, width: '100%' }, textStyleFromParent, textStyle, item.props.style)
27
- }, restProps);
27
+ style: extendObject({ height: itemHeight, width: '100%' }, textStyleFromAncestor, textStyle, item.props.style)
28
+ }, textProps, restProps);
28
29
  const realItem = React.cloneElement(item, itemProps);
29
30
  return (<Reanimated.View key={strKey} style={[{ height: itemHeight, width: itemWidth }, animatedStyles]}>
30
- {wrapChildren({ children: realItem }, {
31
- hasVarDec,
32
- varContext,
33
- textStyle,
34
- textProps
35
- })}
31
+ {realItem}
36
32
  </Reanimated.View>);
37
33
  };
38
- _PickerViewColumnItem.displayName = 'MpxPickerViewColumnItem';
39
- export default _PickerViewColumnItem;
34
+ PickerViewColumnItem.displayName = 'MpxPickerViewColumnItem';
35
+ export default PickerViewColumnItem;
@@ -1,34 +1,31 @@
1
1
  import React, { forwardRef, useRef, useState, useMemo, useEffect, useCallback } from 'react';
2
- import { Platform, SafeAreaView, StyleSheet } from 'react-native';
2
+ import { StyleSheet, View } from 'react-native';
3
3
  import Reanimated, { useAnimatedRef, useScrollViewOffset } from 'react-native-reanimated';
4
- import { vibrateShort } from '@mpxjs/api-proxy';
5
- import { useTransformStyle, splitStyle, splitProps, useLayout, usePrevious } from './utils';
4
+ import { useTransformStyle, splitStyle, splitProps, useLayout, usePrevious, isAndroid, isIOS } from './utils';
6
5
  import useNodesRef from './useNodesRef';
7
- import PickerOverlay from './pickerViewOverlay';
6
+ import PickerIndicator from './pickerViewIndicator';
8
7
  import PickerMask from './pickerViewMask';
9
8
  import MpxPickerVIewColumnItem from './mpx-picker-view-column-item';
10
9
  import { PickerViewColumnAnimationContext } from './pickerVIewContext';
11
10
  const visibleCount = 5;
12
11
  const _PickerViewColumn = forwardRef((props, ref) => {
13
- const { columnData, columnIndex, columnStyle, initialIndex, onSelectChange, style, wrapperStyle, pickerMaskStyle, pickerOverlayStyle, 'enable-var': enableVar, 'external-var-context': externalVarContext } = props;
14
- const { normalStyle, hasVarDec, varContextRef, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, { enableVar, externalVarContext });
15
- const { textStyle: textStyleFromParent = {} } = splitStyle(columnStyle);
12
+ const { columnData, columnIndex, initialIndex, onSelectChange, style, wrapperStyle, pickerMaskStyle, pickerIndicatorStyle, 'enable-var': enableVar, 'external-var-context': externalVarContext } = props;
13
+ const { normalStyle, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, { enableVar, externalVarContext });
16
14
  const { textStyle = {} } = splitStyle(normalStyle);
17
- const { textProps } = splitProps(props);
15
+ const { textProps = {} } = splitProps(props);
18
16
  const scrollViewRef = useAnimatedRef();
19
17
  const offsetYShared = useScrollViewOffset(scrollViewRef);
20
18
  useNodesRef(props, ref, scrollViewRef, {
21
19
  style: normalStyle
22
20
  });
23
21
  const { height: pickerH, itemHeight } = wrapperStyle;
24
- const [scrollViewWidth, setScrollViewWidth] = useState('100%');
25
- const [itemRawW, setItemRawW] = useState('100%');
26
22
  const [itemRawH, setItemRawH] = useState(itemHeight);
27
23
  const maxIndex = useMemo(() => columnData.length - 1, [columnData]);
28
- const maxScrollViewWidth = useRef(-1);
29
24
  const prevScrollingInfo = useRef({ index: initialIndex, y: 0 });
30
25
  const touching = useRef(false);
31
26
  const scrolling = useRef(false);
27
+ const timerResetPosition = useRef(null);
28
+ const timerScrollTo = useRef(null);
32
29
  const activeIndex = useRef(initialIndex);
33
30
  const prevIndex = usePrevious(initialIndex);
34
31
  const prevMaxIndex = usePrevious(maxIndex);
@@ -39,13 +36,8 @@ const _PickerViewColumn = forwardRef((props, ref) => {
39
36
  setHeight,
40
37
  nodeRef: scrollViewRef
41
38
  });
42
- // console.log('[mpx-picker-view-column], render ---> columnIndex=', columnIndex, 'initialIndex=', initialIndex, 'columnData=', columnData.length)
43
- // const initialOffset = useMemo(() => ({
44
- // x: 0,
45
- // y: itemRawH * initialIndex
46
- // }), [itemRawH])
47
- const snapToOffsets = useMemo(() => columnData.map((_, i) => i * itemRawH), [columnData, itemRawH]);
48
- const paddingHeight = useMemo(() => Math.round((pickerH - itemRawH) / 2), [pickerH, itemRawH]);
39
+ const paddingHeight = useMemo(() => Math.round((pickerH - itemHeight) / 2), [pickerH, itemHeight]);
40
+ const snapToOffsets = useMemo(() => Array.from({ length: maxIndex + 1 }, (_, i) => i * itemRawH), [maxIndex, itemRawH]);
49
41
  const contentContainerStyle = useMemo(() => {
50
42
  return [{ paddingVertical: paddingHeight }];
51
43
  }, [paddingHeight]);
@@ -53,6 +45,24 @@ const _PickerViewColumn = forwardRef((props, ref) => {
53
45
  const calc = Math.round(y / itemRawH);
54
46
  return Math.max(0, Math.min(calc, maxIndex));
55
47
  }, [itemRawH, maxIndex]);
48
+ const clearTimerResetPosition = useCallback(() => {
49
+ if (timerResetPosition.current) {
50
+ clearTimeout(timerResetPosition.current);
51
+ timerResetPosition.current = null;
52
+ }
53
+ }, []);
54
+ const clearTimerScrollTo = useCallback(() => {
55
+ if (timerScrollTo.current) {
56
+ clearTimeout(timerScrollTo.current);
57
+ timerScrollTo.current = null;
58
+ }
59
+ }, []);
60
+ useEffect(() => {
61
+ return () => {
62
+ clearTimerResetPosition();
63
+ clearTimerScrollTo();
64
+ };
65
+ }, []);
56
66
  useEffect(() => {
57
67
  if (!scrollViewRef.current ||
58
68
  !itemRawH ||
@@ -64,80 +74,82 @@ const _PickerViewColumn = forwardRef((props, ref) => {
64
74
  maxIndex !== prevMaxIndex) {
65
75
  return;
66
76
  }
67
- // console.log('[mpx-picker-view-column], useEffect ---> columnIndex=', columnIndex, 'initialIndex=', initialIndex, 'y=', itemRawH * initialIndex, `${scrollViewRef.current?.scrollTo}`)
68
- setTimeout(() => {
77
+ clearTimerScrollTo();
78
+ timerScrollTo.current = setTimeout(() => {
69
79
  scrollViewRef.current?.scrollTo({
70
80
  x: 0,
71
- y: itemRawH * initialIndex,
81
+ y: initialIndex * itemRawH,
72
82
  animated: false
73
83
  });
74
- }, 0);
84
+ }, isAndroid ? 200 : 0);
75
85
  activeIndex.current = initialIndex;
76
- }, [itemRawH, initialIndex]);
77
- const onContentSizeChange = (_w, h) => {
78
- const y = itemRawH * initialIndex;
79
- // console.log('[mpx-picker-view-column], onContentSizeChange --->', 'columnIndex=', columnIndex, '_w=', _w, 'h=', h, 'y=', y)
86
+ }, [itemRawH, maxIndex, initialIndex]);
87
+ const onContentSizeChange = useCallback((_w, h) => {
88
+ const y = initialIndex * itemRawH;
80
89
  if (y <= h) {
81
- setTimeout(() => {
90
+ clearTimerScrollTo();
91
+ timerScrollTo.current = setTimeout(() => {
82
92
  scrollViewRef.current?.scrollTo({ x: 0, y, animated: false });
83
93
  }, 0);
84
94
  }
85
- };
86
- const onScrollViewLayout = (e) => {
87
- const { width } = e.nativeEvent.layout;
88
- const widthInt = Math.ceil(width);
89
- // console.log('[mpx-picker-view-column], onScrollViewLayout --->', 'columnIndex=', columnIndex, 'widthInt=', widthInt, 'scrollViewWidth=', scrollViewWidth)
90
- if (widthInt !== scrollViewWidth) {
91
- const maxW = maxScrollViewWidth.current;
92
- if (maxW !== -1 && widthInt > maxW) {
93
- return;
94
- }
95
- if (maxW === -1) {
96
- maxScrollViewWidth.current = Math.ceil(widthInt * 1.5);
97
- }
98
- setScrollViewWidth(widthInt);
95
+ }, [itemRawH, initialIndex]);
96
+ const onItemLayout = useCallback((e) => {
97
+ const { height: rawH } = e.nativeEvent.layout;
98
+ const roundedH = Math.round(rawH);
99
+ if (roundedH && roundedH !== itemRawH) {
100
+ setItemRawH(roundedH);
99
101
  }
100
- if (itemRawW === '100%') {
101
- setItemRawW(widthInt);
102
+ }, [itemRawH]);
103
+ const resetScrollPosition = useCallback((y) => {
104
+ if (touching.current || scrolling.current) {
105
+ return;
102
106
  }
103
- };
104
- const onItemLayout = (e) => {
105
- const { height: rawH } = e.nativeEvent.layout;
106
- if (rawH && itemRawH !== rawH) {
107
- setItemRawH(rawH);
107
+ scrolling.current = true;
108
+ const targetIndex = getIndex(y);
109
+ scrollViewRef.current?.scrollTo({ x: 0, y: targetIndex * itemRawH, animated: false });
110
+ }, [itemRawH, getIndex]);
111
+ const onMomentumScrollBegin = useCallback(() => {
112
+ isIOS && clearTimerResetPosition();
113
+ scrolling.current = true;
114
+ }, []);
115
+ const onMomentumScrollEnd = useCallback((e) => {
116
+ scrolling.current = false;
117
+ const { y: scrollY } = e.nativeEvent.contentOffset;
118
+ if (isIOS && scrollY % itemRawH !== 0) {
119
+ return resetScrollPosition(scrollY);
108
120
  }
109
- };
110
- const onTouchStart = () => {
121
+ const calcIndex = getIndex(scrollY);
122
+ if (calcIndex !== activeIndex.current) {
123
+ activeIndex.current = calcIndex;
124
+ onSelectChange(calcIndex);
125
+ }
126
+ }, [itemRawH, getIndex, onSelectChange, resetScrollPosition]);
127
+ const onScrollBeginDrag = useCallback(() => {
128
+ isIOS && clearTimerResetPosition();
111
129
  touching.current = true;
112
130
  prevScrollingInfo.current = {
113
131
  index: activeIndex.current,
114
132
  y: activeIndex.current * itemRawH
115
133
  };
116
- };
117
- const onTouchEnd = () => {
134
+ }, [itemRawH]);
135
+ const onScrollEndDrag = useCallback((e) => {
118
136
  touching.current = false;
119
- };
120
- const onTouchCancel = () => {
121
- touching.current = false;
122
- };
123
- const onMomentumScrollBegin = () => {
124
- scrolling.current = true;
125
- };
126
- const onMomentumScrollEnd = (e) => {
127
- scrolling.current = false;
128
- if (!itemRawH) {
129
- return;
130
- }
131
- const { y: scrollY } = e.nativeEvent.contentOffset;
132
- let calcIndex = Math.round(scrollY / itemRawH);
133
- activeIndex.current = calcIndex;
134
- if (calcIndex !== initialIndex) {
135
- calcIndex = Math.max(0, Math.min(calcIndex, maxIndex)) || 0;
136
- onSelectChange(calcIndex);
137
+ if (isIOS) {
138
+ const { y } = e.nativeEvent.contentOffset;
139
+ if (y % itemRawH === 0) {
140
+ onMomentumScrollEnd({ nativeEvent: { contentOffset: { y } } });
141
+ }
142
+ else if (y > 0 && y < snapToOffsets[maxIndex]) {
143
+ timerResetPosition.current = setTimeout(() => {
144
+ resetScrollPosition(y);
145
+ }, 10);
146
+ }
137
147
  }
138
- };
139
- const onScroll = (e) => {
140
- if (Platform.OS === 'android') {
148
+ }, [itemRawH, maxIndex, snapToOffsets, onMomentumScrollEnd, resetScrollPosition]);
149
+ const onScroll = useCallback((e) => {
150
+ // 全局注册的振动触感 hook
151
+ const pickerVibrate = global.__mpx?.config?.rnConfig?.pickerVibrate;
152
+ if (typeof pickerVibrate !== 'function') {
141
153
  return;
142
154
  }
143
155
  const { y } = e.nativeEvent.contentOffset;
@@ -150,28 +162,29 @@ const _PickerViewColumn = forwardRef((props, ref) => {
150
162
  index: currentId,
151
163
  y: currentId * itemRawH
152
164
  };
153
- vibrateShort({ type: 'selection' });
165
+ // vibrateShort({ type: 'selection' })
166
+ pickerVibrate();
154
167
  }
155
168
  }
156
169
  }
157
- };
170
+ }, [itemRawH, getIndex]);
158
171
  const renderInnerchild = () => columnData.map((item, index) => {
159
- return (<MpxPickerVIewColumnItem key={index} item={item} index={index} itemHeight={itemHeight} itemWidth={itemRawW} textStyleFromParent={textStyleFromParent} textStyle={textStyle} hasVarDec={hasVarDec} varContext={varContextRef.current} textProps={textProps} visibleCount={visibleCount} onItemLayout={onItemLayout}/>);
172
+ return (<MpxPickerVIewColumnItem key={index} item={item} index={index} itemHeight={itemHeight} textStyle={textStyle} textProps={textProps} visibleCount={visibleCount} onItemLayout={onItemLayout}/>);
160
173
  });
161
174
  const renderScollView = () => {
162
175
  return (<PickerViewColumnAnimationContext.Provider value={offsetYShared}>
163
- <Reanimated.ScrollView ref={scrollViewRef} bounces={true} horizontal={false} nestedScrollEnabled={true} removeClippedSubviews={false} showsVerticalScrollIndicator={false} showsHorizontalScrollIndicator={false} scrollEventThrottle={16} {...layoutProps} style={[{ width: scrollViewWidth }]} decelerationRate="fast" snapToOffsets={snapToOffsets} onScroll={onScroll} onLayout={onScrollViewLayout} onTouchStart={onTouchStart} onTouchEnd={onTouchEnd} onTouchCancel={onTouchCancel} onMomentumScrollBegin={onMomentumScrollBegin} onMomentumScrollEnd={onMomentumScrollEnd} onContentSizeChange={onContentSizeChange} contentContainerStyle={contentContainerStyle}>
176
+ <Reanimated.ScrollView ref={scrollViewRef} bounces={true} horizontal={false} nestedScrollEnabled={true} removeClippedSubviews={false} showsVerticalScrollIndicator={false} showsHorizontalScrollIndicator={false} scrollEventThrottle={16} {...layoutProps} style={[{ width: '100%' }]} decelerationRate="fast" snapToOffsets={snapToOffsets} onScroll={onScroll} onScrollBeginDrag={onScrollBeginDrag} onScrollEndDrag={onScrollEndDrag} onMomentumScrollBegin={onMomentumScrollBegin} onMomentumScrollEnd={onMomentumScrollEnd} onContentSizeChange={onContentSizeChange} contentContainerStyle={contentContainerStyle}>
164
177
  {renderInnerchild()}
165
178
  </Reanimated.ScrollView>
166
179
  </PickerViewColumnAnimationContext.Provider>);
167
180
  };
168
- const renderOverlay = () => (<PickerOverlay itemHeight={itemHeight} overlayItemStyle={pickerOverlayStyle}/>);
181
+ const renderIndicator = () => (<PickerIndicator itemHeight={itemHeight} indicatorItemStyle={pickerIndicatorStyle}/>);
169
182
  const renderMask = () => (<PickerMask itemHeight={itemHeight} maskContainerStyle={pickerMaskStyle}/>);
170
- return (<SafeAreaView style={[styles.wrapper, normalStyle]}>
183
+ return (<View style={[styles.wrapper, normalStyle]}>
171
184
  {renderScollView()}
172
185
  {renderMask()}
173
- {renderOverlay()}
174
- </SafeAreaView>);
186
+ {renderIndicator()}
187
+ </View>);
175
188
  });
176
189
  const styles = StyleSheet.create({
177
190
  wrapper: { display: 'flex', flex: 1 }
@@ -2,7 +2,8 @@ import { View } from 'react-native';
2
2
  import React, { forwardRef, useRef } from 'react';
3
3
  import useInnerProps, { getCustomEvent } from './getInnerListeners';
4
4
  import useNodesRef from './useNodesRef';
5
- import { useLayout, splitProps, splitStyle, wrapChildren, parseInlineStyle, useTransformStyle, extendObject } from './utils';
5
+ import { useLayout, splitProps, splitStyle, wrapChildren, useTransformStyle, extendObject } from './utils';
6
+ import { PickerViewStyleContext } from './pickerVIewContext';
6
7
  const styles = {
7
8
  wrapper: {
8
9
  display: 'flex',
@@ -15,16 +16,13 @@ const styles = {
15
16
  };
16
17
  const DefaultPickerItemH = 36;
17
18
  const _PickerView = forwardRef((props, ref) => {
18
- const { children, value = [], bindchange, style, 'enable-var': enableVar, 'external-var-context': externalVarContext } = props;
19
- const indicatorStyle = parseInlineStyle(props['indicator-style']);
20
- const pickerMaskStyle = parseInlineStyle(props['mask-style']);
21
- const { height: indicatorH, ...pickerOverlayStyle } = indicatorStyle;
19
+ const { children, value = [], bindchange, style, 'indicator-style': indicatorStyle = {}, 'mask-style': pickerMaskStyle = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext } = props;
20
+ const { height: indicatorH, ...pickerIndicatorStyle } = indicatorStyle;
22
21
  const nodeRef = useRef(null);
23
22
  const cloneRef = useRef(null);
24
23
  const activeValueRef = useRef(value);
25
24
  activeValueRef.current = value.slice();
26
25
  const snapActiveValueRef = useRef(null);
27
- console.log('[mpx-picker-view] value=', value, Date.now());
28
26
  const { normalStyle, hasVarDec, varContextRef, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, { enableVar, externalVarContext });
29
27
  useNodesRef(props, ref, nodeRef, {
30
28
  style: normalStyle
@@ -35,8 +33,7 @@ const _PickerView = forwardRef((props, ref) => {
35
33
  const onSelectChange = (columnIndex, selectedIndex) => {
36
34
  const activeValue = activeValueRef.current;
37
35
  activeValue[columnIndex] = selectedIndex;
38
- console.log('[mpx-picker-view], onSelectChange ---> columnIndex=', columnIndex, 'selectedIndex=', selectedIndex, 'activeValue=', activeValue);
39
- const eventData = getCustomEvent('change', {}, { detail: { value: activeValue, source: 'change' }, layoutRef });
36
+ const eventData = getCustomEvent('change', {}, { detail: { value: activeValue.slice(), source: 'change' }, layoutRef });
40
37
  bindchange?.(eventData);
41
38
  snapActiveValueRef.current = activeValueRef.current;
42
39
  };
@@ -45,8 +42,7 @@ const _PickerView = forwardRef((props, ref) => {
45
42
  };
46
43
  const onInitialChange = (isInvalid, value) => {
47
44
  if (isInvalid || !snapActiveValueRef.current || hasDiff(snapActiveValueRef.current, value)) {
48
- console.log('[mpx-picker-view], onInitialChange-1 ===> value=', value);
49
- const eventData = getCustomEvent('change', {}, { detail: { value, source: 'change' }, layoutRef });
45
+ const eventData = getCustomEvent('change', {}, { detail: { value: value.slice(), source: 'change' }, layoutRef });
50
46
  bindchange?.(eventData);
51
47
  snapActiveValueRef.current = value.slice();
52
48
  }
@@ -58,7 +54,13 @@ const _PickerView = forwardRef((props, ref) => {
58
54
  overflow: 'hidden'
59
55
  }),
60
56
  layoutProps
61
- }), ['enable-offset'], { layoutRef });
57
+ }), [
58
+ 'enable-offset',
59
+ 'indicator-style',
60
+ 'indicator-class',
61
+ 'mask-style',
62
+ 'mask-class'
63
+ ], { layoutRef });
62
64
  const renderColumn = (child, index, columnData, initialIndex) => {
63
65
  const childProps = child?.props || {};
64
66
  const wrappedProps = extendObject({}, childProps, {
@@ -70,10 +72,9 @@ const _PickerView = forwardRef((props, ref) => {
70
72
  height: normalStyle?.height || DefaultPickerItemH,
71
73
  itemHeight: indicatorH || DefaultPickerItemH
72
74
  },
73
- columnStyle: normalStyle,
74
75
  onSelectChange: onSelectChange.bind(null, index),
75
76
  initialIndex,
76
- pickerOverlayStyle,
77
+ pickerIndicatorStyle,
77
78
  pickerMaskStyle
78
79
  });
79
80
  const realElement = React.cloneElement(child, wrappedProps);
@@ -114,9 +115,11 @@ const _PickerView = forwardRef((props, ref) => {
114
115
  onInitialChange(isInvalid, validValue);
115
116
  return renderColumns;
116
117
  };
117
- return (<View {...innerProps}>
118
- <View style={[styles.wrapper]}>{renderPickerColumns()}</View>
119
- </View>);
118
+ return (<PickerViewStyleContext.Provider value={textStyle}>
119
+ <View {...innerProps}>
120
+ <View style={[styles.wrapper]}>{renderPickerColumns()}</View>
121
+ </View>
122
+ </PickerViewStyleContext.Provider>);
120
123
  });
121
124
  _PickerView.displayName = 'MpxPickerView';
122
125
  export default _PickerView;