Skip to content

Commit 763e429

Browse files
committed
fix(ui): fix update upload list
1 parent 3d4333a commit 763e429

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

packages/ui/src/components/upload/Upload.tsx

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ export interface DUploadProps extends React.InputHTMLAttributes<HTMLInputElement
6363
onRemove?: (file: DUploadFile) => void;
6464
}
6565

66-
const UID_KEY = Symbol();
67-
6866
const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DUpload' as const });
6967
function Upload(props: DUploadProps, ref: React.ForwardedRef<HTMLInputElement>): JSX.Element | null {
7068
const {
@@ -120,74 +118,72 @@ function Upload(props: DUploadProps, ref: React.ForwardedRef<HTMLInputElement>):
120118
const xhr = new XMLHttpRequest();
121119

122120
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], {
164139
type: 'update',
165-
files: [newList.find((f) => f[UID_KEY] === uid)!],
140+
files: [addFile],
166141
});
142+
return;
167143
}
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+
});
168164
}
169165
};
170166

171167
const upload = (condition: boolean | string | Blob) => {
172168
if (condition === false) {
173-
update({ status: null }, true);
169+
add({ status: null });
174170
} else {
175-
const abort = update({ status: 'progress', percent: 0 }, true);
171+
const abort = add({ status: 'progress', percent: 0 });
176172
if (abort) {
177173
return;
178174
}
179175

180176
xhr.upload.addEventListener('progress', (e) => {
181177
if (e.lengthComputable) {
182178
const percent = Math.round((e.loaded * 100) / e.total);
183-
update({ status: 'progress', percent }, false);
179+
update({ status: 'progress', percent });
184180
}
185181
});
186182
xhr.addEventListener('error', () => {
187-
update({ status: 'error', percent: undefined }, false);
183+
update({ status: 'error', percent: undefined });
188184
});
189185
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 });
191187
});
192188

193189
const {
@@ -228,6 +224,7 @@ function Upload(props: DUploadProps, ref: React.ForwardedRef<HTMLInputElement>):
228224
}
229225
}
230226
}
227+
231228
if (addList.length > 0) {
232229
const newList = changeFileList((draft) => draft.concat(addList));
233230
onModelChange?.(newList, {

packages/ui/src/hooks/useDValue.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { DraftFunction } from '@react-devui/hooks/useImmer';
33

44
import { freeze, produce } from 'immer';
55
import { isFunction, isUndefined } from 'lodash';
6-
import { useState } from 'react';
6+
import { useRef, useState } from 'react';
77

88
export function useDValue<T, S = T>(
99
initialValue: T | (() => T),
@@ -13,13 +13,15 @@ export function useDValue<T, S = T>(
1313
formControlInject?: DFormControlInject
1414
): [T, (arg: S | DraftFunction<S>) => S] {
1515
const [_value, setValue] = useState(initialValue);
16-
const currentValue: T = isUndefined(formControlInject) ? (isUndefined(value) ? _value : value) : formControlInject[0];
16+
17+
const valueRef = useRef<T>(_value);
18+
valueRef.current = isUndefined(formControlInject) ? (isUndefined(value) ? _value : value) : formControlInject[0];
1719

1820
return [
19-
currentValue,
21+
valueRef.current,
2022
(updater: any) => {
21-
const newValue = isFunction(updater) ? produce(currentValue, updater) : freeze(updater);
22-
const shouldUpdate = deepCompare ? !deepCompare(currentValue, newValue) : !Object.is(currentValue, newValue);
23+
const newValue = isFunction(updater) ? produce(valueRef.current, updater) : freeze(updater);
24+
const shouldUpdate = deepCompare ? !deepCompare(valueRef.current, newValue) : !Object.is(valueRef.current, newValue);
2325
if (shouldUpdate) {
2426
setValue(newValue);
2527
onChange?.(newValue);

0 commit comments

Comments
 (0)