|
@@ -0,0 +1,37 @@
|
|
1
|
+import React, { useEffect, useState } from 'react'
|
|
2
|
+import { Picker } from '@tarojs/components'
|
|
3
|
+
|
|
4
|
+export default props => {
|
|
5
|
+ const [index, setIndex] = useState()
|
|
6
|
+
|
|
7
|
+ const handleChange = e => {
|
|
8
|
+ const inx = e.detail.value - 0
|
|
9
|
+ const item = props.range[inx]
|
|
10
|
+ const value = item[props.rangeValue]
|
|
11
|
+ if (props.onChange) {
|
|
12
|
+ props.onChange(value, item)
|
|
13
|
+ }
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ useEffect(() => {
|
|
17
|
+ if (!props.range || !props.range.length) {
|
|
18
|
+ setIndex()
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ const found = false
|
|
22
|
+ for (let i = 0; i < props.range.length; i ++) {
|
|
23
|
+ const item = props.range[i]
|
|
24
|
+ if (item[props.rangeValue] === props.value) {
|
|
25
|
+ setIndex(i)
|
|
26
|
+ found = true
|
|
27
|
+ }
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ if (!found) {
|
|
31
|
+ setIndex()
|
|
32
|
+ }
|
|
33
|
+ }, [props.value, props.range, props.rangeValue])
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+ return <Picker {...props} value={index} onChange={handleChange}>{props.children}</Picker>
|
|
37
|
+}
|