@mpxjs/webpack-plugin 2.9.59 → 2.9.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +1 -3
- package/lib/platform/style/wx/index.js +344 -270
- package/lib/platform/template/wx/component-config/checkbox-group.js +8 -0
- package/lib/platform/template/wx/component-config/checkbox.js +8 -0
- package/lib/platform/template/wx/component-config/cover-image.js +15 -0
- package/lib/platform/template/wx/component-config/cover-view.js +9 -0
- package/lib/platform/template/wx/component-config/form.js +13 -1
- package/lib/platform/template/wx/component-config/icon.js +8 -0
- package/lib/platform/template/wx/component-config/index.js +5 -1
- package/lib/platform/template/wx/component-config/label.js +15 -0
- package/lib/platform/template/wx/component-config/movable-area.js +18 -1
- package/lib/platform/template/wx/component-config/movable-view.js +18 -1
- package/lib/platform/template/wx/component-config/navigator.js +8 -0
- package/lib/platform/template/wx/component-config/picker-view-column.js +8 -0
- package/lib/platform/template/wx/component-config/picker-view.js +18 -2
- package/lib/platform/template/wx/component-config/picker.js +14 -1
- package/lib/platform/template/wx/component-config/radio-group.js +8 -0
- package/lib/platform/template/wx/component-config/radio.js +8 -0
- package/lib/platform/template/wx/component-config/root-portal.js +15 -0
- package/lib/platform/template/wx/component-config/switch.js +8 -0
- package/lib/platform/template/wx/component-config/unsupported.js +1 -3
- package/lib/react/processScript.js +2 -0
- package/lib/react/processStyles.js +1 -0
- package/lib/react/processTemplate.js +2 -3
- package/lib/react/style-helper.js +12 -7
- package/lib/runtime/components/react/context.ts +40 -0
- package/lib/runtime/components/react/dist/context.js +8 -0
- package/lib/runtime/components/react/dist/getInnerListeners.js +34 -12
- package/lib/runtime/components/react/dist/mpx-button.jsx +88 -88
- package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +82 -0
- package/lib/runtime/components/react/dist/mpx-checkbox.jsx +139 -0
- package/lib/runtime/components/react/dist/mpx-form.jsx +61 -0
- package/lib/runtime/components/react/dist/mpx-icon.jsx +48 -0
- package/lib/runtime/components/react/dist/mpx-image/index.jsx +39 -43
- package/lib/runtime/components/react/dist/mpx-image/svg.jsx +3 -2
- package/lib/runtime/components/react/dist/mpx-input.jsx +63 -37
- package/lib/runtime/components/react/dist/mpx-label.jsx +55 -0
- package/lib/runtime/components/react/dist/mpx-movable-area.jsx +41 -0
- package/lib/runtime/components/react/dist/mpx-movable-view.jsx +346 -0
- package/lib/runtime/components/react/dist/mpx-navigator.jsx +35 -0
- package/lib/runtime/components/react/dist/mpx-picker/date.jsx +69 -0
- package/lib/runtime/components/react/dist/mpx-picker/index.jsx +138 -0
- package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +142 -0
- package/lib/runtime/components/react/dist/mpx-picker/region.jsx +94 -0
- package/lib/runtime/components/react/dist/mpx-picker/regionData.js +6099 -0
- package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +76 -0
- package/lib/runtime/components/react/dist/mpx-picker/time.jsx +244 -0
- package/lib/runtime/components/react/dist/mpx-picker/type.js +1 -0
- package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +107 -0
- package/lib/runtime/components/react/dist/mpx-picker-view.jsx +162 -0
- package/lib/runtime/components/react/dist/mpx-radio-group.jsx +80 -0
- package/lib/runtime/components/react/dist/mpx-radio.jsx +154 -0
- package/lib/runtime/components/react/dist/mpx-root-portal.jsx +15 -0
- package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +93 -70
- package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +281 -157
- package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +21 -11
- package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +19 -11
- package/lib/runtime/components/react/dist/mpx-switch.jsx +79 -0
- package/lib/runtime/components/react/dist/mpx-text.jsx +21 -49
- package/lib/runtime/components/react/dist/mpx-textarea.jsx +2 -2
- package/lib/runtime/components/react/dist/mpx-view.jsx +451 -146
- package/lib/runtime/components/react/dist/mpx-web-view.jsx +17 -20
- package/lib/runtime/components/react/dist/parser.js +218 -0
- package/lib/runtime/components/react/dist/types/common.js +1 -0
- package/lib/runtime/components/react/dist/useNodesRef.js +3 -8
- package/lib/runtime/components/react/dist/utils.jsx +433 -0
- package/lib/runtime/components/react/getInnerListeners.ts +43 -21
- package/lib/runtime/components/react/mpx-button.tsx +129 -119
- package/lib/runtime/components/react/mpx-checkbox-group.tsx +152 -0
- package/lib/runtime/components/react/mpx-checkbox.tsx +234 -0
- package/lib/runtime/components/react/mpx-form.tsx +117 -0
- package/lib/runtime/components/react/mpx-icon.tsx +106 -0
- package/lib/runtime/components/react/mpx-image/index.tsx +62 -68
- package/lib/runtime/components/react/mpx-image/svg.tsx +7 -5
- package/lib/runtime/components/react/mpx-input.tsx +90 -42
- package/lib/runtime/components/react/mpx-label.tsx +110 -0
- package/lib/runtime/components/react/mpx-movable-area.tsx +81 -0
- package/lib/runtime/components/react/mpx-movable-view.tsx +424 -0
- package/lib/runtime/components/react/mpx-navigator.tsx +67 -0
- package/lib/runtime/components/react/mpx-picker/date.tsx +82 -0
- package/lib/runtime/components/react/mpx-picker/index.tsx +155 -0
- package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +156 -0
- package/lib/runtime/components/react/mpx-picker/region.tsx +107 -0
- package/lib/runtime/components/react/mpx-picker/regionData.ts +6101 -0
- package/lib/runtime/components/react/mpx-picker/selector.tsx +91 -0
- package/lib/runtime/components/react/mpx-picker/time.tsx +270 -0
- package/lib/runtime/components/react/mpx-picker/type.ts +107 -0
- package/lib/runtime/components/react/mpx-picker-view-column.tsx +156 -0
- package/lib/runtime/components/react/mpx-picker-view.tsx +220 -0
- package/lib/runtime/components/react/mpx-radio-group.tsx +150 -0
- package/lib/runtime/components/react/mpx-radio.tsx +230 -0
- package/lib/runtime/components/react/mpx-root-portal.tsx +27 -0
- package/lib/runtime/components/react/mpx-scroll-view.tsx +184 -130
- package/lib/runtime/components/react/mpx-swiper/carouse.tsx +308 -183
- package/lib/runtime/components/react/mpx-swiper/index.tsx +27 -19
- package/lib/runtime/components/react/mpx-swiper/type.ts +23 -5
- package/lib/runtime/components/react/mpx-swiper-item.tsx +49 -14
- package/lib/runtime/components/react/mpx-switch.tsx +148 -0
- package/lib/runtime/components/react/mpx-text.tsx +53 -77
- package/lib/runtime/components/react/mpx-textarea.tsx +3 -3
- package/lib/runtime/components/react/mpx-view.tsx +576 -195
- package/lib/runtime/components/react/mpx-web-view.tsx +34 -39
- package/lib/runtime/components/react/parser.ts +245 -0
- package/lib/runtime/components/react/types/common.ts +12 -0
- package/lib/runtime/components/react/types/getInnerListeners.ts +2 -1
- package/lib/runtime/components/react/types/global.d.ts +17 -1
- package/lib/runtime/components/react/useNodesRef.ts +4 -10
- package/lib/runtime/components/react/utils.tsx +505 -0
- package/lib/runtime/optionProcessor.js +19 -17
- package/lib/template-compiler/compiler.js +84 -61
- package/lib/template-compiler/gen-node-react.js +7 -9
- package/lib/web/processStyles.js +2 -5
- package/package.json +8 -3
- package/lib/runtime/components/react/dist/utils.js +0 -80
- package/lib/runtime/components/react/utils.ts +0 -92
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { View } from 'react-native'
|
|
2
|
+
import { PickerValue } from '@ant-design/react-native'
|
|
3
|
+
import React, { forwardRef, useRef, useContext, useState } from 'react'
|
|
4
|
+
import { warn } from '@mpxjs/utils'
|
|
5
|
+
import useInnerProps, { getCustomEvent } from '../getInnerListeners'
|
|
6
|
+
import useNodesRef, { HandlerRef } from '../useNodesRef' // 引入辅助函数
|
|
7
|
+
import Selector from './selector'
|
|
8
|
+
import TimeSelector from './time'
|
|
9
|
+
import DateSelector from './date'
|
|
10
|
+
import MultiSelector from './multiSelector'
|
|
11
|
+
import RegionSelector from './region'
|
|
12
|
+
import { PickerProps, EventType, ValueType } from './type'
|
|
13
|
+
import { FormContext, FormFieldValue } from '../context'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* ✔ mode
|
|
17
|
+
* ✔ disabled
|
|
18
|
+
* ✔ bindcancel
|
|
19
|
+
* ✔ bindchange
|
|
20
|
+
* ✔ range
|
|
21
|
+
* ✔ range-key
|
|
22
|
+
* ✔ value
|
|
23
|
+
* ✔ start
|
|
24
|
+
* ✔ end
|
|
25
|
+
* ✔ fields 有效值 year,month,day,表示选择器的粒度
|
|
26
|
+
* ✔ end
|
|
27
|
+
* ✔ custom-item
|
|
28
|
+
* ✔ level 选择器层级 province,city,region,<sub-district不支持>
|
|
29
|
+
* ✔ level
|
|
30
|
+
* ✘ header-text
|
|
31
|
+
* ✘ bindcolumnchange
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
const _Picker = forwardRef<HandlerRef<View, PickerProps>, PickerProps>((props: PickerProps, ref): React.JSX.Element => {
|
|
35
|
+
const { mode = 'selector', value, bindcancel, bindchange, children, bindcolumnchange } = props
|
|
36
|
+
const innerLayout = useRef({})
|
|
37
|
+
const { nodeRef } = useNodesRef<View, PickerProps>(props, ref, {
|
|
38
|
+
})
|
|
39
|
+
const innerProps = useInnerProps(props, {
|
|
40
|
+
ref: nodeRef
|
|
41
|
+
}, [], { layoutRef: innerLayout })
|
|
42
|
+
|
|
43
|
+
const [pickerValue, setPickerValue] = useState(value as ValueType)
|
|
44
|
+
const defaultValues = {
|
|
45
|
+
selector: 0,
|
|
46
|
+
multiSelector: [0],
|
|
47
|
+
time: props.start,
|
|
48
|
+
date: props.start,
|
|
49
|
+
region: undefined
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const formContext = useContext(FormContext)
|
|
53
|
+
|
|
54
|
+
let formValuesMap: Map<string, FormFieldValue> | undefined
|
|
55
|
+
|
|
56
|
+
// 判断 context 是否存在,存在的话读取 context 中存的 formValuesMap
|
|
57
|
+
if (formContext) {
|
|
58
|
+
formValuesMap = formContext.formValuesMap
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const resetValue = () => {
|
|
62
|
+
type curMode = keyof typeof defaultValues
|
|
63
|
+
const defalutValue = (defaultValues[mode as curMode] !== undefined ? defaultValues[mode as curMode] : value) as ValueType
|
|
64
|
+
setPickerValue(defalutValue)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const getValue = () => {
|
|
68
|
+
return pickerValue
|
|
69
|
+
}
|
|
70
|
+
if (formValuesMap) {
|
|
71
|
+
if (!props.name) {
|
|
72
|
+
warn('If a form component is used, the name attribute is required.')
|
|
73
|
+
} else {
|
|
74
|
+
formValuesMap.set(props.name, { getValue, resetValue })
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const getInnerLayout = (layout: React.MutableRefObject<{}>) => {
|
|
79
|
+
innerLayout.current = layout.current
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const onChange = (event: EventType) => {
|
|
83
|
+
const eventData = getCustomEvent('change', {}, { detail: event.detail, layoutRef: innerLayout })
|
|
84
|
+
bindchange && bindchange(eventData)
|
|
85
|
+
setPickerValue(event.detail.value as ValueType)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const columnChange = (value: PickerValue[], index: number) => {
|
|
89
|
+
// type: "columnchange", detail: {column: 1, value: 2}
|
|
90
|
+
const eventData = getCustomEvent('columnchange', {}, { detail: { column: index, value }, layoutRef: innerLayout })
|
|
91
|
+
bindcolumnchange && bindcolumnchange(eventData)
|
|
92
|
+
}
|
|
93
|
+
const commonProps = {
|
|
94
|
+
...{ innerProps },
|
|
95
|
+
mode,
|
|
96
|
+
children,
|
|
97
|
+
bindchange: onChange,
|
|
98
|
+
bindcolumnchange: columnChange,
|
|
99
|
+
bindcancel,
|
|
100
|
+
getInnerLayout
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const selectorProps = {
|
|
104
|
+
...commonProps,
|
|
105
|
+
value: pickerValue as PickerValue,
|
|
106
|
+
range: props.range,
|
|
107
|
+
'range-key': props['range-key']
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const multiProps = {
|
|
111
|
+
...commonProps,
|
|
112
|
+
value: pickerValue as Array<number>,
|
|
113
|
+
range: props.range,
|
|
114
|
+
'range-key': props['range-key']
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const timeProps = {
|
|
118
|
+
...commonProps,
|
|
119
|
+
value: pickerValue as string,
|
|
120
|
+
start: props.start,
|
|
121
|
+
end: props.end
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const dateProps = {
|
|
125
|
+
...commonProps,
|
|
126
|
+
value: pickerValue as string,
|
|
127
|
+
start: props.start,
|
|
128
|
+
end: props.end,
|
|
129
|
+
fileds: props.fields || 'day'
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const regionProps = {
|
|
133
|
+
...commonProps,
|
|
134
|
+
value: pickerValue as Array<string>,
|
|
135
|
+
level: props.level || 'sub-district'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (mode === 'selector') {
|
|
139
|
+
return <Selector {...selectorProps}></Selector>
|
|
140
|
+
} else if (mode === 'multiSelector') {
|
|
141
|
+
return <MultiSelector {...multiProps}></MultiSelector>
|
|
142
|
+
} else if (mode === 'time') {
|
|
143
|
+
return <TimeSelector {...timeProps}></TimeSelector>
|
|
144
|
+
} else if (mode === 'date') {
|
|
145
|
+
return <DateSelector {...dateProps}></DateSelector>
|
|
146
|
+
} else if (mode === 'region') {
|
|
147
|
+
return <RegionSelector {...regionProps}></RegionSelector>
|
|
148
|
+
} else {
|
|
149
|
+
return <View>只支持selector, multiSelector, time, date, region 这些类型</View>
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
_Picker.displayName = 'mpx-picker'
|
|
154
|
+
|
|
155
|
+
export default _Picker
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { View, TouchableWithoutFeedback } from 'react-native'
|
|
2
|
+
import { Picker, PickerValue } from '@ant-design/react-native'
|
|
3
|
+
import React, { forwardRef, useState, useRef, useEffect } from 'react'
|
|
4
|
+
import { MultiSelectorProps, LayoutType } from './type'
|
|
5
|
+
import useNodesRef, { HandlerRef } from '../useNodesRef' // 引入辅助函数
|
|
6
|
+
|
|
7
|
+
function convertToObj (item?: any, rangeKey = ''): any {
|
|
8
|
+
if (typeof item === 'object') {
|
|
9
|
+
return { value: item[rangeKey], label: item[rangeKey] }
|
|
10
|
+
} else {
|
|
11
|
+
return { value: item, label: item }
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// eslint-disable-next-line default-param-last
|
|
16
|
+
function formatRangeFun (range: any[][] = [], rangeKey?: string): any[] {
|
|
17
|
+
const result = (range[0] || []).map(item => {
|
|
18
|
+
return convertToObj(item, rangeKey)
|
|
19
|
+
})
|
|
20
|
+
let tmp = result
|
|
21
|
+
for (let i = 1; i < range.length; i++) {
|
|
22
|
+
const child = Array.isArray(range[i]) ? range[i] : []
|
|
23
|
+
const nextColData = child.map(item => {
|
|
24
|
+
return convertToObj(item, rangeKey)
|
|
25
|
+
})
|
|
26
|
+
tmp.forEach(item => {
|
|
27
|
+
item.children = nextColData
|
|
28
|
+
})
|
|
29
|
+
tmp = nextColData
|
|
30
|
+
}
|
|
31
|
+
return result
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getIndexByValues (range: any[] = [], value: any[] = []): number[] {
|
|
35
|
+
let tmp = range
|
|
36
|
+
return value.map(v => {
|
|
37
|
+
for (let i = 0; i < tmp.length; i++) {
|
|
38
|
+
if (tmp[i].value === v) {
|
|
39
|
+
tmp = tmp[i].children || []
|
|
40
|
+
return i
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return 0
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
// [1,1,2] 寻找出[]
|
|
47
|
+
function getInnerValueByIndex (range: any[] = [], value: any[] = []): string[] {
|
|
48
|
+
let tmp = range
|
|
49
|
+
return value.map(v => {
|
|
50
|
+
const current = tmp[v].value
|
|
51
|
+
tmp = tmp[v].children
|
|
52
|
+
return current
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
// column = 1 value = ['无脊柱动物', '扁性动物', '吸血虫'] 根据column 和value 获取到当前列变动选择的值所在当前列的索引
|
|
56
|
+
function getColumnIndexByValue (range: any[] = [], column: number, value: any[] = []): number {
|
|
57
|
+
let curRange = range
|
|
58
|
+
let changeIndex = 0
|
|
59
|
+
let tmpRange: any[] = []
|
|
60
|
+
value.map((item, index) => {
|
|
61
|
+
if (column === index) {
|
|
62
|
+
curRange.map((ritem, rindex) => {
|
|
63
|
+
if (ritem.value === item) {
|
|
64
|
+
changeIndex = rindex
|
|
65
|
+
}
|
|
66
|
+
return ritem
|
|
67
|
+
})
|
|
68
|
+
} else {
|
|
69
|
+
curRange.map((citem, cindex) => {
|
|
70
|
+
if (citem.value === item) {
|
|
71
|
+
tmpRange = citem.children
|
|
72
|
+
}
|
|
73
|
+
return citem
|
|
74
|
+
})
|
|
75
|
+
curRange = tmpRange
|
|
76
|
+
}
|
|
77
|
+
return item
|
|
78
|
+
})
|
|
79
|
+
return changeIndex
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const _MultiSelectorPicker = forwardRef<HandlerRef<View, MultiSelectorProps>, MultiSelectorProps>((props: MultiSelectorProps, ref): React.JSX.Element => {
|
|
83
|
+
const { range, value, disabled, bindchange, bindcancel, children, bindcolumnchange } = props
|
|
84
|
+
const formatRange = formatRangeFun(range, props['range-key'])
|
|
85
|
+
const initValue = getInnerValueByIndex(formatRange, value)
|
|
86
|
+
// 选中的索引值
|
|
87
|
+
const [selected, setSelected] = useState(initValue)
|
|
88
|
+
// range数据源
|
|
89
|
+
const [data, setData] = useState(formatRange || [])
|
|
90
|
+
// 存储layout布局信息
|
|
91
|
+
const layoutRef = useRef({})
|
|
92
|
+
const { nodeRef: viewRef } = useNodesRef<View, MultiSelectorProps>(props, ref, {
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (range) {
|
|
97
|
+
const newFormatRange = formatRangeFun(range, props['range-key'])
|
|
98
|
+
setData(newFormatRange)
|
|
99
|
+
}
|
|
100
|
+
const newValue = getInnerValueByIndex(formatRange, value)
|
|
101
|
+
value && setSelected(newValue)
|
|
102
|
+
}, [range, value])
|
|
103
|
+
|
|
104
|
+
const onChange = (value: PickerValue[]) => {
|
|
105
|
+
// e.detail.value 都是索引multi
|
|
106
|
+
const strIndex = getIndexByValues(data, value)
|
|
107
|
+
bindchange && bindchange({
|
|
108
|
+
detail: {
|
|
109
|
+
value: strIndex
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const onPickerChange = (value: PickerValue[], column: number) => {
|
|
115
|
+
// onPickerChange--- ["无脊柱动物", "节肢动物", "吸血虫"] 1 拿着column
|
|
116
|
+
const changeIndex = getColumnIndexByValue(data, column, value)
|
|
117
|
+
bindcolumnchange && bindcolumnchange(changeIndex, column)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const onElementLayout = () => {
|
|
121
|
+
viewRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
|
|
122
|
+
layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
|
|
123
|
+
props.getInnerLayout && props.getInnerLayout(layoutRef)
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const antPickerProps = {
|
|
128
|
+
data,
|
|
129
|
+
value: selected,
|
|
130
|
+
cols: range.length,
|
|
131
|
+
defaultValue: initValue,
|
|
132
|
+
disabled,
|
|
133
|
+
itemHeight: 40,
|
|
134
|
+
onChange,
|
|
135
|
+
onDismiss: bindcancel && bindcancel,
|
|
136
|
+
onPickerChange: onPickerChange
|
|
137
|
+
} as any
|
|
138
|
+
|
|
139
|
+
const touchProps = {
|
|
140
|
+
onLayout: onElementLayout,
|
|
141
|
+
ref: viewRef
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return (<Picker {...antPickerProps}>
|
|
145
|
+
<TouchableWithoutFeedback>
|
|
146
|
+
<View {...touchProps}>
|
|
147
|
+
{children}
|
|
148
|
+
</View>
|
|
149
|
+
</TouchableWithoutFeedback>
|
|
150
|
+
</Picker>
|
|
151
|
+
)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
_MultiSelectorPicker.displayName = 'mpx-picker-multiselector'
|
|
155
|
+
|
|
156
|
+
export default _MultiSelectorPicker
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { View, TouchableWithoutFeedback } from 'react-native'
|
|
2
|
+
import { Picker, PickerColumnItem } from '@ant-design/react-native'
|
|
3
|
+
import { regionData } from './regionData'
|
|
4
|
+
import React, { forwardRef, useState, useRef } from 'react'
|
|
5
|
+
import useNodesRef, { HandlerRef } from '../useNodesRef' // 引入辅助函数
|
|
6
|
+
import { RegionProps, RegionObj, LayoutType } from './type'
|
|
7
|
+
|
|
8
|
+
function formateRegionData (clObj: RegionObj[] = [], customItem?: string, depth = 2): PickerColumnItem[] {
|
|
9
|
+
const l = depth
|
|
10
|
+
// 'PickerData[]' is not assignable to type 'PickerColumn | PickerColumn[]'.
|
|
11
|
+
// const obj: PickerColumnItem[] = []
|
|
12
|
+
const obj: PickerColumnItem[] = []
|
|
13
|
+
if (customItem) {
|
|
14
|
+
const objClone: PickerColumnItem = {
|
|
15
|
+
value: customItem,
|
|
16
|
+
label: customItem,
|
|
17
|
+
children: []
|
|
18
|
+
}
|
|
19
|
+
const panding = { ...objClone }
|
|
20
|
+
let loop = panding
|
|
21
|
+
while (depth-- > 0) {
|
|
22
|
+
loop.children = [{ ...objClone }]
|
|
23
|
+
loop = loop.children[0] as PickerColumnItem
|
|
24
|
+
}
|
|
25
|
+
obj.push(panding as PickerColumnItem)
|
|
26
|
+
}
|
|
27
|
+
for (let i = 0; i < clObj.length; i++) {
|
|
28
|
+
const region: PickerColumnItem = {
|
|
29
|
+
value: clObj[i].value,
|
|
30
|
+
label: clObj[i].value
|
|
31
|
+
}
|
|
32
|
+
if (clObj[i].children) {
|
|
33
|
+
region.children = formateRegionData(clObj[i].children, customItem, l - 1)
|
|
34
|
+
}
|
|
35
|
+
obj.push(region)
|
|
36
|
+
}
|
|
37
|
+
return obj
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const _RegionPicker = forwardRef<HandlerRef<View, RegionProps>, RegionProps>((props: RegionProps, ref): React.JSX.Element => {
|
|
41
|
+
const { children, value, bindchange, bindcancel, disabled } = props
|
|
42
|
+
const formatRegionData = formateRegionData(regionData, props['custom-item'])
|
|
43
|
+
|
|
44
|
+
const [regionvalue, setRegionValue] = useState(value)
|
|
45
|
+
// 存储layout布局信息
|
|
46
|
+
const layoutRef = useRef({})
|
|
47
|
+
const { nodeRef: viewRef } = useNodesRef<View, RegionProps>(props, ref, {
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const onChange = (value: string[]): void => {
|
|
51
|
+
// 通过 value 查找 code
|
|
52
|
+
let tmp: RegionObj[] = regionData
|
|
53
|
+
const postcode: (string | undefined)[] = []
|
|
54
|
+
const code = value.map((item) => {
|
|
55
|
+
for (let i = 0; i < tmp.length; i++) {
|
|
56
|
+
if (tmp[i].value === item) {
|
|
57
|
+
const code = tmp[i].code
|
|
58
|
+
postcode.push(tmp[i].postcode)
|
|
59
|
+
tmp = tmp[i].children || []
|
|
60
|
+
return code
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return item
|
|
64
|
+
}).filter(code => !!code)
|
|
65
|
+
const detail: Record<string, any> = { value, code }
|
|
66
|
+
if (postcode[2]) detail.postcode = postcode[2]
|
|
67
|
+
setRegionValue(value)
|
|
68
|
+
bindchange && bindchange({
|
|
69
|
+
detail
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const onElementLayout = (layout: LayoutType) => {
|
|
74
|
+
viewRef.current?.measure((x: number, y: number, width: number, height: number, offsetLeft: number, offsetTop: number) => {
|
|
75
|
+
layoutRef.current = { x, y, width, height, offsetLeft, offsetTop }
|
|
76
|
+
props.getInnerLayout && props.getInnerLayout(layoutRef)
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const onDismiss = (): void => {
|
|
81
|
+
bindcancel && bindcancel()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const regionProps = {
|
|
85
|
+
data: formatRegionData,
|
|
86
|
+
value: regionvalue,
|
|
87
|
+
onChange,
|
|
88
|
+
disabled,
|
|
89
|
+
onDismiss
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const touchProps = {
|
|
93
|
+
onLayout: onElementLayout,
|
|
94
|
+
ref: viewRef
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<Picker {...regionProps}>
|
|
99
|
+
<TouchableWithoutFeedback>
|
|
100
|
+
<View {...touchProps}>{children}</View>
|
|
101
|
+
</TouchableWithoutFeedback>
|
|
102
|
+
</Picker>
|
|
103
|
+
)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
_RegionPicker.displayName = 'mpx-picker-region'
|
|
107
|
+
export default _RegionPicker
|