Merge branch 'joesonw-uploadList#1' into develop-1.0.0

This commit is contained in:
afc163 2016-04-05 13:35:02 +08:00
commit 8420909c75
5 changed files with 73 additions and 31 deletions

View File

@ -1,6 +1,6 @@
--- ---
order: 5 order: 5
hidden: 5 hidden: true
title: 多文件选择 title: 多文件选择
--- ---

View File

@ -6,32 +6,57 @@ title: 图片卡片样式
上传文件为图片,可展示本地缩略图。 上传文件为图片,可展示本地缩略图。
````jsx ````jsx
import { Upload, Icon } from 'antd'; import { Upload, Icon, Modal } from 'antd';
const props = { const ImageUploadList = React.createClass({
action: '/upload.do', getInitialState() {
listType: 'picture-card', return {
defaultFileList: [{ priviewVisible: false,
uid: -1, priviewImage: '',
name: 'xxx.png', };
status: 'done', },
url: 'https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png', handleCancel() {
thumbUrl: 'https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png', this.setState({
}] priviewVisible: false,
}; });
},
render() {
const props = {
action: '/upload.do',
listType: 'picture-card',
defaultFileList: [{
uid: -1,
name: 'xxx.png',
status: 'done',
url: 'https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png',
thumbUrl: 'https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png',
}],
onPreview: (file) => {
this.setState({
priviewImage: file.url,
priviewVisible: true,
});
}
};
return (
<div className="clearfix">
<Upload {...props}>
<Icon type="plus" />
<div className="ant-upload-text">上传照片</div>
</Upload>
<a href="https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png" target="_blank" className="upload-example">
<img src="https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png" />
<span>示例</span>
</a>
<Modal visible={this.state.priviewVisible} footer={null} onCancel={this.handleCancel}>
<img src={this.state.priviewImage} />
</Modal>
</div>
);
}
});
ReactDOM.render( ReactDOM.render(<ImageUploadList />, mountNode);
<div className="clearfix">
<Upload {...props}>
<Icon type="plus" />
<div className="ant-upload-text">上传照片</div>
</Upload>
<a href="https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png" target="_blank" className="upload-example">
<img src="https://os.alipayobjects.com/rmsportal/NDbkJhpzmLxtPhB.png" />
<span>示例</span>
</a>
</div>
, mountNode);
```` ````
````css ````css

View File

@ -188,11 +188,21 @@ export default class Upload extends React.Component {
} }
} }
handlePreview = (file) => {
if ('onPreview' in this.props) {
this.props.onPreview(file);
}
}
handleManualRemove = (file) => { handleManualRemove = (file) => {
/*eslint-disable */ /*eslint-disable */
file.status = 'removed'; file.status = 'removed';
/*eslint-enable */ /*eslint-enable */
this.handleRemove(file); if ('onRemove' in this.props) {
this.props.onRemove(file);
} else {
this.handleRemove(file);
}
} }
onChange = (info) => { onChange = (info) => {
@ -235,6 +245,7 @@ export default class Upload extends React.Component {
uploadList = ( uploadList = (
<UploadList listType={this.props.listType} <UploadList listType={this.props.listType}
items={this.state.fileList} items={this.state.fileList}
onPreview={this.handlePreview}
onRemove={this.handleManualRemove} /> onRemove={this.handleManualRemove} />
); );
} }

View File

@ -29,8 +29,8 @@ english: Upload
| beforeUpload | 可选参数, 上传文件之前的钩子,参数为上传的文件,若返回 `false` 或者 Promise 则停止上传。**注意:该方法不支持老 IE**。 | Function | 无 | | beforeUpload | 可选参数, 上传文件之前的钩子,参数为上传的文件,若返回 `false` 或者 Promise 则停止上传。**注意:该方法不支持老 IE**。 | Function | 无 |
| onChange | 可选参数, 上传文件改变时的状态,详见 onChange | Function | 无 | | onChange | 可选参数, 上传文件改变时的状态,详见 onChange | Function | 无 |
| listType | 上传列表的内建样式,支持两种基本样式 `text` or `picture` | String | 'text'| | listType | 上传列表的内建样式,支持两种基本样式 `text` or `picture` | String | 'text'|
| className | 自定义类名 | String | 无 | | onPreview | 点击文件链接时的回调 | Function(file) | 无 |
| onRemove | 点击移除文件时的回调 | Function(file) | 无 |
### onChange ### onChange

View File

@ -24,10 +24,15 @@ export default class UploadList extends React.Component {
} }
}; };
handleClose(file) { handleClose = (file) => {
this.props.onRemove(file); this.props.onRemove(file);
} }
handlePreview = (file, e) => {
e.preventDefault();
return this.props.onPreview(file);
}
componentDidUpdate() { componentDidUpdate() {
if (this.props.listType !== 'picture' && this.props.listType !== 'picture-card') { if (this.props.listType !== 'picture' && this.props.listType !== 'picture-card') {
return; return;
@ -66,6 +71,7 @@ export default class UploadList extends React.Component {
} }
} else { } else {
icon = (<a className={`${prefixCls}-list-item-thumbnail`} icon = (<a className={`${prefixCls}-list-item-thumbnail`}
onClick={(e) => this.handlePreview(file, e)}
href={file.url} href={file.url}
target="_blank"><img src={file.thumbUrl || file.url} alt={file.name} /></a> target="_blank"><img src={file.thumbUrl || file.url} alt={file.name} /></a>
); );
@ -88,13 +94,13 @@ export default class UploadList extends React.Component {
<div className={`${prefixCls}-list-item-info`}> <div className={`${prefixCls}-list-item-info`}>
{icon} {icon}
{file.url {file.url
? <a href={file.url} target="_blank" className={`${prefixCls}-list-item-name`}>{file.name}</a> ? <a onClick={(e) => this.handlePreview(file, e)} href={file.url} target="_blank" className={`${prefixCls}-list-item-name`}>{file.name}</a>
: <span className={`${prefixCls}-list-item-name`}>{file.name}</span>} : <span className={`${prefixCls}-list-item-name`}>{file.name}</span>}
{ {
this.props.listType === 'picture-card' && file.status !== 'uploading' this.props.listType === 'picture-card' && file.status !== 'uploading'
? ( ? (
<span> <span>
<a href={file.url} target="_blank" style={{ pointerEvents: file.url ? '' : 'none' }}><Icon type="eye-o" /></a> <a onClick={(e) => this.handlePreview(file, e)} href={file.url} target="_blank" style={{ pointerEvents: file.url ? '' : 'none' }}><Icon type="eye-o" /></a>
<Icon type="delete" onClick={() => this.handleClose(file)} /> <Icon type="delete" onClick={() => this.handleClose(file)} />
</span> </span>
) : <Icon type="cross" onClick={() => this.handleClose(file)} /> ) : <Icon type="cross" onClick={() => this.handleClose(file)} />