Yansen 11 months ago
parent
commit
36bcd0682d
1 changed files with 34 additions and 18 deletions
  1. 34
    18
      src/components/Uploader/index.jsx

+ 34
- 18
src/components/Uploader/index.jsx View File

13
   // 单个元素结构为 { url, fileType }
13
   // 单个元素结构为 { url, fileType }
14
   const { value, disabled = false, onChange } = props;
14
   const { value, disabled = false, onChange } = props;
15
   const [loading, setLoading] = React.useState(false);
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
   const onAdd = (e) => {
20
   const onAdd = (e) => {
18
     e.preventDefault();
21
     e.preventDefault();
19
     e.stopPropagation();
22
     e.stopPropagation();
20
 
23
 
21
     if (disabled) return;
24
     if (disabled) return;
25
+
26
+    setValueCopy(valueCopyRef.current.concat(false));
22
     
27
     
23
     setLoading(true);
28
     setLoading(true);
24
 
29
 
42
               reject(err);
47
               reject(err);
43
             });
48
             });
44
         });
49
         });
45
-      }, 60 * 1000);
50
+      }, 10 * 1000);
46
 
51
 
47
       p.then((resp) => {
52
       p.then((resp) => {
48
         setLoading(false);
53
         setLoading(false);
49
         onChange((value || []).concat(resp));
54
         onChange((value || []).concat(resp));
50
       }).catch(() => {
55
       }).catch(() => {
56
+        setValueCopy(valueCopyRef.current.filter(Boolean))
51
         setLoading(false);
57
         setLoading(false);
52
       });
58
       });
53
     } else {
59
     } else {
75
   };
81
   };
76
 
82
 
77
   const onDelete = (item) => {
83
   const onDelete = (item) => {
84
+    if (!item) {
85
+      setValueCopy(valueCopyRef.current.filter(Boolean));
86
+      return;
87
+    }
88
+
78
     const newVal = value.filter((x) => x.url != item.url);
89
     const newVal = value.filter((x) => x.url != item.url);
79
     onChange(newVal);
90
     onChange(newVal);
80
   };
91
   };
93
     });
104
     });
94
   };
105
   };
95
 
106
 
107
+  React.useEffect(() => {
108
+    setValueCopy(value || []);
109
+  }, [value]);
110
+
96
   return (
111
   return (
97
     <View className={style["uploader-wrapper"]}>
112
     <View className={style["uploader-wrapper"]}>
98
       <View className={style["uploader-box"]}>
113
       <View className={style["uploader-box"]}>
99
         {!disabled && (
114
         {!disabled && (
100
           <View className={style["uploader-add"]}>
115
           <View className={style["uploader-add"]}>
101
             <Image src={icon} onClick={onAdd} />
116
             <Image src={icon} onClick={onAdd} />
102
-            {loading && (
103
-              <View>
104
-                <Loading color="#fff" />
105
-              </View>
106
-            )}
107
           </View>
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
             {!disabled && (
136
             {!disabled && (
121
-              <View onClick={() => onDelete(item)}>
137
+              <View onClick={() => onDelete(item)} style={{ zIndex: 99 }}>
122
                 <Image src={closeIcon} />
138
                 <Image src={closeIcon} />
123
               </View>
139
               </View>
124
             )}
140
             )}