mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-01 11:39:28 +08:00
Upload onChang(file) argument should be immutable, close #5280
This commit is contained in:
parent
7290e1070b
commit
89ace33480
@ -1,10 +1,14 @@
|
||||
export default function getFileItem(file, fileList) {
|
||||
let matchWay = (!file.uid) ? 'byName' : 'byUid';
|
||||
let target = fileList.filter((item) => {
|
||||
if (matchWay === 'byName') {
|
||||
return item.name === file.name;
|
||||
}
|
||||
return item.uid === file.uid;
|
||||
})[0];
|
||||
return target;
|
||||
export function getFileItem(file, fileList) {
|
||||
const matchKey = file.uid ? 'uid' : 'name';
|
||||
const target = fileList.filter(item => item[matchKey] === file[matchKey])[0];
|
||||
return { ...target };
|
||||
}
|
||||
|
||||
export function removeFileItem(file, fileList) {
|
||||
const matchKey = file.uid ? 'uid' : 'name';
|
||||
const removed = fileList.filter(item => item[matchKey] !== file[matchKey]);
|
||||
if (removed.length === fileList.length) {
|
||||
return null;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import React from 'react';
|
||||
import RcUpload from 'rc-upload';
|
||||
import UploadList from './uploadList';
|
||||
import getFileItem from './getFileItem';
|
||||
import { getFileItem, removeFileItem } from './getFileItem';
|
||||
import classNames from 'classnames';
|
||||
import assign from 'object-assign';
|
||||
import { UploadProps, UploadLocale } from './interface';
|
||||
@ -146,17 +146,6 @@ export default class Upload extends React.Component<UploadProps, any> {
|
||||
}, 200);
|
||||
}
|
||||
|
||||
removeFile(file) {
|
||||
let fileList = this.state.fileList;
|
||||
let targetItem = getFileItem(file, fileList);
|
||||
let index = fileList.indexOf(targetItem);
|
||||
if (index !== -1) {
|
||||
fileList.splice(index, 1);
|
||||
return fileList;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
onSuccess = (response, file) => {
|
||||
this.clearProgressTimer();
|
||||
try {
|
||||
@ -215,14 +204,15 @@ export default class Upload extends React.Component<UploadProps, any> {
|
||||
const { onRemove } = this.props;
|
||||
// Prevent removing file
|
||||
const onRemoveReturnValue = onRemove && onRemove(file);
|
||||
if (onRemoveReturnValue !== false) {
|
||||
let fileList = this.removeFile(file);
|
||||
if (fileList) {
|
||||
this.onChange({
|
||||
file,
|
||||
fileList,
|
||||
});
|
||||
}
|
||||
if (onRemoveReturnValue === false) {
|
||||
return;
|
||||
}
|
||||
const removedFileList = removeFileItem(file, this.state.fileList);
|
||||
if (removedFileList) {
|
||||
this.onChange({
|
||||
file,
|
||||
fileList: removedFileList,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user