Skip to content

Commit

Permalink
fix: upload thumbUrl preview not work
Browse files Browse the repository at this point in the history
  • Loading branch information
tangjinzhou committed Jul 3, 2018
1 parent 19698a7 commit 1584b38
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions components/upload/Upload.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './u

export { UploadProps }

function noop () {}

export default {
name: 'AUpload',
Dragger: Dragger,
Expand Down Expand Up @@ -189,9 +187,11 @@ export default {
},
on: {
remove: this.handleManualRemove,
preview: this.$listeners.preview || noop,
},
}
if (this.$listeners.preview) {
uploadListProps.on.preview = this.$listeners.preview
}
return (
<UploadList
{...uploadListProps}
Expand Down
4 changes: 4 additions & 0 deletions components/upload/UploadList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export default {
this.$emit('remove', file)
},
handlePreview (file, e) {
const { preview } = this.$listeners
if(!preview) {
return
}
e.preventDefault()
return this.$emit('preview', file)
},
Expand Down
23 changes: 5 additions & 18 deletions components/upload/__tests__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,18 @@ exports[`renders ./components/upload/demo/picture-card.md correctly 1`] = `
`;
exports[`renders ./components/upload/demo/picture-style.md correctly 1`] = `
<div><span><div class="ant-upload-list ant-upload-list-picture-card"><div class="ant-upload-list-item ant-upload-list-item-done"><div class="ant-upload-list-item-info"><span><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" class="ant-upload-list-item-thumbnail"><img src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" alt="xxx.png"></a><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" title="xxx.png" class="ant-upload-list-item-name">xxx.png</a></span></div>
<span
class="ant-upload-list-item-actions">
<a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" title="Preview file">
<i class="anticon anticon-eye-o"></i>
</a>
<i title="Remove file" class="anticon anticon-delete"></i>
</span>
<div><span><div class="ant-upload ant-upload-select ant-upload-select-picture"><!----></div><div class="ant-upload-list ant-upload-list-picture"><div class="ant-upload-list-item ant-upload-list-item-done"><div class="ant-upload-list-item-info"><span><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" class="ant-upload-list-item-thumbnail"><img src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" alt="xxx.png"></a><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" title="xxx.png" class="ant-upload-list-item-name">xxx.png</a></span></div>
<i
class="anticon anticon-cross"></i>
<!---->
</div>
<div class="ant-upload-list-item ant-upload-list-item-done">
<div class="ant-upload-list-item-info"><span><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" class="ant-upload-list-item-thumbnail"><img src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" alt="yyy.png"></a><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" title="yyy.png" class="ant-upload-list-item-name">yyy.png</a></span></div>
<span
class="ant-upload-list-item-actions">
<a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" title="Preview file">
<i class="anticon anticon-eye-o"></i>
</a>
<i title="Remove file" class="anticon anticon-delete"></i>
</span>
<i
class="anticon anticon-cross"></i>
<!---->
</div>
</div>
<div class="ant-upload ant-upload-select ant-upload-select-picture-card">
<!---->
</div>
</span>
<br>
<br> <span class="upload-list-inline"><div class="ant-upload ant-upload-select ant-upload-select-picture"><!----></div><div class="ant-upload-list ant-upload-list-picture"><div class="ant-upload-list-item ant-upload-list-item-done"><div class="ant-upload-list-item-info"><span><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" class="ant-upload-list-item-thumbnail"><img src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" alt="xxx.png"></a><a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" target="_blank" rel="noopener noreferrer" title="xxx.png" class="ant-upload-list-item-name">xxx.png</a></span></div>
Expand Down
37 changes: 23 additions & 14 deletions components/upload/demo/avatar.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ The return value of function `beforeUpload` can be a Promise to check asynchrono
</a-upload>
</template>
<script>
function getBase64(img, callback) {
const reader = new FileReader();
reader.addEventListener('load', () => callback(reader.result));
reader.readAsDataURL(img);
function getBase64 (img, callback) {
const reader = new FileReader()
reader.addEventListener('load', () => callback(reader.result))
reader.readAsDataURL(img)
}
export default {
data () {
Expand All @@ -42,38 +42,47 @@ export default {
}
},
methods: {
handleChange(info) {
handleChange (info) {
if (info.file.status === 'uploading') {
this.loading = true
return;
return
}
if (info.file.status === 'done') {
// Get this url from response in real world.
getBase64(info.file.originFileObj, (imageUrl) => {
this.imageUrl = imageUrl
this.loading = false
});
})
}
},
beforeUpload(file) {
const isJPG = file.type === 'image/jpeg';
beforeUpload (file) {
const isJPG = file.type === 'image/jpeg'
if (!isJPG) {
this.$message.error('You can only upload JPG file!');
this.$message.error('You can only upload JPG file!')
}
const isLt2M = file.size / 1024 / 1024 < 2;
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
this.$message.error('Image must smaller than 2MB!');
this.$message.error('Image must smaller than 2MB!')
}
return isJPG && isLt2M;
return isJPG && isLt2M
},
},
}
</script>
<style scoped>
<style>
.avatar-uploader > .ant-upload {
width: 128px;
height: 128px;
}
.ant-upload-select-picture-card i {
font-size: 32px;
color: #999;
}
.ant-upload-select-picture-card .ant-upload-text {
margin-top: 8px;
color: #666;
}
</style>
```

Expand Down
10 changes: 5 additions & 5 deletions components/upload/demo/picture-card.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ After users upload picture, the thumbnail will be shown in list. The upload butt
</div>
</a-upload>
<a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
<img alt="example" style="width: '100%'" :src="previewImage" />
<img alt="example" style="width: 100%" :src="previewImage" />
</a-modal>
</div>
</template>
Expand All @@ -43,20 +43,20 @@ export default {
}
},
methods: {
handleCancel() {
handleCancel () {
this.previewVisible = false
},
handlePreview(file) {
handlePreview (file) {
this.previewImage = file.url || file.thumbUrl
this.previewVisible = true
},
handleChange({ fileList }) {
handleChange ({ fileList }) {
this.fileList = fileList
},
},
}
</script>
<style scoped>
<style>
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
font-size: 32px;
Expand Down
8 changes: 4 additions & 4 deletions components/upload/demo/picture-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ If uploaded file is a picture, the thumbnail can be shown. `IE8/9` do not suppor
<div>
<a-upload
action="//jsonplaceholder.typicode.com/posts/"
listType="picture-card"
listType="picture"
:defaultFileList="fileList"
>
<a-button>
Expand Down Expand Up @@ -57,15 +57,15 @@ export default {
</script>
<style scoped>
/* tile uploaded pictures */
.upload-list-inline .ant-upload-list-item {
.upload-list-inline >>> .ant-upload-list-item {
float: left;
width: 200px;
margin-right: 8px;
}
.upload-list-inline .ant-upload-animate-enter {
.upload-list-inline >>> .ant-upload-animate-enter {
animation-name: uploadAnimateInlineIn;
}
.upload-list-inline .ant-upload-animate-leave {
.upload-list-inline >>> .ant-upload-animate-leave {
animation-name: uploadAnimateInlineOut;
}
</style>
Expand Down
2 changes: 1 addition & 1 deletion components/upload/interface.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const UploadFileStatus = PropsTypes.oneOf(['error', 'success', 'done', 'u
function UploadFile ({ uid, name }) {
if (!uid && uid !== 0) return false
if (!['string', 'number'].includes(typeof uid)) return false
if (!name && typeof name !== 'string') return false
if (name === '' || typeof name !== 'string') return false
return true
}

Expand Down

0 comments on commit 1584b38

Please sign in to comment.