Skip to content

Commit f10d0f1

Browse files
committed
feat: add menu template support
1 parent a16ca20 commit f10d0f1

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

antdv-demo

components/_util/store/connect.jsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function getDisplayName(WrappedComponent) {
88
}
99

1010
const defaultMapStateToProps = () => ({});
11-
export default function connect(mapStateToProps) {
11+
export default function connect(mapStateToProps, injectExtraPropsKey) {
1212
const shouldSubscribe = !!mapStateToProps;
1313
const finalMapStateToProps = mapStateToProps || defaultMapStateToProps;
1414
return function wrapWithConnect(WrappedComponent) {
@@ -24,18 +24,25 @@ export default function connect(mapStateToProps) {
2424
setup() {
2525
return {
2626
storeContext: inject('storeContext', {}),
27+
injectExtraProps: injectExtraPropsKey ? inject(injectExtraPropsKey, () => ({})) : {},
2728
};
2829
},
2930
data() {
3031
this.store = this.storeContext.store;
31-
this.preProps = getOptionProps(this);
32+
this.preProps = { ...getOptionProps(this), ...this.injectExtraProps };
3233
watchEffect(() => {
3334
if (mapStateToProps && mapStateToProps.length === 2) {
34-
this.subscribed = finalMapStateToProps(this.store.getState(), this.$props);
35+
this.subscribed = finalMapStateToProps(this.store.getState(), {
36+
...this.$props,
37+
...this.injectExtraProps,
38+
});
3539
}
3640
});
3741
return {
38-
subscribed: finalMapStateToProps(this.store.getState(), this.$props),
42+
subscribed: finalMapStateToProps(this.store.getState(), {
43+
...this.$props,
44+
...this.injectExtraProps,
45+
}),
3946
};
4047
},
4148
mounted() {
@@ -50,7 +57,7 @@ export default function connect(mapStateToProps) {
5057
if (!this.unsubscribe) {
5158
return;
5259
}
53-
const props = getOptionProps(this);
60+
const props = { ...getOptionProps(this), ...this.injectExtraProps };
5461
const nextSubscribed = finalMapStateToProps(this.store.getState(), props);
5562
if (
5663
!shallowEqual(this.preProps, props) ||
@@ -79,12 +86,12 @@ export default function connect(mapStateToProps) {
7986
},
8087
render() {
8188
const { $slots = {}, subscribed, store, $attrs } = this;
82-
const props = getOptionProps(this);
89+
const props = { ...getOptionProps(this), ...this.injectExtraProps };
8390
this.preProps = { ...props };
8491
const wrapProps = {
92+
...$attrs,
8593
...props,
8694
...subscribed,
87-
...$attrs,
8895
store,
8996
ref: 'wrappedInstance',
9097
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import PropTypes from '../_util/vue-types';
2+
import { provide, reactive } from 'vue';
3+
4+
const FunctionProvider = {
5+
inheritAttrs: false,
6+
props: {
7+
injectExtraPropsKey: PropTypes.string,
8+
},
9+
setup(props, { slots, attrs }) {
10+
if (props.injectExtraPropsKey) {
11+
provide(props.injectExtraPropsKey, reactive(attrs));
12+
}
13+
return () => slots.default && slots.default();
14+
},
15+
};
16+
17+
export default FunctionProvider;

components/vc-menu/MenuItem.jsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { connect } from '../_util/store';
66
import { noop, menuAllProps } from './util';
77
import { getComponent, getSlot, findDOMNode } from '../_util/props-util';
88
import { inject } from 'vue';
9-
9+
const injectExtraPropsKey = 'ANT_MENU_PROVIDER_PROPS_KEY';
1010
const props = {
1111
attribute: PropTypes.object,
1212
rootPrefixCls: PropTypes.string,
@@ -199,10 +199,13 @@ const MenuItem = {
199199
},
200200
};
201201

202-
const connected = connect(({ activeKey, selectedKeys }, { eventKey, subMenuKey }) => ({
203-
active: activeKey[subMenuKey] === eventKey,
204-
isSelected: selectedKeys.indexOf(eventKey) !== -1,
205-
}))(MenuItem);
202+
const connected = connect(
203+
({ activeKey, selectedKeys }, { eventKey, subMenuKey }) => ({
204+
active: activeKey[subMenuKey] === eventKey,
205+
isSelected: selectedKeys.indexOf(eventKey) !== -1,
206+
}),
207+
injectExtraPropsKey,
208+
)(MenuItem);
206209

207210
export default connected;
208211
export { props as menuItemProps };

components/vc-menu/SubMenu.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { noop, loopMenuItemRecursively, getMenuIdFromSubMenuEventKey } from './u
1313
import getTransitionProps from '../_util/getTransitionProps';
1414

1515
let guid = 0;
16-
16+
const injectExtraPropsKey = 'ANT_MENU_PROVIDER_PROPS_KEY';
1717
const popupPlacementMap = {
1818
horizontal: 'bottomLeft',
1919
vertical: 'rightTop',
@@ -337,6 +337,7 @@ const SubMenu = {
337337
},
338338
renderChildren(children) {
339339
const props = { ...this.$props, ...this.$attrs };
340+
340341
const subPopupMenuProps = {
341342
mode: props.mode === 'horizontal' ? 'vertical' : props.mode,
342343
visible: props.isOpen,
@@ -541,7 +542,7 @@ const connected = connect(({ openKeys, activeKey, selectedKeys }, { eventKey, su
541542
active: activeKey[subMenuKey] === eventKey,
542543
selectedKeys,
543544
};
544-
})(SubMenu);
545+
}, injectExtraPropsKey)(SubMenu);
545546

546547
connected.isSubMenu = true;
547548

components/vc-menu/SubPopupMenu.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import KeyCode from '../_util/KeyCode';
66
import classNames from '../_util/classNames';
77
import { getKeyFromChildrenIndex, loopMenuItem, noop, isMobileDevice, menuAllProps } from './util';
88
import DOMWrap from './DOMWrap';
9-
import { cloneElement } from '../_util/vnode';
109
import {
1110
initDefaultProps,
1211
getOptionProps,
1312
getComponent,
1413
splitAttrs,
1514
getPropsData,
1615
} from '../_util/props-util';
17-
16+
import FunctionProvider from './FunctionProvider';
17+
const injectExtraPropsKey = 'ANT_MENU_PROVIDER_PROPS_KEY';
1818
function allDisabled(arr) {
1919
if (!arr.length) {
2020
return true;
@@ -323,7 +323,11 @@ const SubPopupMenu = {
323323
if (props.mode === 'inline' || isMobileDevice()) {
324324
newChildProps.triggerSubMenuAction = 'click';
325325
}
326-
return cloneElement(child, newChildProps);
326+
return (
327+
<FunctionProvider injectExtraPropsKey={injectExtraPropsKey} {...newChildProps}>
328+
{child}
329+
</FunctionProvider>
330+
);
327331
},
328332

329333
renderMenuItem(c, i, subMenuKey) {
@@ -389,4 +393,4 @@ const SubPopupMenu = {
389393
},
390394
};
391395

392-
export default connect()(SubPopupMenu);
396+
export default connect(undefined)(SubPopupMenu);

0 commit comments

Comments
 (0)