@mpxjs/webpack-plugin 2.9.67 → 2.9.69-beta.0

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 (140) hide show
  1. package/lib/index.js +13 -8
  2. package/lib/platform/json/wx/index.js +21 -8
  3. package/lib/platform/style/wx/index.js +35 -38
  4. package/lib/platform/template/wx/component-config/canvas.js +8 -0
  5. package/lib/platform/template/wx/component-config/fix-component-name.js +15 -12
  6. package/lib/platform/template/wx/component-config/index.js +1 -1
  7. package/lib/platform/template/wx/component-config/input.js +1 -1
  8. package/lib/platform/template/wx/component-config/rich-text.js +8 -0
  9. package/lib/platform/template/wx/component-config/swiper.js +1 -1
  10. package/lib/platform/template/wx/component-config/textarea.js +1 -1
  11. package/lib/platform/template/wx/component-config/unsupported.js +1 -1
  12. package/lib/react/processStyles.js +14 -4
  13. package/lib/react/processTemplate.js +3 -0
  14. package/lib/resolver/AddModePlugin.js +8 -8
  15. package/lib/runtime/components/react/context.ts +6 -0
  16. package/lib/runtime/components/react/dist/context.js +2 -0
  17. package/lib/runtime/components/react/dist/event.config.js +24 -24
  18. package/lib/runtime/components/react/dist/getInnerListeners.js +183 -174
  19. package/lib/runtime/components/react/dist/mpx-button.jsx +77 -49
  20. package/lib/runtime/components/react/dist/mpx-canvas/Bus.js +60 -0
  21. package/lib/runtime/components/react/dist/mpx-canvas/CanvasGradient.js +15 -0
  22. package/lib/runtime/components/react/dist/mpx-canvas/CanvasRenderingContext2D.js +84 -0
  23. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +87 -0
  24. package/lib/runtime/components/react/dist/mpx-canvas/ImageData.js +15 -0
  25. package/lib/runtime/components/react/dist/mpx-canvas/constructorsRegistry.js +28 -0
  26. package/lib/runtime/components/react/dist/mpx-canvas/html.js +343 -0
  27. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +232 -0
  28. package/lib/runtime/components/react/dist/mpx-canvas/utils.jsx +89 -0
  29. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +13 -19
  30. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +29 -38
  31. package/lib/runtime/components/react/dist/mpx-form.jsx +16 -19
  32. package/lib/runtime/components/react/dist/mpx-icon.jsx +8 -16
  33. package/lib/runtime/components/react/dist/mpx-image.jsx +291 -0
  34. package/lib/runtime/components/react/dist/mpx-input.jsx +54 -27
  35. package/lib/runtime/components/react/dist/mpx-label.jsx +15 -22
  36. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +13 -16
  37. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +13 -13
  38. package/lib/runtime/components/react/dist/mpx-navigator.jsx +2 -4
  39. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +6 -2
  40. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +5 -3
  41. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +6 -2
  42. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +6 -2
  43. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +6 -2
  44. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +10 -15
  45. package/lib/runtime/components/react/dist/mpx-picker-view-column-item.jsx +39 -0
  46. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +160 -88
  47. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +80 -121
  48. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +11 -19
  49. package/lib/runtime/components/react/dist/mpx-radio.jsx +27 -42
  50. package/lib/runtime/components/react/dist/mpx-rich-text/html.js +39 -0
  51. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +63 -0
  52. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +6 -4
  53. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +47 -41
  54. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +11 -0
  55. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +4 -2
  56. package/lib/runtime/components/react/dist/mpx-swiper.jsx +606 -0
  57. package/lib/runtime/components/react/dist/mpx-switch.jsx +20 -10
  58. package/lib/runtime/components/react/dist/mpx-text.jsx +11 -10
  59. package/lib/runtime/components/react/dist/mpx-textarea.jsx +8 -3
  60. package/lib/runtime/components/react/dist/mpx-view.jsx +65 -61
  61. package/lib/runtime/components/react/dist/mpx-web-view.jsx +112 -35
  62. package/lib/runtime/components/react/dist/pickerFaces.js +81 -0
  63. package/lib/runtime/components/react/dist/pickerVIewContext.js +9 -0
  64. package/lib/runtime/components/react/dist/pickerViewMask.jsx +18 -0
  65. package/lib/runtime/components/react/dist/pickerViewOverlay.jsx +23 -0
  66. package/lib/runtime/components/react/dist/useAnimationHooks.js +35 -9
  67. package/lib/runtime/components/react/dist/utils.jsx +70 -23
  68. package/lib/runtime/components/react/event.config.ts +25 -26
  69. package/lib/runtime/components/react/getInnerListeners.ts +237 -199
  70. package/lib/runtime/components/react/mpx-button.tsx +104 -57
  71. package/lib/runtime/components/react/mpx-canvas/Bus.ts +70 -0
  72. package/lib/runtime/components/react/mpx-canvas/CanvasGradient.ts +18 -0
  73. package/lib/runtime/components/react/mpx-canvas/CanvasRenderingContext2D.ts +87 -0
  74. package/lib/runtime/components/react/mpx-canvas/Image.ts +102 -0
  75. package/lib/runtime/components/react/mpx-canvas/ImageData.ts +23 -0
  76. package/lib/runtime/components/react/mpx-canvas/constructorsRegistry.ts +38 -0
  77. package/lib/runtime/components/react/mpx-canvas/html.ts +343 -0
  78. package/lib/runtime/components/react/mpx-canvas/index.tsx +296 -0
  79. package/lib/runtime/components/react/mpx-canvas/utils.tsx +150 -0
  80. package/lib/runtime/components/react/mpx-checkbox-group.tsx +28 -25
  81. package/lib/runtime/components/react/mpx-checkbox.tsx +48 -49
  82. package/lib/runtime/components/react/mpx-form.tsx +25 -28
  83. package/lib/runtime/components/react/mpx-icon.tsx +12 -17
  84. package/lib/runtime/components/react/mpx-image.tsx +436 -0
  85. package/lib/runtime/components/react/mpx-input.tsx +77 -57
  86. package/lib/runtime/components/react/mpx-label.tsx +26 -27
  87. package/lib/runtime/components/react/mpx-movable-area.tsx +18 -23
  88. package/lib/runtime/components/react/mpx-movable-view.tsx +21 -25
  89. package/lib/runtime/components/react/mpx-navigator.tsx +2 -8
  90. package/lib/runtime/components/react/mpx-picker/date.tsx +5 -2
  91. package/lib/runtime/components/react/mpx-picker/index.tsx +3 -2
  92. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +5 -2
  93. package/lib/runtime/components/react/mpx-picker/region.tsx +5 -2
  94. package/lib/runtime/components/react/mpx-picker/selector.tsx +5 -2
  95. package/lib/runtime/components/react/mpx-picker/time.tsx +10 -15
  96. package/lib/runtime/components/react/mpx-picker/type.ts +48 -43
  97. package/lib/runtime/components/react/mpx-picker-view-column.tsx +236 -104
  98. package/lib/runtime/components/react/mpx-picker-view.tsx +132 -122
  99. package/lib/runtime/components/react/mpx-radio-group.tsx +24 -27
  100. package/lib/runtime/components/react/mpx-radio.tsx +45 -54
  101. package/lib/runtime/components/react/mpx-rich-text/html.ts +40 -0
  102. package/lib/runtime/components/react/mpx-rich-text/index.tsx +121 -0
  103. package/lib/runtime/components/react/mpx-root-portal.tsx +3 -5
  104. package/lib/runtime/components/react/mpx-scroll-view.tsx +72 -71
  105. package/lib/runtime/components/react/mpx-simple-text.tsx +18 -0
  106. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +4 -2
  107. package/lib/runtime/components/react/mpx-swiper-item.tsx +3 -2
  108. package/lib/runtime/components/react/mpx-switch.tsx +29 -23
  109. package/lib/runtime/components/react/mpx-text.tsx +14 -18
  110. package/lib/runtime/components/react/mpx-textarea.tsx +11 -10
  111. package/lib/runtime/components/react/mpx-view.tsx +92 -76
  112. package/lib/runtime/components/react/mpx-web-view.tsx +116 -54
  113. package/lib/runtime/components/react/pickerFaces.ts +104 -0
  114. package/lib/runtime/components/react/pickerOverlay.tsx +32 -0
  115. package/lib/runtime/components/react/types/common.ts +2 -0
  116. package/lib/runtime/components/react/types/global.d.ts +5 -17
  117. package/lib/runtime/components/react/useAnimationHooks.ts +36 -11
  118. package/lib/runtime/components/react/utils.tsx +99 -28
  119. package/lib/runtime/components/web/getInnerListeners.js +6 -6
  120. package/lib/runtime/components/web/mpx-movable-view.vue +334 -344
  121. package/lib/runtime/components/web/mpx-picker-view-column.vue +75 -75
  122. package/lib/runtime/components/web/mpx-picker.vue +382 -385
  123. package/lib/runtime/components/web/mpx-web-view.vue +175 -161
  124. package/lib/runtime/optionProcessor.js +7 -38
  125. package/lib/runtime/utils.js +2 -0
  126. package/lib/style-compiler/plugins/scope-id.js +30 -2
  127. package/lib/template-compiler/bind-this.js +7 -2
  128. package/lib/template-compiler/compiler.js +79 -47
  129. package/lib/template-compiler/gen-node-react.js +3 -3
  130. package/lib/utils/pre-process-json.js +9 -5
  131. package/package.json +6 -4
  132. package/LICENSE +0 -433
  133. package/lib/runtime/components/react/dist/mpx-image/index.jsx +0 -226
  134. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +0 -7
  135. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +0 -478
  136. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +0 -68
  137. package/lib/runtime/components/react/dist/mpx-swiper/type.js +0 -1
  138. package/lib/runtime/components/react/mpx-image/index.tsx +0 -345
  139. package/lib/runtime/components/react/mpx-image/svg.tsx +0 -22
  140. package/lib/runtime/components/web/event.js +0 -105
@@ -17,12 +17,12 @@
17
17
  * ✔ htouchmove
18
18
  * ✔ vtouchmove
19
19
  */
20
- import { useEffect, forwardRef, useContext, useCallback, useRef, useMemo } from 'react';
20
+ import { useEffect, forwardRef, useContext, useCallback, useRef, useMemo, createElement } from 'react';
21
21
  import { StyleSheet } from 'react-native';
22
22
  import { getCustomEvent } from './getInnerListeners';
23
23
  import useNodesRef from './useNodesRef';
24
24
  import { MovableAreaContext } from './context';
25
- import { useTransformStyle, splitProps, splitStyle, HIDDEN_STYLE, wrapChildren, flatGesture } from './utils';
25
+ import { useTransformStyle, splitProps, splitStyle, HIDDEN_STYLE, wrapChildren, flatGesture, extendObject } 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
28
  const styles = StyleSheet.create({
@@ -64,7 +64,7 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
64
64
  const waitForHandlers = flatGesture(waitFor);
65
65
  const nodeRef = useRef(null);
66
66
  useNodesRef(props, ref, nodeRef, {
67
- defaultStyle: styles.container,
67
+ style: normalStyle,
68
68
  gestureRef: movableGestureRef
69
69
  });
70
70
  const hasSimultaneousHandlersChanged = prevSimultaneousHandlersRef.current.length !== (originSimultaneousHandlers?.length || 0) ||
@@ -416,16 +416,16 @@ const _MovableView = forwardRef((movableViewProps, ref) => {
416
416
  };
417
417
  const catchEventHandlers = injectCatchEvent(props);
418
418
  const layoutStyle = !hasLayoutRef.current && hasSelfPercent ? HIDDEN_STYLE : {};
419
- return (<GestureDetector gesture={gesture}>
420
- <Animated.View ref={nodeRef} onLayout={onLayout} style={[innerStyle, animatedStyles, layoutStyle]} {...catchEventHandlers}>
421
- {wrapChildren(props, {
422
- hasVarDec,
423
- varContext: varContextRef.current,
424
- textStyle,
425
- textProps
426
- })}
427
- </Animated.View>
428
- </GestureDetector>);
419
+ return createElement(GestureDetector, { gesture: gesture }, createElement(Animated.View, extendObject({
420
+ ref: nodeRef,
421
+ onLayout: onLayout,
422
+ style: [innerStyle, animatedStyles, layoutStyle]
423
+ }, catchEventHandlers), wrapChildren(props, {
424
+ hasVarDec,
425
+ varContext: varContextRef.current,
426
+ textStyle,
427
+ textProps
428
+ })));
429
429
  });
430
430
  _MovableView.displayName = 'MpxMovableView';
431
431
  export default _MovableView;
@@ -1,4 +1,4 @@
1
- import { useCallback, forwardRef } from 'react';
1
+ import { useCallback, forwardRef, createElement } from 'react';
2
2
  import useInnerProps from './getInnerListeners';
3
3
  import { redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy';
4
4
  import MpxView from './mpx-view';
@@ -27,9 +27,7 @@ const _Navigator = forwardRef((props, ref) => {
27
27
  ref,
28
28
  bindtap: handleClick
29
29
  });
30
- return (<MpxView {...innerProps}>
31
- {children}
32
- </MpxView>);
30
+ return createElement(MpxView, innerProps, children);
33
31
  });
34
32
  _Navigator.displayName = 'MpxNavigator';
35
33
  export default _Navigator;
@@ -23,12 +23,15 @@ function dateToString(date, fields = 'day') {
23
23
  return ret;
24
24
  }
25
25
  const _DatePicker = forwardRef((props, ref) => {
26
- const { children, start = '1970-01-01', end = '2999-01-01', value, bindchange, bindcancel, disabled, fields } = props;
26
+ const { children, start = '1970-01-01', end = '2999-01-01', value, bindchange, bindcancel, disabled, fields, style } = props;
27
27
  const [datevalue, setDateValue] = useState(value);
28
28
  // 存储layout布局信息
29
29
  const layoutRef = useRef({});
30
30
  const viewRef = useRef(null);
31
- useNodesRef(props, ref, viewRef, {});
31
+ const nodeRef = useRef(null);
32
+ useNodesRef(props, ref, nodeRef, {
33
+ style
34
+ });
32
35
  useEffect(() => {
33
36
  value && setDateValue(value);
34
37
  }, [value]);
@@ -48,6 +51,7 @@ const _DatePicker = forwardRef((props, ref) => {
48
51
  });
49
52
  };
50
53
  const dateProps = {
54
+ ref: nodeRef,
51
55
  precision: fields,
52
56
  value: formatTimeStr(datevalue),
53
57
  minDate: formatTimeStr(start),
@@ -28,10 +28,12 @@ import { FormContext } from '../context';
28
28
  * ✘ bindcolumnchange
29
29
  */
30
30
  const _Picker = forwardRef((props, ref) => {
31
- const { mode = 'selector', value, bindcancel, bindchange, children, bindcolumnchange } = props;
31
+ const { mode = 'selector', value, bindcancel, bindchange, children, bindcolumnchange, style } = props;
32
32
  const innerLayout = useRef({});
33
33
  const nodeRef = useRef(null);
34
- useNodesRef(props, ref, nodeRef, {});
34
+ useNodesRef(props, ref, nodeRef, {
35
+ style
36
+ });
35
37
  const innerProps = useInnerProps(props, {
36
38
  ref: nodeRef
37
39
  }, [], { layoutRef: innerLayout });
@@ -78,7 +80,7 @@ const _Picker = forwardRef((props, ref) => {
78
80
  bindcolumnchange && bindcolumnchange(eventData);
79
81
  };
80
82
  const commonProps = {
81
- ...{ innerProps },
83
+ ...innerProps,
82
84
  mode,
83
85
  children,
84
86
  bindchange: onChange,
@@ -77,7 +77,7 @@ function getColumnIndexByValue(range = [], column, value = []) {
77
77
  return changeIndex;
78
78
  }
79
79
  const _MultiSelectorPicker = forwardRef((props, ref) => {
80
- const { range, value, disabled, bindchange, bindcancel, children, bindcolumnchange } = props;
80
+ const { range, value, disabled, bindchange, bindcancel, children, bindcolumnchange, style } = props;
81
81
  const formatRange = formatRangeFun(range, props['range-key']);
82
82
  const initValue = getInnerValueByIndex(formatRange, value);
83
83
  // 选中的索引值
@@ -87,7 +87,10 @@ const _MultiSelectorPicker = forwardRef((props, ref) => {
87
87
  // 存储layout布局信息
88
88
  const layoutRef = useRef({});
89
89
  const viewRef = useRef(null);
90
- useNodesRef(props, ref, viewRef, {});
90
+ const nodeRef = useRef(null);
91
+ useNodesRef(props, ref, nodeRef, {
92
+ style
93
+ });
91
94
  useEffect(() => {
92
95
  if (range) {
93
96
  const newFormatRange = formatRangeFun(range, props['range-key']);
@@ -117,6 +120,7 @@ const _MultiSelectorPicker = forwardRef((props, ref) => {
117
120
  });
118
121
  };
119
122
  const antPickerProps = {
123
+ ref: nodeRef,
120
124
  data,
121
125
  value: selected,
122
126
  cols: range.length,
@@ -35,13 +35,16 @@ function formateRegionData(clObj = [], customItem, depth = 2) {
35
35
  return obj;
36
36
  }
37
37
  const _RegionPicker = forwardRef((props, ref) => {
38
- const { children, value, bindchange, bindcancel, disabled } = props;
38
+ const { children, value, bindchange, bindcancel, disabled, style } = props;
39
39
  const formatRegionData = formateRegionData(regionData, props['custom-item']);
40
40
  const [regionvalue, setRegionValue] = useState(value);
41
41
  // 存储layout布局信息
42
42
  const layoutRef = useRef({});
43
43
  const viewRef = useRef(null);
44
- useNodesRef(props, ref, viewRef, {});
44
+ const nodeRef = useRef(null);
45
+ useNodesRef(props, ref, nodeRef, {
46
+ style
47
+ });
45
48
  const onChange = (value) => {
46
49
  // 通过 value 查找 code
47
50
  let tmp = regionData;
@@ -75,6 +78,7 @@ const _RegionPicker = forwardRef((props, ref) => {
75
78
  bindcancel && bindcancel();
76
79
  };
77
80
  const regionProps = {
81
+ ref: nodeRef,
78
82
  data: formatRegionData,
79
83
  value: regionvalue,
80
84
  onChange,
@@ -18,7 +18,7 @@ const formatRangeFun = (range, rangeKey = '') => {
18
18
  return newRange;
19
19
  };
20
20
  const _SelectorPicker = forwardRef((props, ref) => {
21
- const { range, children, value, disabled, bindchange, bindcancel } = props;
21
+ const { range, children, value, disabled, bindchange, bindcancel, style } = props;
22
22
  // 格式化数据为Array<*>
23
23
  const formatRange = formatRangeFun(range, props['range-key']);
24
24
  // 选中的索引值
@@ -28,7 +28,10 @@ const _SelectorPicker = forwardRef((props, ref) => {
28
28
  // 存储layout布局信息
29
29
  const layoutRef = useRef({});
30
30
  const viewRef = useRef(null);
31
- useNodesRef(props, ref, viewRef, {});
31
+ const nodeRef = useRef(null);
32
+ useNodesRef(props, ref, nodeRef, {
33
+ style
34
+ });
32
35
  useEffect(() => {
33
36
  if (range) {
34
37
  const newFormatRange = formatRangeFun(range, props['range-key']);
@@ -53,6 +56,7 @@ const _SelectorPicker = forwardRef((props, ref) => {
53
56
  });
54
57
  };
55
58
  const antPickerProps = {
59
+ ref: nodeRef,
56
60
  data,
57
61
  value: [selected],
58
62
  cols: 1,
@@ -1,5 +1,5 @@
1
1
  import { View, Text, Modal, TouchableWithoutFeedback } from 'react-native';
2
- import { PickerView } from '@ant-design/react-native';
2
+ import { PickerView, Portal } from '@ant-design/react-native';
3
3
  import React, { forwardRef, useState, useRef, useEffect } from 'react';
4
4
  import useNodesRef from '../useNodesRef'; // 引入辅助函数
5
5
  // 可见应用窗口的大小。
@@ -11,8 +11,8 @@ const styles = {
11
11
  showModal: {
12
12
  backgroundColor: 'black',
13
13
  opacity: 0.5,
14
- position: 'absolute',
15
- width: '100%'
14
+ width: '100%',
15
+ height: '100%'
16
16
  },
17
17
  hideModal: {
18
18
  opacity: 1,
@@ -117,7 +117,7 @@ function checkSelectedIsValid(strStart, strEnd, selected) {
117
117
  */
118
118
  // start="02:10" end = 23:01
119
119
  const _TimePicker = forwardRef((props, ref) => {
120
- const { children, start, end, value, bindchange, bindcancel, disabled } = props;
120
+ const { children, start, end, value, bindchange, bindcancel, style } = props;
121
121
  const defaultProps = {
122
122
  start: '00:10',
123
123
  end: '23:59'
@@ -127,11 +127,11 @@ const _TimePicker = forwardRef((props, ref) => {
127
127
  // 存储layout布局信息
128
128
  const layoutRef = useRef({});
129
129
  const viewRef = useRef(null);
130
- useNodesRef(props, ref, viewRef, {});
130
+ const nodeRef = useRef(null);
131
+ useNodesRef(props, ref, nodeRef, { style });
131
132
  // 存储modal的布局信息
132
133
  const modalLayoutRef = useRef({});
133
134
  const modalRef = useRef(null);
134
- useNodesRef(props, ref, modalRef, {});
135
135
  const [visible, setVisible] = useState(false);
136
136
  const columnData = generateColumns();
137
137
  const [data, setData] = useState(columnData);
@@ -175,12 +175,6 @@ const _TimePicker = forwardRef((props, ref) => {
175
175
  else {
176
176
  // [9, 13]
177
177
  setTimeValue(date);
178
- const strDate = formatStr(date);
179
- bindchange && bindchange({
180
- detail: {
181
- value: strDate
182
- }
183
- });
184
178
  }
185
179
  };
186
180
  const onElementLayout = () => {
@@ -197,6 +191,7 @@ const _TimePicker = forwardRef((props, ref) => {
197
191
  };
198
192
  const renderModalChildren = () => {
199
193
  const pickerProps = {
194
+ ref: nodeRef,
200
195
  data,
201
196
  value: timevalue,
202
197
  defaultValue: timevalue,
@@ -230,15 +225,15 @@ const _TimePicker = forwardRef((props, ref) => {
230
225
  </TouchableWithoutFeedback>
231
226
  </View>;
232
227
  };
233
- const strStyle = visible ? styles.showModal : styles.hideModal;
234
- const mheight = Math.floor(offsetTop);
235
228
  // Animated.View
236
229
  return (<>
237
- <View style={{ ...strStyle, height: visible ? mheight : 0, bottom: 0 }}>
230
+ <Portal>
231
+ <View style={visible ? styles.showModal : styles.hideModal}>
238
232
  <Modal animationType="slide" transparent={true} visible={visible}>
239
233
  {renderModalChildren()}
240
234
  </Modal>
241
235
  </View>
236
+ </Portal>
242
237
  {renderChildren()}
243
238
  </>);
244
239
  });
@@ -0,0 +1,39 @@
1
+ import React, { useEffect } from 'react';
2
+ import Reanimated, { Extrapolation, interpolate, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
3
+ import { wrapChildren, extendObject } from './utils';
4
+ import { createFaces } from './pickerFaces';
5
+ import { usePickerViewColumnAnimationContext } from './pickerVIewContext';
6
+ const _PickerViewColumnItem = ({ item, index, itemHeight, itemWidth, textStyleFromParent, textStyle, hasVarDec, varContext, textProps, visibleCount, onItemLayout }) => {
7
+ const offsetYShared = usePickerViewColumnAnimationContext();
8
+ const facesShared = useSharedValue(createFaces(itemHeight, visibleCount));
9
+ useEffect(() => {
10
+ facesShared.value = createFaces(itemHeight, visibleCount);
11
+ }, [itemHeight]);
12
+ const animatedStyles = useAnimatedStyle(() => {
13
+ const inputRange = facesShared.value.map((f) => itemHeight * (index + f.index));
14
+ return {
15
+ opacity: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.opacity), Extrapolation.CLAMP),
16
+ transform: [
17
+ { rotateX: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.deg), Extrapolation.CLAMP) + 'deg' },
18
+ { translateY: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.offsetY), Extrapolation.EXTEND) },
19
+ { scale: interpolate(offsetYShared.value, inputRange, facesShared.value.map((x) => x.scale), Extrapolation.EXTEND) }
20
+ ]
21
+ };
22
+ });
23
+ const strKey = `picker-column-item-${index}`;
24
+ const restProps = index === 0 ? { onLayout: onItemLayout } : {};
25
+ const itemProps = extendObject({
26
+ style: extendObject({ height: itemHeight, width: '100%' }, textStyleFromParent, textStyle, item.props.style)
27
+ }, restProps);
28
+ const realItem = React.cloneElement(item, itemProps);
29
+ return (<Reanimated.View key={strKey} style={[{ height: itemHeight, width: itemWidth }, animatedStyles]}>
30
+ {wrapChildren({ children: realItem }, {
31
+ hasVarDec,
32
+ varContext,
33
+ textStyle,
34
+ textProps
35
+ })}
36
+ </Reanimated.View>);
37
+ };
38
+ _PickerViewColumnItem.displayName = 'MpxPickerViewColumnItem';
39
+ export default _PickerViewColumnItem;
@@ -1,108 +1,180 @@
1
- import { View, Animated, SafeAreaView } from 'react-native';
2
- import React, { forwardRef, useRef, useState, useEffect } from 'react';
3
- import { useTransformStyle, splitStyle, splitProps, wrapChildren, useLayout } from './utils';
4
- import useNodesRef from './useNodesRef'; // 引入辅助函数
5
- const defaultItemHeight = 36;
6
- // 每个Column 都有个外层的高度, 内部的元素高度
7
- // 默认的高度
1
+ import React, { forwardRef, useRef, useState, useMemo, useEffect, useCallback } from 'react';
2
+ import { Platform, SafeAreaView, StyleSheet } from 'react-native';
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';
6
+ import useNodesRef from './useNodesRef';
7
+ import PickerOverlay from './pickerViewOverlay';
8
+ import PickerMask from './pickerViewMask';
9
+ import MpxPickerVIewColumnItem from './mpx-picker-view-column-item';
10
+ import { PickerViewColumnAnimationContext } from './pickerVIewContext';
11
+ const visibleCount = 5;
8
12
  const _PickerViewColumn = forwardRef((props, ref) => {
9
- const { children, selectedIndex, onColumnLayoutChange, onSelectChange, getInnerLayout, style, wrapperStyle, 'enable-var': enableVar, 'external-var-context': externalVarContext } = props;
10
- // PickerViewColumn
13
+ const { columnData, columnIndex, columnStyle, initialIndex, onSelectChange, style, wrapperStyle, pickerMaskStyle, pickerOverlayStyle, 'enable-var': enableVar, 'external-var-context': externalVarContext } = props;
11
14
  const { normalStyle, hasVarDec, varContextRef, hasSelfPercent, setWidth, setHeight } = useTransformStyle(style, { enableVar, externalVarContext });
12
- const { textStyle } = splitStyle(normalStyle);
15
+ const { textStyle: textStyleFromParent = {} } = splitStyle(columnStyle);
16
+ const { textStyle = {} } = splitStyle(normalStyle);
13
17
  const { textProps } = splitProps(props);
14
- // const { innerStyle } = splitStyle(normalStyle)
15
- // scrollView的ref
16
- const scrollViewRef = useRef(null);
17
- useNodesRef(props, ref, scrollViewRef, {});
18
- // 每个元素的高度
19
- let [itemH, setItemH] = useState(0);
18
+ const scrollViewRef = useAnimatedRef();
19
+ const offsetYShared = useScrollViewOffset(scrollViewRef);
20
+ useNodesRef(props, ref, scrollViewRef, {
21
+ style: normalStyle
22
+ });
23
+ const { height: pickerH, itemHeight } = wrapperStyle;
24
+ const [scrollViewWidth, setScrollViewWidth] = useState('100%');
25
+ const [itemRawW, setItemRawW] = useState('100%');
26
+ const [itemRawH, setItemRawH] = useState(itemHeight);
27
+ const maxIndex = useMemo(() => columnData.length - 1, [columnData]);
28
+ const maxScrollViewWidth = useRef(-1);
29
+ const prevScrollingInfo = useRef({ index: initialIndex, y: 0 });
30
+ const touching = useRef(false);
31
+ const scrolling = useRef(false);
32
+ const activeIndex = useRef(initialIndex);
33
+ const prevIndex = usePrevious(initialIndex);
34
+ const prevMaxIndex = usePrevious(maxIndex);
35
+ const { layoutProps } = useLayout({
36
+ props,
37
+ hasSelfPercent,
38
+ setWidth,
39
+ setHeight,
40
+ nodeRef: scrollViewRef
41
+ });
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]);
49
+ const contentContainerStyle = useMemo(() => {
50
+ return [{ paddingVertical: paddingHeight }];
51
+ }, [paddingHeight]);
52
+ const getIndex = useCallback((y) => {
53
+ const calc = Math.round(y / itemRawH);
54
+ return Math.max(0, Math.min(calc, maxIndex));
55
+ }, [itemRawH, maxIndex]);
20
56
  useEffect(() => {
21
- if (selectedIndex && itemH) {
22
- const offsetY = selectedIndex * itemH;
23
- scrollViewRef.current?.scrollTo({ x: 0, y: offsetY, animated: true });
57
+ if (!scrollViewRef.current ||
58
+ !itemRawH ||
59
+ touching.current ||
60
+ scrolling.current ||
61
+ prevIndex == null ||
62
+ initialIndex === prevIndex ||
63
+ initialIndex === activeIndex.current ||
64
+ maxIndex !== prevMaxIndex) {
65
+ return;
66
+ }
67
+ // console.log('[mpx-picker-view-column], useEffect ---> columnIndex=', columnIndex, 'initialIndex=', initialIndex, 'y=', itemRawH * initialIndex, `${scrollViewRef.current?.scrollTo}`)
68
+ setTimeout(() => {
69
+ scrollViewRef.current?.scrollTo({
70
+ x: 0,
71
+ y: itemRawH * initialIndex,
72
+ animated: false
73
+ });
74
+ }, 0);
75
+ 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)
80
+ if (y <= h) {
81
+ setTimeout(() => {
82
+ scrollViewRef.current?.scrollTo({ x: 0, y, animated: false });
83
+ }, 0);
84
+ }
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);
99
+ }
100
+ if (itemRawW === '100%') {
101
+ setItemRawW(widthInt);
24
102
  }
25
- }, [selectedIndex, itemH]);
26
- const onScrollViewLayout = () => {
27
- getInnerLayout && getInnerLayout(layoutRef);
28
103
  };
29
- const {
30
- // 存储layout布局信息
31
- layoutRef, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: scrollViewRef, onLayout: onScrollViewLayout });
32
104
  const onItemLayout = (e) => {
33
- const layout = e.nativeEvent.layout;
34
- if (layout.height && itemH !== layout.height) {
35
- itemH = layout.height;
36
- setItemH(layout.height);
37
- onColumnLayoutChange && onColumnLayoutChange({ height: layout.height * 5 });
105
+ const { height: rawH } = e.nativeEvent.layout;
106
+ if (rawH && itemRawH !== rawH) {
107
+ setItemRawH(rawH);
38
108
  }
39
109
  };
110
+ const onTouchStart = () => {
111
+ touching.current = true;
112
+ prevScrollingInfo.current = {
113
+ index: activeIndex.current,
114
+ y: activeIndex.current * itemRawH
115
+ };
116
+ };
117
+ const onTouchEnd = () => {
118
+ touching.current = false;
119
+ };
120
+ const onTouchCancel = () => {
121
+ touching.current = false;
122
+ };
123
+ const onMomentumScrollBegin = () => {
124
+ scrolling.current = true;
125
+ };
40
126
  const onMomentumScrollEnd = (e) => {
41
- if (scrollViewRef && itemH) {
42
- const { y: scrollY } = e.nativeEvent.contentOffset;
43
- const selIndex = Math.floor(scrollY / itemH);
44
- onSelectChange(selIndex);
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);
45
137
  }
46
138
  };
47
- const renderInnerchild = () => {
48
- // Fragment 节点
49
- let realElement = [];
50
- const getRealChilds = () => {
51
- if (Array.isArray(children)) {
52
- realElement = children;
53
- }
54
- else {
55
- const tempChild = children;
56
- if (tempChild.props.children && tempChild.props.children) {
57
- realElement = tempChild.props.children;
58
- }
59
- else {
60
- realElement = [children];
139
+ const onScroll = (e) => {
140
+ if (Platform.OS === 'android') {
141
+ return;
142
+ }
143
+ const { y } = e.nativeEvent.contentOffset;
144
+ const { index: prevIndex, y: _y } = prevScrollingInfo.current;
145
+ if (touching.current || scrolling.current) {
146
+ if (Math.abs(y - _y) >= itemRawH) {
147
+ const currentId = getIndex(y);
148
+ if (currentId !== prevIndex) {
149
+ prevScrollingInfo.current = {
150
+ index: currentId,
151
+ y: currentId * itemRawH
152
+ };
153
+ vibrateShort({ type: 'selection' });
61
154
  }
62
155
  }
63
- return realElement;
64
- };
65
- const realChilds = getRealChilds();
66
- const arrChild = realChilds.map((item, index) => {
67
- const InnerProps = index === 0 ? { onLayout: onItemLayout } : {};
68
- const strKey = 'picker' + props.prefix + '-column' + index;
69
- const arrHeight = (wrapperStyle.itemHeight + '').match(/\d+/g) || [];
70
- const iHeight = (arrHeight[0] || defaultItemHeight);
71
- return <View key={strKey} {...InnerProps} style={[{ height: iHeight, width: '100%' }]}>
72
- {wrapChildren({
73
- children: item
74
- }, {
75
- hasVarDec,
76
- varContext: varContextRef.current,
77
- textStyle,
78
- textProps
79
- })}
80
- </View>;
81
- });
82
- const totalHeight = itemH * 5;
83
- if (wrapperStyle.height && totalHeight !== wrapperStyle.height) {
84
- const fix = Math.ceil((totalHeight - wrapperStyle.height) / 2);
85
- arrChild.unshift(<View key="picker-column-0" style={[{ height: itemH - fix }]}></View>);
86
- arrChild.unshift(<View key="picker-column-1" style={[{ height: itemH }]}></View>);
87
- arrChild.push(<View key="picker-column-2" style={[{ height: itemH }]}></View>);
88
- arrChild.push(<View key="picker-column-3" style={[{ height: itemH - fix }]}></View>);
89
156
  }
90
- else {
91
- arrChild.unshift(<View key="picker-column-0" style={[{ height: itemH }]}></View>);
92
- arrChild.unshift(<View key="picker-column-1" style={[{ height: itemH }]}></View>);
93
- arrChild.push(<View key="picker-column-2" style={[{ height: itemH }]}></View>);
94
- arrChild.push(<View key="picker-column-3" style={[{ height: itemH }]}></View>);
95
- }
96
- return arrChild;
97
157
  };
158
+ 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}/>);
160
+ });
98
161
  const renderScollView = () => {
99
- return (<Animated.ScrollView horizontal={false} ref={scrollViewRef} bounces={false} scrollsToTop={false} removeClippedSubviews={true} showsHorizontalScrollIndicator={false} showsVerticalScrollIndicator={false} pagingEnabled={false} snapToInterval={itemH} automaticallyAdjustContentInsets={false} {...layoutProps} onMomentumScrollEnd={onMomentumScrollEnd}>
100
- {renderInnerchild()}
101
- </Animated.ScrollView>);
162
+ 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}>
164
+ {renderInnerchild()}
165
+ </Reanimated.ScrollView>
166
+ </PickerViewColumnAnimationContext.Provider>);
102
167
  };
103
- return (<SafeAreaView style={[{ display: 'flex', flex: 1 }]}>
104
- {renderScollView()}
105
- </SafeAreaView>);
168
+ const renderOverlay = () => (<PickerOverlay itemHeight={itemHeight} overlayItemStyle={pickerOverlayStyle}/>);
169
+ const renderMask = () => (<PickerMask itemHeight={itemHeight} maskContainerStyle={pickerMaskStyle}/>);
170
+ return (<SafeAreaView style={[styles.wrapper, normalStyle]}>
171
+ {renderScollView()}
172
+ {renderMask()}
173
+ {renderOverlay()}
174
+ </SafeAreaView>);
175
+ });
176
+ const styles = StyleSheet.create({
177
+ wrapper: { display: 'flex', flex: 1 }
106
178
  });
107
- _PickerViewColumn.displayName = 'mpx-picker-view-column';
179
+ _PickerViewColumn.displayName = 'MpxPickerViewColumn';
108
180
  export default _PickerViewColumn;