feat: migrate Uploader component

This commit is contained in:
chenjiahan 2020-08-16 15:40:56 +08:00
parent 0ed0337de0
commit 7c2b4c9abc
4 changed files with 37 additions and 34 deletions

View File

@ -56,4 +56,5 @@ module.exports = [
'password-input', 'password-input',
'search', 'search',
'stepper', 'stepper',
'uploader',
]; ];

View File

@ -51,6 +51,7 @@ Vant 遵循 [Semver](https://semver.org/lang/zh-CN/) 语义化版本规范。
- Stepper - Stepper
- Switch - Switch
- Sidebar - Sidebar
- Uploader
```html ```html
<!-- before --> <!-- before -->

View File

@ -18,10 +18,6 @@ export default createComponent({
mixins: [FieldMixin], mixins: [FieldMixin],
model: {
prop: 'fileList',
},
props: { props: {
disabled: Boolean, disabled: Boolean,
lazyLoad: Boolean, lazyLoad: Boolean,
@ -39,7 +35,7 @@ export default createComponent({
type: String, type: String,
default: 'image/*', default: 'image/*',
}, },
fileList: { modelValue: {
type: Array, type: Array,
default: () => [], default: () => [],
}, },
@ -81,19 +77,22 @@ export default createComponent({
}, },
}, },
emits: [
'delete',
'oversize',
'close-preview',
'click-preview',
'update:modelValue',
],
computed: { computed: {
previewSizeWithUnit() { previewSizeWithUnit() {
return addUnit(this.previewSize); return addUnit(this.previewSize);
}, },
// for form
value() {
return this.fileList;
},
}, },
methods: { methods: {
getDetail(index = this.fileList.length) { getDetail(index = this.modelValue.length) {
return { return {
name: this.name, name: this.name,
index, index,
@ -139,7 +138,7 @@ export default createComponent({
const oversize = isOversize(files, this.maxSize); const oversize = isOversize(files, this.maxSize);
if (Array.isArray(files)) { if (Array.isArray(files)) {
const maxCount = this.maxCount - this.fileList.length; const maxCount = this.maxCount - this.modelValue.length;
if (files.length > maxCount) { if (files.length > maxCount) {
files = files.slice(0, maxCount); files = files.slice(0, maxCount);
@ -203,7 +202,10 @@ export default createComponent({
: Boolean(validFiles); : Boolean(validFiles);
if (isValidFiles) { if (isValidFiles) {
this.$emit('input', [...this.fileList, ...toArray(validFiles)]); this.$emit('update:modelValue', [
...this.modelValue,
...toArray(validFiles),
]);
if (this.afterRead) { if (this.afterRead) {
this.afterRead(validFiles, this.getDetail()); this.afterRead(validFiles, this.getDetail());
@ -233,10 +235,10 @@ export default createComponent({
}, },
deleteFile(file, index) { deleteFile(file, index) {
const fileList = this.fileList.slice(0); const fileList = this.modelValue.slice(0);
fileList.splice(index, 1); fileList.splice(index, 1);
this.$emit('input', fileList); this.$emit('update:modelValue', fileList);
this.$emit('delete', file, this.getDetail(index)); this.$emit('delete', file, this.getDetail(index));
}, },
@ -252,7 +254,7 @@ export default createComponent({
return; return;
} }
const imageFiles = this.fileList.filter((item) => isImageFile(item)); const imageFiles = this.modelValue.filter((item) => isImageFile(item));
const imageContents = imageFiles.map((item) => item.content || item.url); const imageContents = imageFiles.map((item) => item.content || item.url);
this.imagePreview = ImagePreview({ this.imagePreview = ImagePreview({
@ -320,9 +322,10 @@ export default createComponent({
</div> </div>
); );
const PreviewCoverContent = this.slots('preview-cover', item); const PreviewCover = this.$slots['preview-cover'] && (
const PreviewCover = PreviewCoverContent && ( <div class={bem('preview-cover')}>
<div class={bem('preview-cover')}>{PreviewCoverContent}</div> {this.$slots['preview-cover'](item)}
</div>
); );
const Preview = isImageFile(item) ? ( const Preview = isImageFile(item) ? (
@ -371,20 +374,18 @@ export default createComponent({
genPreviewList() { genPreviewList() {
if (this.previewImage) { if (this.previewImage) {
return this.fileList.map(this.genPreviewItem); return this.modelValue.map(this.genPreviewItem);
} }
}, },
genUpload() { genUpload() {
if (this.fileList.length >= this.maxCount || !this.showUpload) { if (this.modelValue.length >= this.maxCount || !this.showUpload) {
return; return;
} }
const slot = this.slots();
const Input = ( const Input = (
<input <input
{...{ attrs: this.$attrs }} {...this.$attrs}
ref="input" ref="input"
type="file" type="file"
accept={this.accept} accept={this.accept}
@ -394,10 +395,10 @@ export default createComponent({
/> />
); );
if (slot) { if (this.$slots.default) {
return ( return (
<div class={bem('input-wrapper')}> <div class={bem('input-wrapper')}>
{slot} {this.$slots.default()}
{Input} {Input}
</div> </div>
); );

View File

@ -171,10 +171,10 @@ module.exports = {
path: 'switch', path: 'switch',
title: 'Switch 开关', title: 'Switch 开关',
}, },
// { {
// path: 'uploader', path: 'uploader',
// title: 'Uploader 文件上传', title: 'Uploader 文件上传',
// }, },
], ],
}, },
{ {
@ -505,10 +505,10 @@ module.exports = {
path: 'switch', path: 'switch',
title: 'Switch', title: 'Switch',
}, },
// { {
// path: 'uploader', path: 'uploader',
// title: 'Uploader', title: 'Uploader',
// }, },
], ],
}, },
{ {