张延森 il y a 4 ans
Parent
révision
f18bd6243c
2 fichiers modifiés avec 39 ajouts et 2 suppressions
  1. 2
    2
      config/dev.js
  2. 37
    0
      src/components/Picker/index.jsx

+ 2
- 2
config/dev.js Voir le fichier

@@ -3,8 +3,8 @@ module.exports = {
3 3
     NODE_ENV: '"development"'
4 4
   },
5 5
   defineConstants: {
6
-    HOST: '"https://xs.ycjcjy.com"'
7
-    // HOST: '"http://127.0.0.1:6060"'
6
+    // HOST: '"https://xs.ycjcjy.com"'
7
+    HOST: '"http://127.0.0.1:6060"'
8 8
   },
9 9
   mini: {
10 10
     debugReact: true

+ 37
- 0
src/components/Picker/index.jsx Voir le fichier

@@ -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
+}