2020-12-13 00:28:03 +00:00
|
|
|
|
import { createVNode, defineComponent, inject, provide, watch } from 'vue';
|
2020-11-23 06:21:18 +00:00
|
|
|
|
import { injectExtraPropsKey } from './FunctionProvider';
|
|
|
|
|
|
|
|
|
|
export default function wrapWithConnect(WrappedComponent) {
|
|
|
|
|
const tempProps = WrappedComponent.props || {};
|
|
|
|
|
const props = {};
|
|
|
|
|
Object.keys(tempProps).forEach(k => {
|
|
|
|
|
props[k] = { ...tempProps[k], required: false };
|
|
|
|
|
});
|
|
|
|
|
const Connect = {
|
|
|
|
|
name: `Connect_${WrappedComponent.name}`,
|
|
|
|
|
inheritAttrs: false,
|
|
|
|
|
props,
|
|
|
|
|
setup(props) {
|
|
|
|
|
provide(injectExtraPropsKey, undefined); // 断掉 injectExtraPropsKey 的依赖
|
2020-12-13 00:28:03 +00:00
|
|
|
|
const injectExtraProps = injectExtraPropsKey ? inject(injectExtraPropsKey, () => ({})) : {};
|
|
|
|
|
watch(injectExtraProps, ()=>{
|
|
|
|
|
// 神奇的问题,vue 3.0.3 之后,没能正确响应式,暂时加个 watch hack 一下
|
|
|
|
|
});
|
2020-11-23 06:21:18 +00:00
|
|
|
|
return {
|
|
|
|
|
props,
|
2020-12-13 00:28:03 +00:00
|
|
|
|
injectExtraProps,
|
2020-11-23 06:21:18 +00:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
getWrappedInstance() {
|
|
|
|
|
return this.$refs.wrappedInstance;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
render() {
|
|
|
|
|
const { $slots = {}, $attrs } = this;
|
|
|
|
|
const props = { ...this.props, ...this.injectExtraProps };
|
|
|
|
|
const wrapProps = {
|
|
|
|
|
...$attrs,
|
|
|
|
|
...props,
|
|
|
|
|
ref: 'wrappedInstance',
|
|
|
|
|
};
|
|
|
|
|
// const slots = {};
|
|
|
|
|
// for (let [key, value] of Object.entries($slots)) {
|
|
|
|
|
// slots[key] = () => value();
|
|
|
|
|
// }
|
|
|
|
|
return createVNode(WrappedComponent, wrapProps, $slots);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
return defineComponent(Connect);
|
|
|
|
|
}
|