2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
import PropTypes from './vue-types'
|
|
|
|
import { getOptionProps } from './props-util'
|
|
|
|
|
|
|
|
function getDisplayName (WrappedComponent) {
|
|
|
|
return WrappedComponent.name || 'Component'
|
|
|
|
}
|
|
|
|
export default function wrapWithConnect (WrappedComponent) {
|
|
|
|
const tempProps = WrappedComponent.props || {}
|
|
|
|
const methods = WrappedComponent.methods || {}
|
|
|
|
const props = {}
|
2018-09-29 07:07:37 +00:00
|
|
|
Object.keys(tempProps).forEach(k => { props[k] = ({ ...k, required: false }) })
|
2018-09-05 13:28:54 +00:00
|
|
|
WrappedComponent.props.__propsSymbol__ = PropTypes.any
|
|
|
|
WrappedComponent.props.children = PropTypes.array.def([])
|
|
|
|
const ProxyWrappedComponent = {
|
|
|
|
props,
|
|
|
|
model: WrappedComponent.model,
|
|
|
|
name: `Proxy_${getDisplayName(WrappedComponent)}`,
|
|
|
|
methods: {
|
|
|
|
getProxyWrappedInstance () {
|
|
|
|
return this.$refs.wrappedInstance
|
|
|
|
},
|
|
|
|
},
|
|
|
|
render () {
|
2018-09-29 07:07:37 +00:00
|
|
|
const { $listeners, $slots = {}, $attrs, $scopedSlots } = this
|
2018-09-05 13:28:54 +00:00
|
|
|
const props = getOptionProps(this)
|
|
|
|
const wrapProps = {
|
|
|
|
props: {
|
|
|
|
...props,
|
|
|
|
__propsSymbol__: Symbol(),
|
2018-11-26 13:49:05 +00:00
|
|
|
componentWillReceiveProps: { ...props },
|
2018-09-29 07:07:37 +00:00
|
|
|
children: $slots.default || props.children || [],
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
|
|
|
on: $listeners,
|
|
|
|
attrs: $attrs,
|
|
|
|
}
|
2018-11-26 13:49:05 +00:00
|
|
|
if (Object.keys($scopedSlots).length) {
|
|
|
|
wrapProps.scopedSlots = $scopedSlots
|
|
|
|
}
|
|
|
|
const slotsKey = Object.keys($slots)
|
2018-09-05 13:28:54 +00:00
|
|
|
return (
|
2018-09-29 07:07:37 +00:00
|
|
|
<WrappedComponent {...wrapProps} ref='wrappedInstance'>
|
2018-11-26 13:49:05 +00:00
|
|
|
{slotsKey.length ? slotsKey.map(name => {
|
2018-09-29 07:07:37 +00:00
|
|
|
return <template slot={name}>{$slots[name]}</template>
|
2018-11-26 13:49:05 +00:00
|
|
|
}) : null}
|
2018-09-29 07:07:37 +00:00
|
|
|
</WrappedComponent>
|
2018-09-05 13:28:54 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Object.keys(methods).map(m => {
|
|
|
|
ProxyWrappedComponent.methods[m] = function () {
|
2018-10-10 06:09:13 +00:00
|
|
|
return this.getProxyWrappedInstance()[m](...arguments)
|
2018-09-05 13:28:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return ProxyWrappedComponent
|
|
|
|
}
|