ant-design-vue/components/vc-upload/src/Upload.jsx

99 lines
2.4 KiB
React
Raw Normal View History

2018-04-11 07:17:34 +00:00
import PropTypes from '../../_util/vue-types'
import { initDefaultProps } from '../../_util/props-util'
import BaseMixin from '../../_util/BaseMixin'
import AjaxUpload from './AjaxUploader'
import IframeUpload from './IframeUploader'
// function empty () {
// }
const uploadProps = {
2018-04-12 06:37:22 +00:00
componentTag: PropTypes.string,
2018-04-11 07:17:34 +00:00
prefixCls: PropTypes.string,
action: PropTypes.string,
name: PropTypes.string,
multipart: PropTypes.bool,
// onError: PropTypes.func,
// onSuccess: PropTypes.func,
// onProgress: PropTypes.func,
// onStart: PropTypes.func,
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func,
]),
headers: PropTypes.object,
accept: PropTypes.string,
multiple: PropTypes.bool,
disabled: PropTypes.bool,
beforeUpload: PropTypes.func,
customRequest: PropTypes.func,
// onReady: PropTypes.func,
withCredentials: PropTypes.bool,
supportServerRender: PropTypes.bool,
}
export default {
name: 'Upload',
mixins: [BaseMixin],
props: initDefaultProps(uploadProps, {
2018-04-12 06:37:22 +00:00
componentTag: 'span',
2018-04-11 07:17:34 +00:00
prefixCls: 'rc-upload',
data: {},
headers: {},
name: 'file',
multipart: false,
// onReady: empty,
// onStart: empty,
// onError: empty,
// onSuccess: empty,
supportServerRender: false,
multiple: false,
beforeUpload: null,
customRequest: null,
withCredentials: false,
}),
data () {
return {
Component: null,
}
},
mounted () {
this.$nextTick(() => {
if (this.supportServerRender) {
/* eslint react/no-did-mount-set-state:0 */
this.setState({
Component: this.getComponent(),
}, () => {
this.$emit('ready')
})
}
})
},
methods: {
getComponent () {
return typeof File !== 'undefined' ? AjaxUpload : IframeUpload
},
abort (file) {
this.$refs.uploaderRef.abort(file)
},
},
render () {
2018-04-12 06:37:22 +00:00
const componentProps = {
props: {
...this.$props,
},
on: this.$listeners,
ref: 'uploaderRef',
}
2018-04-11 07:17:34 +00:00
if (this.supportServerRender) {
const ComponentUploader = this.Component
if (ComponentUploader) {
2018-04-12 06:37:22 +00:00
return <ComponentUploader {...componentProps} >{this.$slots.default}</ComponentUploader>
2018-04-11 07:17:34 +00:00
}
return null
}
const ComponentUploader = this.getComponent()
2018-04-12 06:37:22 +00:00
return <ComponentUploader {...componentProps} >{this.$slots.default}</ComponentUploader>
2018-04-11 07:17:34 +00:00
},
}