魏超 5 年之前
父節點
當前提交
5b871cfa15
共有 1 個檔案被更改,包括 24 行新增5 行删除
  1. 24
    5
      src/components/SelectButton/NewTypeSelect.jsx

+ 24
- 5
src/components/SelectButton/NewTypeSelect.jsx 查看文件

@@ -1,10 +1,18 @@
1
-import React, { useState, useEffect } from 'react';
1
+import React, { useState, useEffect, useRef } from 'react';
2 2
 import { Select } from 'antd';
3 3
 
4 4
 import request from '../../utils/request'
5 5
 
6 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,12 +21,18 @@ const { Option } = Select;
13 21
  */
14 22
 const NewsTypeSelect = (props) => {
15 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 31
   useEffect(() => {
18
-    getCityList();
32
+    getNewsTypeList();
19 33
   },[])
20 34
 
21
-  const getCityList = (e) => {
35
+  const getNewsTypeList = (e) => {
22 36
     request({
23 37
         url: '/api/admin/taNewsType',
24 38
         method: 'GET',
@@ -29,10 +43,15 @@ const NewsTypeSelect = (props) => {
29 43
     })
30 44
   }
31 45
 
46
+  const handleChange = (e) => {
47
+    setValue(e)
48
+    props.onChange(e)
49
+  }
50
+
32 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 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 56
       </Select>
38 57
   )