魏超 5 years ago
parent
commit
5b871cfa15
1 changed files with 24 additions and 5 deletions
  1. 24
    5
      src/components/SelectButton/NewTypeSelect.jsx

+ 24
- 5
src/components/SelectButton/NewTypeSelect.jsx View File

1
-import React, { useState, useEffect } from 'react';
1
+import React, { useState, useEffect, useRef } from 'react';
2
 import { Select } from 'antd';
2
 import { Select } from 'antd';
3
 
3
 
4
 import request from '../../utils/request'
4
 import request from '../../utils/request'
5
 
5
 
6
 const { Option } = Select;
6
 const { Option } = Select;
7
 
7
 
8
+function usePrevious(props) {
9
+  const ref = useRef();
10
+  useEffect(() => {
11
+    ref.current = props;
12
+  });
13
+  return ref.current;
14
+}
15
+
8
 /**
16
 /**
9
  *
17
  *
10
  *
18
  *
13
  */
21
  */
14
 const NewsTypeSelect = (props) => {
22
 const NewsTypeSelect = (props) => {
15
   const [ data, setData ] = useState([])
23
   const [ data, setData ] = useState([])
24
+  const [ value, setValue ] = useState('')
25
+  const preProps = usePrevious(props)
26
+
27
+  if ((!preProps || !preProps.value) && props.value && !value) {
28
+    setValue(props.value)
29
+  }
16
 
30
 
17
   useEffect(() => {
31
   useEffect(() => {
18
-    getCityList();
32
+    getNewsTypeList();
19
   },[])
33
   },[])
20
 
34
 
21
-  const getCityList = (e) => {
35
+  const getNewsTypeList = (e) => {
22
     request({
36
     request({
23
         url: '/api/admin/taNewsType',
37
         url: '/api/admin/taNewsType',
24
         method: 'GET',
38
         method: 'GET',
29
     })
43
     })
30
   }
44
   }
31
 
45
 
46
+  const handleChange = (e) => {
47
+    setValue(e)
48
+    props.onChange(e)
49
+  }
50
+
32
   return (
51
   return (
33
-      <Select value={props.value} style={{ width: '180px' }} placeholder="请选择资讯类型" onChange={props.onChange}>
52
+      <Select value={'' === value ? undefined : value} style={{ width: '180px' }} placeholder="请选择资讯类型" onChange={handleChange}>
34
           {data.map(type => (
53
           {data.map(type => (
35
-            <Option key={type.newsTypeId}>{type.newsTypeName}</Option>
54
+            <Option key={type.newsTypeId} value={type.newsTypeId}>{type.newsTypeName}</Option>
36
           ))}
55
           ))}
37
       </Select>
56
       </Select>
38
   )
57
   )