Yansen il y a 11 mois
Parent
révision
36bcd0682d
1 fichiers modifiés avec 34 ajouts et 18 suppressions
  1. 34
    18
      src/components/Uploader/index.jsx

+ 34
- 18
src/components/Uploader/index.jsx Voir le fichier

@@ -13,12 +13,17 @@ export default (props) => {
13 13
   // 单个元素结构为 { url, fileType }
14 14
   const { value, disabled = false, onChange } = props;
15 15
   const [loading, setLoading] = React.useState(false);
16
+  const [valueCopy, setValueCopy] = React.useState(value);
17
+  const valueCopyRef = React.useRef(valueCopy);
18
+  valueCopyRef.current = valueCopy;
16 19
 
17 20
   const onAdd = (e) => {
18 21
     e.preventDefault();
19 22
     e.stopPropagation();
20 23
 
21 24
     if (disabled) return;
25
+
26
+    setValueCopy(valueCopyRef.current.concat(false));
22 27
     
23 28
     setLoading(true);
24 29
 
@@ -42,12 +47,13 @@ export default (props) => {
42 47
               reject(err);
43 48
             });
44 49
         });
45
-      }, 60 * 1000);
50
+      }, 10 * 1000);
46 51
 
47 52
       p.then((resp) => {
48 53
         setLoading(false);
49 54
         onChange((value || []).concat(resp));
50 55
       }).catch(() => {
56
+        setValueCopy(valueCopyRef.current.filter(Boolean))
51 57
         setLoading(false);
52 58
       });
53 59
     } else {
@@ -75,6 +81,11 @@ export default (props) => {
75 81
   };
76 82
 
77 83
   const onDelete = (item) => {
84
+    if (!item) {
85
+      setValueCopy(valueCopyRef.current.filter(Boolean));
86
+      return;
87
+    }
88
+
78 89
     const newVal = value.filter((x) => x.url != item.url);
79 90
     onChange(newVal);
80 91
   };
@@ -93,32 +104,37 @@ export default (props) => {
93 104
     });
94 105
   };
95 106
 
107
+  React.useEffect(() => {
108
+    setValueCopy(value || []);
109
+  }, [value]);
110
+
96 111
   return (
97 112
     <View className={style["uploader-wrapper"]}>
98 113
       <View className={style["uploader-box"]}>
99 114
         {!disabled && (
100 115
           <View className={style["uploader-add"]}>
101 116
             <Image src={icon} onClick={onAdd} />
102
-            {loading && (
103
-              <View>
104
-                <Loading color="#fff" />
105
-              </View>
106
-            )}
107 117
           </View>
108 118
         )}
109
-        {(value || []).map((item, inx) => (
110
-          <View key={item.url} className={style["uploader-item"]}>
111
-            {item.fileType.indexOf("image") == 0 ? (
112
-              <Image src={item.url} onClick={() => onPreview(inx)} />
113
-            ) : (
114
-              <Video
115
-                src={item.url}
116
-                controls={false}
117
-                onClick={() => onPreview(inx)}
118
-              />
119
-            )}
119
+        {valueCopy.map((item, inx) => (
120
+          <View key={item?.url || '123'} className={style["uploader-item"]}>
121
+            {
122
+              !item ? (
123
+                <Loading color="gray" style={{ width: '100%', height: '100%' }} />
124
+              ) : (
125
+                item.fileType.indexOf("image") == 0 ? (
126
+                  <Image src={item.url} onClick={() => onPreview(inx)} />
127
+                ) : (
128
+                  <Video
129
+                    src={item.url}
130
+                    controls={false}
131
+                    onClick={() => onPreview(inx)}
132
+                  />
133
+                )
134
+              )
135
+            }
120 136
             {!disabled && (
121
-              <View onClick={() => onDelete(item)}>
137
+              <View onClick={() => onDelete(item)} style={{ zIndex: 99 }}>
122 138
                 <Image src={closeIcon} />
123 139
               </View>
124 140
             )}