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

43 lines
1.3 KiB
JavaScript

import { createVNode, defineComponent, inject, provide } from 'vue';
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 的依赖
return {
props,
injectExtraProps: injectExtraPropsKey ? inject(injectExtraPropsKey, () => ({})) : {},
};
},
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);
}