From af30f55e7a5e7517ef8eddc4b3af8ddfec1e01a6 Mon Sep 17 00:00:00 2001 From: yiminghe Date: Tue, 1 Sep 2015 11:26:33 +0800 Subject: [PATCH] optimize --- components/select/index.md | 1 + components/upload/demo/defaultFileList.md | 4 +- components/upload/demo/fileList.md | 4 +- components/upload/index.jsx | 55 ++++++++++++----------- components/upload/index.md | 6 +-- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/components/select/index.md b/components/select/index.md index b10e3eea8c..4abd1515af 100644 --- a/components/select/index.md +++ b/components/select/index.md @@ -35,6 +35,7 @@ | allowClear | 显示清除按钮 | | false | | placeholder | 选择框默认文字 | string | 无 | | searchPlaceholder | 搜索框默认文字 | string | 无 | +| optionFilterProp | 输入项过滤对应的 option 属性 | string | value | | combobox | 输入框自动提示模式 | | false | | size | 选择框大小 | String | 无 | diff --git a/components/upload/demo/defaultFileList.md b/components/upload/demo/defaultFileList.md index 7de15e10c2..0cbdc7d096 100644 --- a/components/upload/demo/defaultFileList.md +++ b/components/upload/demo/defaultFileList.md @@ -18,12 +18,12 @@ var props = { } }, defaultFileList: [{ - uid: 12345, + uid: -1, name: 'xxx.png', status: 'done', url: 'http://www.baidu.com/xxx.png' }, { - uid: 12346, + uid: -2, name: 'yyy.png', status: 'done', url: 'http://www.baidu.com/yyy.png' diff --git a/components/upload/demo/fileList.md b/components/upload/demo/fileList.md index d359cff48e..ecc371814f 100644 --- a/components/upload/demo/fileList.md +++ b/components/upload/demo/fileList.md @@ -15,7 +15,7 @@ ````jsx var Upload = antd.Upload; var fileList = [{ - uid: 123, + uid: -1, name: 'xxx.png', status: 'done', url: 'http://www.baidu.com/xxx.png' @@ -32,7 +32,7 @@ var MyUpload = React.createClass({ // 1. 上传列表数量的限制 // 只显示最近上传的一个,旧的会被新的顶掉 - fileList = fileList.slice(-1); + fileList = fileList.slice(-2); // 2. 读取远程路径并显示链接 fileList = fileList.map(function(file) { diff --git a/components/upload/index.jsx b/components/upload/index.jsx index 246560e35f..e8f2b1c665 100644 --- a/components/upload/index.jsx +++ b/components/upload/index.jsx @@ -16,10 +16,10 @@ const AntUpload = React.createClass({ }; }, onStart(file) { - let nextFileList = this.state.fileList; + let nextFileList = this.state.fileList.concat(); file.status = 'uploading'; nextFileList.push(file); - this.props.onChange({ + this.onChange({ file: file, fileList: nextFileList }); @@ -31,30 +31,34 @@ const AntUpload = React.createClass({ let index = fileList.indexOf(targetItem); if (index !== -1) { fileList.splice(index, 1); + return fileList; } - return fileList; + return null; }, onSuccess(response, file) { let fileList = this.state.fileList.concat(); Message.success(file.name + '上传完成。'); let targetItem = getFileItem(file, fileList); - targetItem.status = 'done'; - targetItem.response = response; - // 解析出文件上传后的远程地址 - if (typeof this.props.urlResolver === 'function') { - targetItem.url = this.props.urlResolver(response); + // 之前已经删除 + if (targetItem) { + targetItem.status = 'done'; + targetItem.response = response; + this.onChange({ + file: targetItem, + fileList: this.state.fileList + }); } - this.onChange({ - file: targetItem, - fileList: this.state.fileList - }); }, onProgress(e, file) { - this.onChange({ - event: e, - file: file, - fileList: this.state.fileList - }); + let fileList = this.state.fileList; + let targetItem = getFileItem(file, fileList); + if (targetItem) { + this.onChange({ + event: e, + file: file, + fileList: this.state.fileList + }); + } }, onError(error, response, file) { Message.error(file.name + ' 上传失败。'); @@ -64,10 +68,12 @@ const AntUpload = React.createClass({ }, handleRemove(file) { let fileList = this.removeFile(file); - this.onChange({ - file: file, - fileList: fileList - }); + if (fileList) { + this.onChange({ + file: file, + fileList: fileList + }); + } }, onChange(info) { // 1. 有设置外部属性时不改变 fileList @@ -88,11 +94,6 @@ const AntUpload = React.createClass({ data: {}, accept: '', onChange: noop, - urlResolver: function(response) { - try { - return JSON.parse(response).url; - } catch(e) {} - } }; }, componentWillReceiveProps(nextProps) { @@ -129,7 +130,7 @@ const AntUpload = React.createClass({ + onRemove={this.handleRemove}/> ); } diff --git a/components/upload/index.md b/components/upload/index.md index 536696aa78..1b2640c408 100644 --- a/components/upload/index.md +++ b/components/upload/index.md @@ -43,9 +43,9 @@ ```js { - uid: 'uid', // 文件唯一标识 + uid: 'uid', // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突 name: 'xx.png' // 文件名 - status: 'done', // 状态:uploading done removed + status: 'done', // 状态:uploading done response: '{"status":"success"}' // 服务端响应内容 } ``` @@ -55,4 +55,4 @@ ## 显示下载链接 -默认会解析服务器返回数据的 `body.url` 作为下载链接,需要自行配置请使用 fileList 属性进行展示控制。 +请使用 fileList 属性设置数组项的 url 属性进行展示控制。