Upload onChang(file) argument should be immutable, close #5280

This commit is contained in:
afc163 2017-03-14 14:21:01 +08:00
parent 7290e1070b
commit 89ace33480
2 changed files with 23 additions and 29 deletions

View File

@ -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;
}

View File

@ -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,
});
}
}