@@ -63,8 +63,6 @@ export interface DUploadProps extends React.InputHTMLAttributes<HTMLInputElement
63
63
onRemove ?: ( file : DUploadFile ) => void ;
64
64
}
65
65
66
- const UID_KEY = Symbol ( ) ;
67
-
68
66
const { COMPONENT_NAME } = registerComponentMate ( { COMPONENT_NAME : 'DUpload' as const } ) ;
69
67
function Upload ( props : DUploadProps , ref : React . ForwardedRef < HTMLInputElement > ) : JSX . Element | null {
70
68
const {
@@ -120,74 +118,72 @@ function Upload(props: DUploadProps, ref: React.ForwardedRef<HTMLInputElement>):
120
118
const xhr = new XMLHttpRequest ( ) ;
121
119
122
120
let uid : DId = getUID ( ) ;
123
- const update = ( obj : any , add : boolean ) => {
124
- if ( add ) {
125
- const fileURL = URL . createObjectURL ( file ) ;
126
- dataRef . current . fileURLs . push ( fileURL ) ;
127
- const addFile = {
128
- uid,
129
- [ UID_KEY ] : uid ,
130
- url : fileURL ,
131
- thumbUrl : file . type . startsWith ( 'image' ) ? fileURL : undefined ,
132
- name : file . name ,
133
- originFile : file ,
134
- response : xhr . response ,
135
- ...obj ,
136
- } ;
137
-
138
- if ( isNumber ( dMax ) && fileList . length + addList . length >= dMax ) {
139
- if ( dMax === 1 ) {
140
- uid = addFile . uid = addFile [ UID_KEY ] = fileList [ 0 ] . uid ;
141
- changeFileList ( [ addFile ] ) ;
142
- onModelChange ?.( [ addFile ] , {
143
- type : 'update' ,
144
- files : [ addFile ] ,
145
- } ) ;
146
- return ;
147
- }
148
- return true ;
149
- }
150
-
151
- addList . push ( addFile ) ;
152
- } else {
153
- let hasChange = true ;
154
- const newList = changeFileList ( ( draft ) => {
155
- const index = draft . findIndex ( ( f ) => f [ UID_KEY ] === uid ) ;
156
- if ( index !== - 1 ) {
157
- draft [ index ] = Object . assign ( draft [ index ] , { response : xhr . response , ...obj } ) ;
158
- } else {
159
- hasChange = false ;
160
- }
161
- } ) ;
162
- if ( hasChange ) {
163
- onModelChange ?.( newList , {
121
+ const add = ( obj : any ) => {
122
+ const fileURL = URL . createObjectURL ( file ) ;
123
+ dataRef . current . fileURLs . push ( fileURL ) ;
124
+ const addFile = {
125
+ uid,
126
+ url : fileURL ,
127
+ thumbUrl : file . type . startsWith ( 'image' ) ? fileURL : undefined ,
128
+ name : file . name ,
129
+ originFile : file ,
130
+ response : xhr . response ,
131
+ ...obj ,
132
+ } ;
133
+
134
+ if ( isNumber ( dMax ) && fileList . length + addList . length >= dMax ) {
135
+ if ( dMax === 1 ) {
136
+ uid = addFile . uid = fileList [ 0 ] . uid ;
137
+ changeFileList ( [ addFile ] ) ;
138
+ onModelChange ?.( [ addFile ] , {
164
139
type : 'update' ,
165
- files : [ newList . find ( ( f ) => f [ UID_KEY ] === uid ) ! ] ,
140
+ files : [ addFile ] ,
166
141
} ) ;
142
+ return ;
167
143
}
144
+ return true ;
145
+ }
146
+
147
+ addList . push ( addFile ) ;
148
+ } ;
149
+ const update = ( obj : any ) => {
150
+ let hasChange = true ;
151
+ const newList = changeFileList ( ( draft ) => {
152
+ const index = draft . findIndex ( ( f ) => f . uid === uid ) ;
153
+ if ( index !== - 1 ) {
154
+ draft [ index ] = Object . assign ( draft [ index ] , { response : xhr . response , ...obj } ) ;
155
+ } else {
156
+ hasChange = false ;
157
+ }
158
+ } ) ;
159
+ if ( hasChange ) {
160
+ onModelChange ?.( newList , {
161
+ type : 'update' ,
162
+ files : [ newList . find ( ( f ) => f . uid === uid ) ! ] ,
163
+ } ) ;
168
164
}
169
165
} ;
170
166
171
167
const upload = ( condition : boolean | string | Blob ) => {
172
168
if ( condition === false ) {
173
- update ( { status : null } , true ) ;
169
+ add ( { status : null } ) ;
174
170
} else {
175
- const abort = update ( { status : 'progress' , percent : 0 } , true ) ;
171
+ const abort = add ( { status : 'progress' , percent : 0 } ) ;
176
172
if ( abort ) {
177
173
return ;
178
174
}
179
175
180
176
xhr . upload . addEventListener ( 'progress' , ( e ) => {
181
177
if ( e . lengthComputable ) {
182
178
const percent = Math . round ( ( e . loaded * 100 ) / e . total ) ;
183
- update ( { status : 'progress' , percent } , false ) ;
179
+ update ( { status : 'progress' , percent } ) ;
184
180
}
185
181
} ) ;
186
182
xhr . addEventListener ( 'error' , ( ) => {
187
- update ( { status : 'error' , percent : undefined } , false ) ;
183
+ update ( { status : 'error' , percent : undefined } ) ;
188
184
} ) ;
189
185
xhr . addEventListener ( 'load' , ( ) => {
190
- update ( { status : xhr . status >= 200 && xhr . status < 300 ? 'load' : 'error' , percent : undefined } , false ) ;
186
+ update ( { status : xhr . status >= 200 && xhr . status < 300 ? 'load' : 'error' , percent : undefined } ) ;
191
187
} ) ;
192
188
193
189
const {
@@ -228,6 +224,7 @@ function Upload(props: DUploadProps, ref: React.ForwardedRef<HTMLInputElement>):
228
224
}
229
225
}
230
226
}
227
+
231
228
if ( addList . length > 0 ) {
232
229
const newList = changeFileList ( ( draft ) => draft . concat ( addList ) ) ;
233
230
onModelChange ?.( newList , {
0 commit comments