[new feature] Uploader: add max-count prop

This commit is contained in:
陈嘉涵
2019-06-04 15:53:19 +08:00
parent 03af04e81e
commit 47bb6fc7fa
8 changed files with 98 additions and 11 deletions

View File

@@ -24,6 +24,10 @@ export default sfc({
maxSize: {
type: Number,
default: Number.MAX_VALUE
},
maxCount: {
type: Number,
default: Number.MAX_VALUE
}
},
@@ -44,17 +48,22 @@ export default sfc({
methods: {
onChange(event) {
let { files } = event.target;
if (this.disabled || !files.length) {
return;
}
files = files.length === 1 ? files[0] : [].slice.call(files, 0);
if (!files || (this.beforeRead && !this.beforeRead(files, this.detail))) {
this.resetInput();
return;
}
if (Array.isArray(files)) {
const maxCount = this.maxCount - this.previewImages.length;
files = files.slice(0, maxCount);
Promise.all(files.map(this.readFile)).then(contents => {
let oversize = false;
const payload = files.map((file, index) => {
@@ -96,17 +105,19 @@ export default sfc({
onAfterRead(files, oversize) {
if (oversize) {
this.$emit('oversize', files, this.detail);
} else {
if (Array.isArray(files)) {
files.forEach(this.addPreviewImage);
} else {
this.addPreviewImage(files);
}
if (this.afterRead) {
this.afterRead(files, this.detail);
}
return;
}
if (Array.isArray(files)) {
files.forEach(this.addPreviewImage);
} else {
this.addPreviewImage(files);
}
if (this.afterRead) {
this.afterRead(files, this.detail);
}
this.resetInput();
},
@@ -130,6 +141,10 @@ export default sfc({
},
renderUpload() {
if (this.previewImages.length >= this.maxCount) {
return;
}
const slot = this.slots();
const Input = (