@mpxjs/webpack-plugin 2.10.3 → 2.10.4-beta.11

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 (155) hide show
  1. package/lib/config.js +2 -1
  2. package/lib/index.js +1 -1
  3. package/lib/platform/json/wx/index.js +6 -4
  4. package/lib/platform/style/wx/index.js +42 -30
  5. package/lib/platform/template/wx/component-config/button.js +20 -3
  6. package/lib/platform/template/wx/component-config/canvas.js +4 -0
  7. package/lib/platform/template/wx/component-config/checkbox-group.js +4 -0
  8. package/lib/platform/template/wx/component-config/checkbox.js +4 -0
  9. package/lib/platform/template/wx/component-config/cover-image.js +7 -1
  10. package/lib/platform/template/wx/component-config/cover-view.js +4 -0
  11. package/lib/platform/template/wx/component-config/fix-component-name.js +3 -2
  12. package/lib/platform/template/wx/component-config/form.js +7 -1
  13. package/lib/platform/template/wx/component-config/icon.js +4 -0
  14. package/lib/platform/template/wx/component-config/image.js +7 -1
  15. package/lib/platform/template/wx/component-config/index.js +5 -1
  16. package/lib/platform/template/wx/component-config/input.js +19 -4
  17. package/lib/platform/template/wx/component-config/label.js +4 -0
  18. package/lib/platform/template/wx/component-config/movable-area.js +7 -1
  19. package/lib/platform/template/wx/component-config/movable-view.js +12 -3
  20. package/lib/platform/template/wx/component-config/navigator.js +4 -0
  21. package/lib/platform/template/wx/component-config/picker-view-column.js +4 -0
  22. package/lib/platform/template/wx/component-config/picker-view.js +7 -1
  23. package/lib/platform/template/wx/component-config/picker.js +7 -1
  24. package/lib/platform/template/wx/component-config/radio-group.js +4 -0
  25. package/lib/platform/template/wx/component-config/radio.js +4 -0
  26. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  27. package/lib/platform/template/wx/component-config/root-portal.js +4 -0
  28. package/lib/platform/template/wx/component-config/scroll-view.js +10 -2
  29. package/lib/platform/template/wx/component-config/sticky-header.js +23 -0
  30. package/lib/platform/template/wx/component-config/sticky-section.js +23 -0
  31. package/lib/platform/template/wx/component-config/swiper-item.js +7 -1
  32. package/lib/platform/template/wx/component-config/swiper.js +12 -3
  33. package/lib/platform/template/wx/component-config/switch.js +4 -0
  34. package/lib/platform/template/wx/component-config/text.js +24 -3
  35. package/lib/platform/template/wx/component-config/textarea.js +17 -2
  36. package/lib/platform/template/wx/component-config/unsupported.js +7 -0
  37. package/lib/platform/template/wx/component-config/video.js +10 -2
  38. package/lib/platform/template/wx/component-config/view.js +11 -1
  39. package/lib/platform/template/wx/component-config/web-view.js +4 -0
  40. package/lib/platform/template/wx/index.js +42 -75
  41. package/lib/react/processJSON.js +7 -6
  42. package/lib/react/processScript.js +1 -18
  43. package/lib/runtime/components/react/context.ts +12 -3
  44. package/lib/runtime/components/react/dist/context.js +4 -1
  45. package/lib/runtime/components/react/dist/event.config.js +0 -1
  46. package/lib/runtime/components/react/dist/getInnerListeners.js +127 -142
  47. package/lib/runtime/components/react/dist/mpx-button.jsx +4 -5
  48. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +4 -5
  49. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +2 -2
  50. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +2 -3
  51. package/lib/runtime/components/react/dist/mpx-form.jsx +2 -2
  52. package/lib/runtime/components/react/dist/mpx-icon/index.jsx +2 -2
  53. package/lib/runtime/components/react/dist/mpx-image.jsx +2 -2
  54. package/lib/runtime/components/react/dist/mpx-inline-text.jsx +11 -0
  55. package/lib/runtime/components/react/dist/mpx-input.jsx +20 -17
  56. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.jsx +24 -31
  57. package/lib/runtime/components/react/dist/mpx-label.jsx +2 -3
  58. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +2 -2
  59. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +1 -1
  60. package/lib/runtime/components/react/dist/mpx-navigator.jsx +11 -3
  61. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +194 -68
  62. package/lib/runtime/components/react/dist/mpx-picker/dateData.js +17 -0
  63. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +180 -98
  64. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +79 -139
  65. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +190 -90
  66. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +60 -75
  67. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +100 -228
  68. package/lib/runtime/components/react/dist/{mpx-picker-view.jsx → mpx-picker-view/index.jsx} +5 -6
  69. package/lib/runtime/components/react/dist/{mpx-picker-view-column.jsx → mpx-picker-view-column/index.jsx} +66 -18
  70. package/lib/runtime/components/react/dist/{mpx-picker-view-column-item.jsx → mpx-picker-view-column/pickerViewColumnItem.jsx} +8 -5
  71. package/lib/runtime/components/react/dist/{pickerFaces.js → mpx-picker-view-column/pickerViewFaces.js} +6 -0
  72. package/lib/runtime/components/react/dist/mpx-popup/index.jsx +61 -0
  73. package/lib/runtime/components/react/dist/mpx-popup/popupBase.jsx +92 -0
  74. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +2 -2
  75. package/lib/runtime/components/react/dist/mpx-radio.jsx +2 -3
  76. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +2 -2
  77. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +207 -29
  78. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +7 -5
  79. package/lib/runtime/components/react/dist/mpx-simple-view.jsx +11 -15
  80. package/lib/runtime/components/react/dist/mpx-sticky-header.jsx +115 -0
  81. package/lib/runtime/components/react/dist/mpx-sticky-section.jsx +45 -0
  82. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +4 -5
  83. package/lib/runtime/components/react/dist/mpx-swiper.jsx +9 -9
  84. package/lib/runtime/components/react/dist/mpx-switch.jsx +3 -5
  85. package/lib/runtime/components/react/dist/mpx-text.jsx +4 -7
  86. package/lib/runtime/components/react/dist/mpx-video.jsx +5 -5
  87. package/lib/runtime/components/react/dist/mpx-view.jsx +23 -9
  88. package/lib/runtime/components/react/dist/mpx-web-view.jsx +16 -13
  89. package/lib/runtime/components/react/dist/useAnimationHooks.js +26 -4
  90. package/lib/runtime/components/react/dist/utils.jsx +14 -2
  91. package/lib/runtime/components/react/event.config.ts +1 -6
  92. package/lib/runtime/components/react/getInnerListeners.ts +148 -191
  93. package/lib/runtime/components/react/mpx-button.tsx +7 -7
  94. package/lib/runtime/components/react/mpx-canvas/index.tsx +25 -17
  95. package/lib/runtime/components/react/mpx-checkbox-group.tsx +4 -3
  96. package/lib/runtime/components/react/mpx-checkbox.tsx +8 -9
  97. package/lib/runtime/components/react/mpx-form.tsx +25 -19
  98. package/lib/runtime/components/react/mpx-icon/index.tsx +4 -3
  99. package/lib/runtime/components/react/mpx-image.tsx +4 -3
  100. package/lib/runtime/components/react/mpx-inline-text.tsx +18 -0
  101. package/lib/runtime/components/react/mpx-input.tsx +24 -21
  102. package/lib/runtime/components/react/mpx-keyboard-avoiding-view.tsx +31 -42
  103. package/lib/runtime/components/react/mpx-label.tsx +4 -5
  104. package/lib/runtime/components/react/mpx-movable-area.tsx +22 -13
  105. package/lib/runtime/components/react/mpx-movable-view.tsx +47 -40
  106. package/lib/runtime/components/react/mpx-navigator.tsx +4 -6
  107. package/lib/runtime/components/react/mpx-picker/date.tsx +226 -69
  108. package/lib/runtime/components/react/mpx-picker/dateData.ts +22 -0
  109. package/lib/runtime/components/react/mpx-picker/index.tsx +242 -118
  110. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +96 -139
  111. package/lib/runtime/components/react/mpx-picker/region.tsx +217 -89
  112. package/lib/runtime/components/react/mpx-picker/selector.tsx +75 -80
  113. package/lib/runtime/components/react/mpx-picker/time.tsx +119 -236
  114. package/lib/runtime/components/react/mpx-picker/type.ts +85 -71
  115. package/lib/runtime/components/react/{mpx-picker-view.tsx → mpx-picker-view/index.tsx} +24 -21
  116. package/lib/runtime/components/react/{mpx-picker-view-column.tsx → mpx-picker-view-column/index.tsx} +72 -21
  117. package/lib/runtime/components/react/{mpx-picker-view-column-item.tsx → mpx-picker-view-column/pickerViewColumnItem.tsx} +8 -5
  118. package/lib/runtime/components/react/{pickerFaces.ts → mpx-picker-view-column/pickerViewFaces.ts} +7 -0
  119. package/lib/runtime/components/react/mpx-popup/index.tsx +86 -0
  120. package/lib/runtime/components/react/mpx-popup/popupBase.tsx +130 -0
  121. package/lib/runtime/components/react/mpx-radio-group.tsx +4 -3
  122. package/lib/runtime/components/react/mpx-radio.tsx +8 -9
  123. package/lib/runtime/components/react/mpx-rich-text/index.tsx +15 -6
  124. package/lib/runtime/components/react/mpx-scroll-view.tsx +326 -96
  125. package/lib/runtime/components/react/mpx-simple-text.tsx +17 -8
  126. package/lib/runtime/components/react/mpx-simple-view.tsx +17 -16
  127. package/lib/runtime/components/react/mpx-sticky-header.tsx +179 -0
  128. package/lib/runtime/components/react/mpx-sticky-section.tsx +96 -0
  129. package/lib/runtime/components/react/mpx-swiper-item.tsx +31 -24
  130. package/lib/runtime/components/react/mpx-swiper.tsx +67 -61
  131. package/lib/runtime/components/react/mpx-switch.tsx +19 -14
  132. package/lib/runtime/components/react/mpx-text.tsx +16 -13
  133. package/lib/runtime/components/react/mpx-video.tsx +36 -35
  134. package/lib/runtime/components/react/mpx-view.tsx +41 -17
  135. package/lib/runtime/components/react/mpx-web-view.tsx +15 -12
  136. package/lib/runtime/components/react/types/getInnerListeners.d.ts +69 -35
  137. package/lib/runtime/components/react/types/global.d.ts +1 -1
  138. package/lib/runtime/components/react/useAnimationHooks.ts +29 -9
  139. package/lib/runtime/components/react/utils.tsx +15 -3
  140. package/lib/runtime/components/web/mini-video-controls.min.js +1 -1
  141. package/lib/runtime/components/web/mpx-input.vue +1 -1
  142. package/lib/runtime/components/web/mpx-scroll-view.vue +21 -4
  143. package/lib/runtime/components/web/mpx-sticky-header.vue +91 -0
  144. package/lib/runtime/components/web/mpx-sticky-section.vue +15 -0
  145. package/lib/runtime/stringify.wxs +2 -2
  146. package/lib/template-compiler/compiler.js +8 -8
  147. package/lib/utils/env.js +1 -1
  148. package/package.json +4 -5
  149. package/LICENSE +0 -433
  150. /package/lib/runtime/components/react/dist/{pickerVIewContext.js → mpx-picker-view/pickerVIewContext.js} +0 -0
  151. /package/lib/runtime/components/react/dist/{pickerViewIndicator.jsx → mpx-picker-view-column/pickerViewIndicator.jsx} +0 -0
  152. /package/lib/runtime/components/react/dist/{pickerViewMask.jsx → mpx-picker-view-column/pickerViewMask.jsx} +0 -0
  153. /package/lib/runtime/components/react/{pickerVIewContext.ts → mpx-picker-view/pickerVIewContext.ts} +0 -0
  154. /package/lib/runtime/components/react/{pickerViewIndicator.tsx → mpx-picker-view-column/pickerViewIndicator.tsx} +0 -0
  155. /package/lib/runtime/components/react/{pickerViewMask.tsx → mpx-picker-view-column/pickerViewMask.tsx} +0 -0
@@ -1,99 +1,199 @@
1
- import { View, TouchableWithoutFeedback } from 'react-native';
2
- import { Picker } from '@ant-design/react-native';
1
+ import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react';
2
+ import { StyleSheet, Text } from 'react-native';
3
+ import MpxPickerView from '../mpx-picker-view';
4
+ import MpxPickerViewColumn from '../mpx-picker-view-column';
3
5
  import { regionData } from './regionData';
4
- import React, { forwardRef, useState, useRef } from 'react';
5
- import useNodesRef from '../useNodesRef'; // 引入辅助函数
6
- function formateRegionData(clObj = [], customItem, depth = 2) {
7
- const l = depth;
8
- // 'PickerData[]' is not assignable to type 'PickerColumn | PickerColumn[]'.
9
- // const obj: PickerColumnItem[] = []
10
- const obj = [];
11
- if (customItem) {
12
- const objClone = {
13
- value: customItem,
14
- label: customItem,
15
- children: []
16
- };
17
- const panding = { ...objClone };
18
- let loop = panding;
19
- while (depth-- > 0) {
20
- loop.children = [{ ...objClone }];
21
- loop = loop.children[0];
6
+ import { extendObject, useUpdateEffect } from '../utils';
7
+ const styles = StyleSheet.create({
8
+ pickerContainer: {
9
+ height: 240,
10
+ paddingHorizontal: 10,
11
+ borderTopLeftRadius: 10,
12
+ borderTopRightRadius: 10
13
+ },
14
+ pickerIndicator: {
15
+ height: 45
16
+ },
17
+ pickerItem: {
18
+ fontSize: 16,
19
+ lineHeight: 45,
20
+ textAlign: 'center'
21
+ }
22
+ });
23
+ const rangeProvince = regionData.map(item => item.value);
24
+ const findIndex = (arr, val) => {
25
+ const res = arr.findIndex(item => item === val);
26
+ return res === -1 ? 0 : res;
27
+ };
28
+ const getColumnLength = (level) => {
29
+ if (level === 'province') {
30
+ return 1;
31
+ }
32
+ else if (level === 'city') {
33
+ return 2;
34
+ }
35
+ else {
36
+ return 3;
37
+ }
38
+ };
39
+ const valueStr2Obj = (value = [], limit, customItem = '') => {
40
+ const offsetIndex = customItem ? 1 : 0;
41
+ let indexProvince = 0;
42
+ if (customItem && value[0] === customItem) {
43
+ indexProvince = 0;
44
+ }
45
+ else {
46
+ indexProvince = findIndex(rangeProvince, value[0]) + offsetIndex;
47
+ }
48
+ const ans = {
49
+ indexArr: [indexProvince],
50
+ rangeArr: [customItem ? [customItem, ...rangeProvince] : rangeProvince]
51
+ };
52
+ for (let i = 1, lastIndex = indexProvince, lastData = regionData, lastRange = rangeProvince; i < limit; i++) {
53
+ if (customItem) {
54
+ if (lastIndex === 0) {
55
+ if (i === 1) {
56
+ ans.indexArr.push(0, 0);
57
+ ans.rangeArr.push([customItem], [customItem]);
58
+ }
59
+ else {
60
+ ans.indexArr.push(0);
61
+ ans.rangeArr.push([customItem]);
62
+ }
63
+ return ans;
64
+ }
22
65
  }
23
- obj.push(panding);
66
+ lastData = lastData[lastIndex - offsetIndex].children;
67
+ lastRange = lastData.map((item) => item.value);
68
+ lastIndex = findIndex(lastRange, value[i]) + offsetIndex;
69
+ if (customItem && customItem === value[i]) {
70
+ lastIndex = 0;
71
+ }
72
+ ans.indexArr.push(Math.max(0, lastIndex));
73
+ ans.rangeArr.push(customItem ? [customItem, ...lastRange] : lastRange);
24
74
  }
25
- for (let i = 0; i < clObj.length; i++) {
26
- const region = {
27
- value: clObj[i].value,
28
- label: clObj[i].value
29
- };
30
- if (clObj[i].children) {
31
- region.children = formateRegionData(clObj[i].children, customItem, l - 1);
75
+ return ans;
76
+ };
77
+ const valueChanged2Obj = (currentObj, value, limit = 3, customItem = '') => {
78
+ const offsetIndex = customItem ? 1 : 0;
79
+ const newValue = new Array(limit).fill(0);
80
+ const currentValue = currentObj.indexArr;
81
+ for (let i = 0; i < limit; i++) {
82
+ if (i === limit - 1) {
83
+ return {
84
+ indexArr: value,
85
+ rangeArr: currentObj.rangeArr
86
+ };
87
+ }
88
+ newValue[i] = value[i];
89
+ if (currentValue[i] !== value[i]) {
90
+ break;
32
91
  }
33
- obj.push(region);
34
92
  }
35
- return obj;
36
- }
37
- const _RegionPicker = forwardRef((props, ref) => {
38
- const { children, value, bindchange, bindcancel, disabled, style } = props;
39
- const formatRegionData = formateRegionData(regionData, props['custom-item']);
40
- const [regionvalue, setRegionValue] = useState(value);
41
- // 存储layout布局信息
42
- const layoutRef = useRef({});
43
- const viewRef = useRef(null);
44
- const nodeRef = useRef(null);
45
- useNodesRef(props, ref, nodeRef, {
46
- style
47
- });
48
- const onChange = (value) => {
49
- // 通过 value 查找 code
50
- let tmp = regionData;
51
- const postcode = [];
52
- const code = value.map((item) => {
53
- for (let i = 0; i < tmp.length; i++) {
54
- if (tmp[i].value === item) {
55
- const code = tmp[i].code;
56
- postcode.push(tmp[i].postcode);
57
- tmp = tmp[i].children || [];
58
- return code;
93
+ const ans = {
94
+ indexArr: [newValue[0]],
95
+ rangeArr: [currentObj.rangeArr[0]]
96
+ };
97
+ let data = regionData;
98
+ for (let i = 1; i < limit; i++) {
99
+ if (customItem) {
100
+ if (newValue[i - 1] === 0) {
101
+ if (i === 1) {
102
+ ans.indexArr.push(0, 0);
103
+ ans.rangeArr.push([customItem], [customItem]);
104
+ }
105
+ else {
106
+ ans.indexArr.push(0);
107
+ ans.rangeArr.push([customItem]);
59
108
  }
109
+ return ans;
60
110
  }
61
- return item;
62
- }).filter(code => !!code);
63
- const detail = { value, code };
64
- if (postcode[2])
65
- detail.postcode = postcode[2];
66
- setRegionValue(value);
67
- bindchange && bindchange({
68
- detail
69
- });
70
- };
71
- const onElementLayout = (layout) => {
72
- viewRef.current?.measure((x, y, width, height, offsetLeft, offsetTop) => {
73
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop };
74
- props.getInnerLayout && props.getInnerLayout(layoutRef);
75
- });
76
- };
77
- const onDismiss = () => {
78
- bindcancel && bindcancel();
79
- };
80
- const regionProps = {
81
- ref: nodeRef,
82
- data: formatRegionData,
83
- value: regionvalue,
84
- onChange,
85
- disabled,
86
- onDismiss
87
- };
88
- const touchProps = {
89
- onLayout: onElementLayout,
90
- ref: viewRef
111
+ }
112
+ data = data[newValue[i - 1] - offsetIndex].children;
113
+ const range = data.map(item => item.value);
114
+ ans.indexArr.push(newValue[i]);
115
+ ans.rangeArr.push(customItem ? [customItem, ...range] : range);
116
+ }
117
+ return ans;
118
+ };
119
+ const valueNum2String = (value, customItem = '') => {
120
+ let data = regionData;
121
+ return value.map(index => {
122
+ if (customItem) {
123
+ if (index === 0) {
124
+ return customItem;
125
+ }
126
+ else {
127
+ index -= 1;
128
+ }
129
+ }
130
+ const item = data[index];
131
+ data = item.children;
132
+ return item.value;
133
+ });
134
+ };
135
+ const hasDiff = (currentValue, value, limit = 3) => {
136
+ for (let i = 0; i < limit; i++) {
137
+ if (currentValue[i] !== value[i]) {
138
+ return true;
139
+ }
140
+ }
141
+ return false;
142
+ };
143
+ const PickerRegion = forwardRef((props, ref) => {
144
+ const { value = [], level = 'region', 'custom-item': customItem = '', bindchange } = props;
145
+ const nodeRef = useRef(null);
146
+ const columnLength = useMemo(() => getColumnLength(level), [level]);
147
+ const [formatObj, setFormatObj] = useState(valueStr2Obj(value, columnLength, customItem));
148
+ const updateValue = useCallback((value = []) => {
149
+ const calibratedValue = valueStr2Obj(value, columnLength, customItem);
150
+ setFormatObj(calibratedValue);
151
+ }, [columnLength, customItem]);
152
+ const _props = useRef(props);
153
+ _props.current = props;
154
+ useImperativeHandle(ref, () => ({
155
+ updateValue,
156
+ getNodeInstance: () => ({
157
+ props: _props,
158
+ nodeRef,
159
+ instance: {
160
+ style: {}
161
+ }
162
+ })
163
+ }));
164
+ useUpdateEffect(() => {
165
+ const calibratedValue = valueStr2Obj(value, columnLength, customItem);
166
+ if (hasDiff(formatObj.indexArr, calibratedValue.indexArr, columnLength)) {
167
+ setFormatObj(calibratedValue);
168
+ }
169
+ }, [value, columnLength, customItem]);
170
+ const onChange = useCallback((e) => {
171
+ const { value } = e.detail;
172
+ const currentValue = formatObj.indexArr;
173
+ const newObj = valueChanged2Obj(formatObj, value, columnLength, customItem);
174
+ if (hasDiff(currentValue, value, columnLength)) {
175
+ setFormatObj(newObj);
176
+ }
177
+ bindchange?.({ detail: { value: valueNum2String(newObj.indexArr, customItem) } });
178
+ }, [formatObj, columnLength, customItem, bindchange]);
179
+ const renderColumn = () => {
180
+ return formatObj.rangeArr?.map((item, index) => (
181
+ // @ts-expect-error ignore
182
+ <MpxPickerViewColumn key={index}>
183
+ {item.map((item, index) => {
184
+ const len = item.length;
185
+ const style = extendObject({}, styles.pickerItem, {
186
+ fontSize: len > 5 ? 21 - len : 16
187
+ });
188
+ return <Text key={index} style={style}>
189
+ {item}
190
+ </Text>;
191
+ })}
192
+ </MpxPickerViewColumn>));
91
193
  };
92
- return (<Picker {...regionProps}>
93
- <TouchableWithoutFeedback>
94
- <View {...touchProps}>{children}</View>
95
- </TouchableWithoutFeedback>
96
- </Picker>);
194
+ return (<MpxPickerView style={styles.pickerContainer} indicator-style={styles.pickerIndicator} value={formatObj.indexArr} bindchange={onChange}>
195
+ {renderColumn()}
196
+ </MpxPickerView>);
97
197
  });
98
- _RegionPicker.displayName = 'mpx-picker-region';
99
- export default _RegionPicker;
198
+ PickerRegion.displayName = 'MpxPickerRegion';
199
+ export default PickerRegion;
@@ -1,81 +1,66 @@
1
- /**
2
- * 普通选择器,range可以是Array<Obj> 也可以是Array
3
- */
4
- import { View, TouchableWithoutFeedback } from 'react-native';
5
- import React, { forwardRef, useState, useRef, useEffect } from 'react';
6
- import { Picker } from '@ant-design/react-native';
7
- import useNodesRef from '../useNodesRef'; // 引入辅助函数
8
- const formatRangeFun = (range, rangeKey = '') => {
9
- let newRange = [];
10
- newRange = (range || []).map((item, index) => {
11
- if (typeof item === 'object') {
12
- return { value: index, label: item[rangeKey] };
13
- }
14
- else {
15
- return { value: index, label: item };
16
- }
17
- });
18
- return newRange;
1
+ import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
2
+ import { StyleSheet, Text } from 'react-native';
3
+ import MpxPickerView from '../mpx-picker-view';
4
+ import MpxPickerViewColumn from '../mpx-picker-view-column';
5
+ import { useUpdateEffect } from '../utils';
6
+ const styles = StyleSheet.create({
7
+ pickerContainer: {
8
+ height: 240,
9
+ paddingHorizontal: 10,
10
+ borderTopLeftRadius: 10,
11
+ borderTopRightRadius: 10
12
+ },
13
+ pickerIndicator: {
14
+ height: 45
15
+ },
16
+ pickerItem: {
17
+ fontSize: 18,
18
+ lineHeight: 45,
19
+ textAlign: 'center'
20
+ }
21
+ });
22
+ const formatRangeFun = (range, rangeKey = '') => rangeKey ? range.map((item) => item[rangeKey]) : range;
23
+ const formatValueFn = (value = 0) => {
24
+ const _value = Array.isArray(value) ? value[0] : value;
25
+ return +_value;
19
26
  };
20
- const _SelectorPicker = forwardRef((props, ref) => {
21
- const { range, children, value, disabled, bindchange, bindcancel, style } = props;
22
- // 格式化数据为Array<*>
27
+ const PickerSelector = forwardRef((props, ref) => {
28
+ const { value, range = [], bindchange } = props;
29
+ const [formatValue, setFormatValue] = useState(formatValueFn(value));
23
30
  const formatRange = formatRangeFun(range, props['range-key']);
24
- // 选中的索引值
25
- const [selected, setSelected] = useState(value || 0);
26
- // range数据源
27
- const [data, setData] = useState(formatRange || []);
28
- // 存储layout布局信息
29
- const layoutRef = useRef({});
30
- const viewRef = useRef(null);
31
31
  const nodeRef = useRef(null);
32
- useNodesRef(props, ref, nodeRef, {
33
- style
34
- });
35
- useEffect(() => {
36
- if (range) {
37
- const newFormatRange = formatRangeFun(range, props['range-key']);
38
- setData(newFormatRange);
39
- }
40
- setSelected(() => {
41
- return value;
42
- });
43
- }, [range, value]);
44
- const defaultValue = [value];
45
- const onChange = (value) => {
46
- bindchange && bindchange({
47
- detail: {
48
- value: value && value[0]
49
- }
50
- });
32
+ const updateValue = (value = 0) => {
33
+ const newValue = formatValueFn(value);
34
+ setFormatValue(newValue);
51
35
  };
52
- const onElementLayout = (layout) => {
53
- viewRef.current?.measure((x, y, width, height, offsetLeft, offsetTop) => {
54
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop };
55
- props.getInnerLayout && props.getInnerLayout(layoutRef);
56
- });
57
- };
58
- const antPickerProps = {
59
- ref: nodeRef,
60
- data,
61
- value: [selected],
62
- cols: 1,
63
- defaultValue,
64
- itemHeight: 40,
65
- onChange,
66
- onDismiss: bindcancel && bindcancel
67
- };
68
- const touchProps = {
69
- onLayout: onElementLayout,
70
- ref: viewRef
36
+ useUpdateEffect(() => {
37
+ updateValue(value);
38
+ }, [value]);
39
+ const _props = useRef(props);
40
+ _props.current = props;
41
+ useImperativeHandle(ref, () => ({
42
+ updateValue,
43
+ getNodeInstance: () => ({
44
+ props: _props,
45
+ nodeRef,
46
+ instance: {
47
+ style: {}
48
+ }
49
+ })
50
+ }));
51
+ const onChange = (e) => {
52
+ const { value } = e.detail;
53
+ if (formatValue !== value[0]) {
54
+ setFormatValue(value[0]);
55
+ }
56
+ bindchange?.({ detail: { value: value[0] + '' } });
71
57
  };
72
- return (<Picker {...antPickerProps}>
73
- <TouchableWithoutFeedback>
74
- <View {...touchProps}>
75
- {children}
76
- </View>
77
- </TouchableWithoutFeedback>
78
- </Picker>);
58
+ return (<MpxPickerView style={styles.pickerContainer} indicator-style={styles.pickerIndicator} value={[formatValue]} bindchange={onChange}>
59
+ {/* @ts-expect-error ignore */}
60
+ <MpxPickerViewColumn>
61
+ {formatRange.map((item, index) => (<Text key={index} style={styles.pickerItem}>{item}</Text>))}
62
+ </MpxPickerViewColumn>
63
+ </MpxPickerView>);
79
64
  });
80
- _SelectorPicker.displayName = 'mpx-picker-selector';
81
- export default _SelectorPicker;
65
+ PickerSelector.displayName = 'MpxPickerSelector';
66
+ export default PickerSelector;