@mpxjs/webpack-plugin 2.10.3 → 2.10.4

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 (103) 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 -3
  4. package/lib/platform/style/wx/index.js +23 -12
  5. package/lib/platform/template/wx/component-config/button.js +19 -2
  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/input.js +18 -3
  16. package/lib/platform/template/wx/component-config/label.js +4 -0
  17. package/lib/platform/template/wx/component-config/movable-area.js +7 -1
  18. package/lib/platform/template/wx/component-config/movable-view.js +12 -3
  19. package/lib/platform/template/wx/component-config/navigator.js +4 -0
  20. package/lib/platform/template/wx/component-config/picker-view-column.js +4 -0
  21. package/lib/platform/template/wx/component-config/picker-view.js +7 -1
  22. package/lib/platform/template/wx/component-config/picker.js +7 -1
  23. package/lib/platform/template/wx/component-config/radio-group.js +4 -0
  24. package/lib/platform/template/wx/component-config/radio.js +4 -0
  25. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  26. package/lib/platform/template/wx/component-config/root-portal.js +4 -0
  27. package/lib/platform/template/wx/component-config/scroll-view.js +10 -2
  28. package/lib/platform/template/wx/component-config/swiper-item.js +7 -1
  29. package/lib/platform/template/wx/component-config/swiper.js +12 -3
  30. package/lib/platform/template/wx/component-config/switch.js +4 -0
  31. package/lib/platform/template/wx/component-config/text.js +24 -3
  32. package/lib/platform/template/wx/component-config/textarea.js +17 -2
  33. package/lib/platform/template/wx/component-config/unsupported.js +7 -0
  34. package/lib/platform/template/wx/component-config/video.js +10 -2
  35. package/lib/platform/template/wx/component-config/view.js +11 -1
  36. package/lib/platform/template/wx/component-config/web-view.js +4 -0
  37. package/lib/platform/template/wx/index.js +42 -75
  38. package/lib/react/processScript.js +1 -18
  39. package/lib/runtime/components/react/dist/event.config.js +1 -0
  40. package/lib/runtime/components/react/dist/getInnerListeners.js +18 -7
  41. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +1 -1
  42. package/lib/runtime/components/react/dist/mpx-inline-text.jsx +11 -0
  43. package/lib/runtime/components/react/dist/mpx-input.jsx +3 -6
  44. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.jsx +2 -2
  45. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +194 -68
  46. package/lib/runtime/components/react/dist/mpx-picker/dateData.js +17 -0
  47. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +178 -96
  48. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +79 -139
  49. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +190 -90
  50. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +60 -75
  51. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +100 -228
  52. package/lib/runtime/components/react/dist/{mpx-picker-view.jsx → mpx-picker-view/index.jsx} +3 -3
  53. package/lib/runtime/components/react/dist/{mpx-picker-view-column.jsx → mpx-picker-view-column/index.jsx} +64 -16
  54. package/lib/runtime/components/react/dist/{mpx-picker-view-column-item.jsx → mpx-picker-view-column/pickerViewColumnItem.jsx} +8 -5
  55. package/lib/runtime/components/react/dist/{pickerFaces.js → mpx-picker-view-column/pickerViewFaces.js} +6 -0
  56. package/lib/runtime/components/react/dist/mpx-popup/index.jsx +61 -0
  57. package/lib/runtime/components/react/dist/mpx-popup/popupBase.jsx +92 -0
  58. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +192 -25
  59. package/lib/runtime/components/react/dist/mpx-simple-text.jsx +8 -7
  60. package/lib/runtime/components/react/dist/mpx-simple-view.jsx +11 -15
  61. package/lib/runtime/components/react/dist/mpx-video.jsx +3 -3
  62. package/lib/runtime/components/react/dist/mpx-web-view.jsx +7 -4
  63. package/lib/runtime/components/react/dist/utils.jsx +2 -1
  64. package/lib/runtime/components/react/event.config.ts +2 -0
  65. package/lib/runtime/components/react/getInnerListeners.ts +28 -25
  66. package/lib/runtime/components/react/mpx-canvas/index.tsx +2 -2
  67. package/lib/runtime/components/react/mpx-inline-text.tsx +18 -0
  68. package/lib/runtime/components/react/mpx-input.tsx +2 -6
  69. package/lib/runtime/components/react/mpx-keyboard-avoiding-view.tsx +1 -1
  70. package/lib/runtime/components/react/mpx-picker/date.tsx +226 -69
  71. package/lib/runtime/components/react/mpx-picker/dateData.ts +22 -0
  72. package/lib/runtime/components/react/mpx-picker/index.tsx +239 -118
  73. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +96 -139
  74. package/lib/runtime/components/react/mpx-picker/region.tsx +217 -89
  75. package/lib/runtime/components/react/mpx-picker/selector.tsx +75 -80
  76. package/lib/runtime/components/react/mpx-picker/time.tsx +119 -236
  77. package/lib/runtime/components/react/mpx-picker/type.ts +85 -71
  78. package/lib/runtime/components/react/{mpx-picker-view.tsx → mpx-picker-view/index.tsx} +7 -7
  79. package/lib/runtime/components/react/{mpx-picker-view-column.tsx → mpx-picker-view-column/index.tsx} +70 -19
  80. package/lib/runtime/components/react/{mpx-picker-view-column-item.tsx → mpx-picker-view-column/pickerViewColumnItem.tsx} +8 -5
  81. package/lib/runtime/components/react/{pickerFaces.ts → mpx-picker-view-column/pickerViewFaces.ts} +7 -0
  82. package/lib/runtime/components/react/mpx-popup/index.tsx +86 -0
  83. package/lib/runtime/components/react/mpx-popup/popupBase.tsx +130 -0
  84. package/lib/runtime/components/react/mpx-scroll-view.tsx +249 -43
  85. package/lib/runtime/components/react/mpx-simple-text.tsx +10 -8
  86. package/lib/runtime/components/react/mpx-simple-view.tsx +11 -16
  87. package/lib/runtime/components/react/mpx-video.tsx +2 -2
  88. package/lib/runtime/components/react/mpx-web-view.tsx +7 -4
  89. package/lib/runtime/components/react/types/getInnerListeners.d.ts +5 -1
  90. package/lib/runtime/components/react/types/global.d.ts +1 -1
  91. package/lib/runtime/components/react/utils.tsx +3 -2
  92. package/lib/runtime/components/web/mini-video-controls.min.js +1 -1
  93. package/lib/runtime/components/web/mpx-input.vue +1 -1
  94. package/lib/runtime/stringify.wxs +2 -2
  95. package/lib/template-compiler/compiler.js +8 -8
  96. package/lib/utils/env.js +1 -1
  97. package/package.json +4 -5
  98. /package/lib/runtime/components/react/dist/{pickerVIewContext.js → mpx-picker-view/pickerVIewContext.js} +0 -0
  99. /package/lib/runtime/components/react/dist/{pickerViewIndicator.jsx → mpx-picker-view-column/pickerViewIndicator.jsx} +0 -0
  100. /package/lib/runtime/components/react/dist/{pickerViewMask.jsx → mpx-picker-view-column/pickerViewMask.jsx} +0 -0
  101. /package/lib/runtime/components/react/{pickerVIewContext.ts → mpx-picker-view/pickerVIewContext.ts} +0 -0
  102. /package/lib/runtime/components/react/{pickerViewIndicator.tsx → mpx-picker-view-column/pickerViewIndicator.tsx} +0 -0
  103. /package/lib/runtime/components/react/{pickerViewMask.tsx → mpx-picker-view-column/pickerViewMask.tsx} +0 -0
@@ -1,147 +1,87 @@
1
- import { View, TouchableWithoutFeedback } from 'react-native';
2
- import { Picker } from '@ant-design/react-native';
3
- import React, { forwardRef, useState, useRef, useEffect } from 'react';
4
- import useNodesRef from '../useNodesRef'; // 引入辅助函数
5
- function convertToObj(item, rangeKey = '') {
6
- if (typeof item === 'object') {
7
- return { value: item[rangeKey], label: item[rangeKey] };
1
+ import React, { forwardRef, useCallback, 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
+ const styles = StyleSheet.create({
6
+ pickerContainer: {
7
+ height: 240,
8
+ paddingHorizontal: 10,
9
+ borderTopLeftRadius: 10,
10
+ borderTopRightRadius: 10
11
+ },
12
+ pickerIndicator: {
13
+ height: 45
14
+ },
15
+ pickerItem: {
16
+ fontSize: 18,
17
+ lineHeight: 45,
18
+ textAlign: 'center'
8
19
  }
9
- else {
10
- return { value: item, label: item };
11
- }
12
- }
13
- // eslint-disable-next-line default-param-last
14
- function formatRangeFun(range = [], rangeKey) {
15
- const result = (range[0] || []).map(item => {
16
- return convertToObj(item, rangeKey);
17
- });
18
- let tmp = result;
19
- for (let i = 1; i < range.length; i++) {
20
- const child = Array.isArray(range[i]) ? range[i] : [];
21
- const nextColData = child.map(item => {
22
- return convertToObj(item, rangeKey);
23
- });
24
- tmp.forEach(item => {
25
- item.children = nextColData;
26
- });
27
- tmp = nextColData;
28
- }
29
- return result;
30
- }
31
- function getIndexByValues(range = [], value = []) {
32
- let tmp = range;
33
- return value.map(v => {
34
- for (let i = 0; i < tmp.length; i++) {
35
- if (tmp[i].value === v) {
36
- tmp = tmp[i].children || [];
37
- return i;
38
- }
39
- }
40
- return 0;
41
- });
42
- }
43
- // [1,1,2] 寻找出[]
44
- function getInnerValueByIndex(range = [], value = []) {
45
- let tmp = range;
46
- return value.map(v => {
47
- const current = tmp[v].value;
48
- tmp = tmp[v].children;
49
- return current;
50
- });
51
- }
52
- // column = 1 value = ['无脊柱动物', '扁性动物', '吸血虫'] 根据column 和value 获取到当前列变动选择的值所在当前列的索引
53
- function getColumnIndexByValue(range = [], column, value = []) {
54
- let curRange = range;
55
- let changeIndex = 0;
56
- let tmpRange = [];
57
- value.map((item, index) => {
58
- if (column === index) {
59
- curRange.map((ritem, rindex) => {
60
- if (ritem.value === item) {
61
- changeIndex = rindex;
62
- }
63
- return ritem;
64
- });
65
- }
66
- else {
67
- curRange.map((citem, cindex) => {
68
- if (citem.value === item) {
69
- tmpRange = citem.children;
70
- }
71
- return citem;
72
- });
73
- curRange = tmpRange;
74
- }
75
- return item;
76
- });
77
- return changeIndex;
78
- }
79
- const _MultiSelectorPicker = forwardRef((props, ref) => {
80
- const { range, value, disabled, bindchange, bindcancel, children, bindcolumnchange, style } = props;
81
- const formatRange = formatRangeFun(range, props['range-key']);
82
- const initValue = getInnerValueByIndex(formatRange, value);
83
- // 选中的索引值
84
- const [selected, setSelected] = useState(initValue);
85
- // range数据源
86
- const [data, setData] = useState(formatRange || []);
87
- // 存储layout布局信息
88
- const layoutRef = useRef({});
89
- const viewRef = useRef(null);
20
+ });
21
+ const formatRangeFun = (range, rangeKey = '') => rangeKey ? range.map((item) => item[rangeKey]) : range;
22
+ const formatValueFn = (value) => {
23
+ return Array.isArray(value) ? value : [value];
24
+ };
25
+ const hasDiff = (a, b) => {
26
+ return a.length !== b.length || a.some((item, index) => item !== b[index]);
27
+ };
28
+ const PickerMultiSelector = forwardRef((props, ref) => {
29
+ const { value = [], range = [], bindchange, bindcolumnchange } = props;
30
+ const _value = formatValueFn(value);
31
+ const [formatValue, setFormatValue] = useState(_value);
32
+ const [formatRange, setFormatRange] = useState(formatRangeFun(range, props['range-key']));
90
33
  const nodeRef = useRef(null);
91
- useNodesRef(props, ref, nodeRef, {
92
- style
93
- });
94
- useEffect(() => {
95
- if (range) {
96
- const newFormatRange = formatRangeFun(range, props['range-key']);
97
- setData(newFormatRange);
34
+ const updateValue = useCallback((value = []) => {
35
+ let newValue = formatValueFn(value);
36
+ if (newValue.length === 0) {
37
+ newValue = formatValue.map(() => 0);
98
38
  }
99
- const newValue = getInnerValueByIndex(formatRange, value);
100
- value && setSelected(newValue);
101
- }, [range, value]);
102
- const onChange = (value) => {
103
- // e.detail.value 都是索引multi
104
- const strIndex = getIndexByValues(data, value);
105
- bindchange && bindchange({
106
- detail: {
107
- value: strIndex
108
- }
109
- });
110
- };
111
- const onPickerChange = (value, column) => {
112
- // onPickerChange--- ["无脊柱动物", "节肢动物", "吸血虫"] 1 拿着column
113
- const changeIndex = getColumnIndexByValue(data, column, value);
114
- bindcolumnchange && bindcolumnchange(changeIndex, column);
39
+ checkColumnChange(newValue, formatValue);
40
+ if (hasDiff(newValue, formatValue)) {
41
+ setFormatValue(newValue);
42
+ }
43
+ }, [formatValue]);
44
+ const updateRange = (newRange) => {
45
+ const range = formatRangeFun(newRange.slice(), props['range-key']);
46
+ setFormatRange(range);
115
47
  };
116
- const onElementLayout = () => {
117
- viewRef.current?.measure((x, y, width, height, offsetLeft, offsetTop) => {
118
- layoutRef.current = { x, y, width, height, offsetLeft, offsetTop };
119
- props.getInnerLayout && props.getInnerLayout(layoutRef);
120
- });
48
+ const _props = useRef(props);
49
+ _props.current = props;
50
+ useImperativeHandle(ref, () => ({
51
+ updateValue,
52
+ updateRange,
53
+ getNodeInstance: () => ({
54
+ props: _props,
55
+ nodeRef,
56
+ instance: {
57
+ style: {}
58
+ }
59
+ })
60
+ }));
61
+ const onChange = (e) => {
62
+ const { value } = e.detail;
63
+ checkColumnChange(value, formatValue);
64
+ bindchange?.({ detail: { value: value } });
65
+ if (hasDiff(value, formatValue)) {
66
+ setFormatValue(value.slice());
67
+ }
121
68
  };
122
- const antPickerProps = {
123
- ref: nodeRef,
124
- data,
125
- value: selected,
126
- cols: range.length,
127
- defaultValue: initValue,
128
- disabled,
129
- itemHeight: 40,
130
- onChange,
131
- onDismiss: bindcancel && bindcancel,
132
- onPickerChange: onPickerChange
69
+ const checkColumnChange = (value, formatValue) => {
70
+ const index = value.findIndex((v, i) => v !== formatValue[i]);
71
+ if (index !== -1) {
72
+ bindcolumnchange?.(index, value[index]);
73
+ }
133
74
  };
134
- const touchProps = {
135
- onLayout: onElementLayout,
136
- ref: viewRef
75
+ const renderColumn = (columnData, index) => {
76
+ return (
77
+ // @ts-expect-error ignore
78
+ <MpxPickerViewColumn key={index}>
79
+ {columnData.map((item, index) => (<Text key={index} style={styles.pickerItem}>{item}</Text>))}
80
+ </MpxPickerViewColumn>);
137
81
  };
138
- return (<Picker {...antPickerProps}>
139
- <TouchableWithoutFeedback>
140
- <View {...touchProps}>
141
- {children}
142
- </View>
143
- </TouchableWithoutFeedback>
144
- </Picker>);
82
+ return (<MpxPickerView style={styles.pickerContainer} indicator-style={styles.pickerIndicator} value={formatValue} bindchange={onChange}>
83
+ {formatRange.map((item, index) => (renderColumn(item, index)))}
84
+ </MpxPickerView>);
145
85
  });
146
- _MultiSelectorPicker.displayName = 'mpx-picker-multiselector';
147
- export default _MultiSelectorPicker;
86
+ PickerMultiSelector.displayName = 'MpxPickerMultiSelector';
87
+ export default PickerMultiSelector;
@@ -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;