ant-design-vue/components/vc-menu/InjectExtraProps.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

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 的依赖
const injectExtraProps = injectExtraPropsKey ? inject(injectExtraPropsKey, () => ({})) : {};
2021-01-24 05:22:45 +00:00
watch(injectExtraProps, () => {
// 神奇的问题vue 3.0.3 之后,没能正确响应式,暂时加个 watch hack 一下
});
2020-11-23 06:21:18 +00:00
return {
props,
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);
}