|
| 1 | +import { Upload } from 'element-ui' |
| 2 | +import noop from 'lodash/noop' |
| 3 | +import ConnectedControlMixin from '../mixins/ConnectedControl' |
| 4 | + |
| 5 | +const defaultHandler = { |
| 6 | + type: Function, |
| 7 | + default: noop, |
| 8 | +} |
| 9 | + |
| 10 | +const httpRequest = ({ headers, file, filename, action }) => { |
| 11 | + const method = 'POST' |
| 12 | + |
| 13 | + const body = new FormData() |
| 14 | + |
| 15 | + body.append(filename, file) |
| 16 | + |
| 17 | + return fetch(action, { |
| 18 | + method, |
| 19 | + headers, |
| 20 | + body, |
| 21 | + }).then(response => response.json()) |
| 22 | +} |
| 23 | + |
| 24 | +const ConnectedInput = { |
| 25 | + props: { |
| 26 | + name: { |
| 27 | + type: String, |
| 28 | + required: true, |
| 29 | + }, |
| 30 | + |
| 31 | + endpoint: { |
| 32 | + type: String, |
| 33 | + required: true, |
| 34 | + }, |
| 35 | + |
| 36 | + headers: Object, |
| 37 | + multiple: Boolean, |
| 38 | + data: Object, |
| 39 | + |
| 40 | + // name on element-ui |
| 41 | + prop: { |
| 42 | + type: String, |
| 43 | + default: 'file', |
| 44 | + }, |
| 45 | + |
| 46 | + withCredentials: Boolean, |
| 47 | + showFileList: Boolean, |
| 48 | + drag: Boolean, |
| 49 | + accept: String, |
| 50 | + |
| 51 | + // hook function when clicking the uploaded files |
| 52 | + handlePreview: defaultHandler, |
| 53 | + |
| 54 | + // hook function when files are removed |
| 55 | + handleRemove: defaultHandler, |
| 56 | + |
| 57 | + // hook function when uploaded successfully |
| 58 | + handleSuccess: defaultHandler, |
| 59 | + |
| 60 | + // hook function when some errors occurs |
| 61 | + handleError: defaultHandler, |
| 62 | + |
| 63 | + // hook function when some progress occurs |
| 64 | + handleProgress: defaultHandler, |
| 65 | + |
| 66 | + // hook function when select file or upload file success or upload file fail |
| 67 | + handleChange: defaultHandler, |
| 68 | + |
| 69 | + // hook function before uploading with the file to be uploaded as its parameter. |
| 70 | + // If false is returned or a Promise is returned and then is rejected, uploading will be aborted |
| 71 | + beforeUpload: defaultHandler, |
| 72 | + |
| 73 | + // hook function before removing a file with the file and file list as its parameters. |
| 74 | + // If false is returned or a Promise is returned and then is rejected, removing will be aborted |
| 75 | + beforeRemove: defaultHandler, |
| 76 | + |
| 77 | + // hook function when limit is exceeded |
| 78 | + handleExceed: defaultHandler, |
| 79 | + |
| 80 | + formatResponse: { |
| 81 | + type: Function, |
| 82 | + default: response => response, |
| 83 | + }, |
| 84 | + |
| 85 | + fileList: { |
| 86 | + type: Array, |
| 87 | + default: () => [], |
| 88 | + }, |
| 89 | + listType: { |
| 90 | + type: String, |
| 91 | + default: 'text', |
| 92 | + }, |
| 93 | + autoUpload: { |
| 94 | + type: Boolean, |
| 95 | + default: true, |
| 96 | + }, |
| 97 | + httpRequest: { |
| 98 | + type: Function, |
| 99 | + default: httpRequest, |
| 100 | + }, |
| 101 | + disabled: Boolean, |
| 102 | + limit: Number, |
| 103 | + |
| 104 | + // Slots |
| 105 | + trigger: [Function, Object], |
| 106 | + tip: [Function, Object], |
| 107 | + |
| 108 | + // FormItem Props |
| 109 | + label: [String, Boolean], |
| 110 | + formItem: Boolean, |
| 111 | + labelWidth: String, |
| 112 | + |
| 113 | + // vue-form Props |
| 114 | + validators: Array, |
| 115 | + asyncValidators: Array, |
| 116 | + }, |
| 117 | + |
| 118 | + mixins: [ConnectedControlMixin], |
| 119 | + |
| 120 | + computed: { |
| 121 | + callbacks() { |
| 122 | + return { |
| 123 | + props: { |
| 124 | + onPreview: this.handlePreview, |
| 125 | + onRemove: this.handleRemove, |
| 126 | + onSuccess: this.handleFieldSuccess, |
| 127 | + onError: this.handleError, |
| 128 | + onProgress: this.handleProgress, |
| 129 | + onChange: this.handleFieldChange, |
| 130 | + onExceed: this.handleExceed, |
| 131 | + }, |
| 132 | + } |
| 133 | + }, |
| 134 | + }, |
| 135 | + |
| 136 | + methods: { |
| 137 | + clearFiles() { |
| 138 | + return this.$refs.uiUpload.clearFiles() |
| 139 | + }, |
| 140 | + |
| 141 | + abort(...args) { |
| 142 | + return this.$refs.uiUpload.abort(...args) |
| 143 | + }, |
| 144 | + |
| 145 | + submit() { |
| 146 | + return this.refs.$uiUpload.submit() |
| 147 | + }, |
| 148 | + |
| 149 | + handleFieldRemove() { |
| 150 | + this.handleRemove() |
| 151 | + |
| 152 | + const [, setValue] = this.state |
| 153 | + setValue([]) |
| 154 | + }, |
| 155 | + |
| 156 | + handleFieldSuccess(response, file, filelist) { |
| 157 | + this.handleSuccess(response, file, filelist) |
| 158 | + |
| 159 | + const [, setValue] = this.state |
| 160 | + setValue(this.formatResponse(response, file, filelist)) |
| 161 | + }, |
| 162 | + |
| 163 | + renderComponent() { |
| 164 | + return ( |
| 165 | + <Upload |
| 166 | + {...this.callbacks} |
| 167 | + ref="uiUpload" |
| 168 | + action={this.endpoint} |
| 169 | + headers={this.headers} |
| 170 | + multiple={this.multiple} |
| 171 | + data={this.data} |
| 172 | + name={this.prop} |
| 173 | + with-credentials={this.withCredentials} |
| 174 | + show-file-list={this.showFileList} |
| 175 | + drag={this.drag} |
| 176 | + accept={this.accept} |
| 177 | + before-upload={this.beforeUpload} |
| 178 | + before-remove={this.beforeRemove} |
| 179 | + file-list={this.fileList} |
| 180 | + list-type={this.listType} |
| 181 | + auto-upload={this.autoUpload} |
| 182 | + http-request={this.httpRequest} |
| 183 | + disabled={this.disabled} |
| 184 | + limit={this.limit}> |
| 185 | + {Boolean(this.trigger) && <template slot="trigger">{this.trigger}</template>} |
| 186 | + {Boolean(this.tip) && <template slot="tip">{this.tip}</template>} |
| 187 | + {this.$slots.default} |
| 188 | + </Upload> |
| 189 | + ) |
| 190 | + }, |
| 191 | + }, |
| 192 | +} |
| 193 | + |
| 194 | +export default ConnectedInput |
0 commit comments