Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"classnames": "2.x",
"create-react-class": "^15.5.2",
"dom-scroll-into-view": "1.x",
"mini-store": "^1.0.2",
"prop-types": "^15.5.6",
"rc-animate": "2.x",
"rc-trigger": "^2.3.0",
Expand Down
42 changes: 26 additions & 16 deletions src/Menu.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// import React from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
import MenuMixin from './MenuMixin';
import { Provider, create } from 'mini-store';
import { default as MenuMixin, getActiveKey } from './MenuMixin';
import { noop } from './util';

const Menu = createReactClass({
Expand Down Expand Up @@ -59,20 +60,24 @@ const Menu = createReactClass({
if ('openKeys' in props) {
openKeys = props.openKeys || [];
}
return {

this.store = create({
selectedKeys,
openKeys,
};
activeKey: { '0-menu-': getActiveKey(props, props.activeKey) },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

activeKey 是不是只要数组就可以了?因为 key 是全局唯一的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key是全局唯一的,但是activeKey不是,每层都有一个,这样get操作也比较方便。

Copy link
Member

@yesmeck yesmeck Mar 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

activeKey 应该是跟 openKeys 类似的。里面包含的 key 是唯一的。

});

return {};
},

componentWillReceiveProps(nextProps) {
if ('selectedKeys' in nextProps) {
this.setState({
this.store.setState({
selectedKeys: nextProps.selectedKeys || [],
});
}
if ('openKeys' in nextProps) {
this.setState({
this.store.setState({
openKeys: nextProps.openKeys || [],
});
}
Expand All @@ -82,15 +87,15 @@ const Menu = createReactClass({
const props = this.props;
if (props.selectable) {
// root menu
let selectedKeys = this.state.selectedKeys;
let selectedKeys = this.store.getState().selectedKeys;
const selectedKey = selectInfo.key;
if (props.multiple) {
selectedKeys = selectedKeys.concat([selectedKey]);
} else {
selectedKeys = [selectedKey];
}
if (!('selectedKeys' in props)) {
this.setState({
this.store.setState({
selectedKeys,
});
}
Expand All @@ -107,7 +112,7 @@ const Menu = createReactClass({

onOpenChange(event) {
const props = this.props;
const openKeys = this.state.openKeys.concat();
const openKeys = this.store.getState().openKeys.concat();
let changed = false;
const processSingle = (e) => {
let oneChanged = false;
Expand All @@ -133,7 +138,7 @@ const Menu = createReactClass({
}
if (changed) {
if (!('openKeys' in this.props)) {
this.setState({ openKeys });
this.store.setState({ openKeys });
}
props.onOpenChange(openKeys);
}
Expand All @@ -142,14 +147,14 @@ const Menu = createReactClass({
onDeselect(selectInfo) {
const props = this.props;
if (props.selectable) {
const selectedKeys = this.state.selectedKeys.concat();
const selectedKeys = this.store.getState().selectedKeys.concat();
const selectedKey = selectInfo.key;
const index = selectedKeys.indexOf(selectedKey);
if (index !== -1) {
selectedKeys.splice(index, 1);
}
if (!('selectedKeys' in props)) {
this.setState({
this.store.setState({
selectedKeys,
});
}
Expand All @@ -176,7 +181,7 @@ const Menu = createReactClass({

lastOpenSubMenu() {
let lastOpen = [];
const { openKeys } = this.state;
const { openKeys } = this.store.getState();
if (openKeys.length) {
lastOpen = this.getFlatInstanceArray().filter((c) => {
return c && openKeys.indexOf(c.props.eventKey) !== -1;
Expand All @@ -185,23 +190,28 @@ const Menu = createReactClass({
return lastOpen[0];
},

renderMenuItem(c, i, subIndex) {
renderMenuItem(c, i, subIndex, subMenuKey) {
if (!c) {
return null;
}
const state = this.state;
const state = this.store.getState();
const extraProps = {
openKeys: state.openKeys,
selectedKeys: state.selectedKeys,
triggerSubMenuAction: this.props.triggerSubMenuAction,
subMenuKey,
};
return this.renderCommonMenuItem(c, i, subIndex, extraProps);
},

render() {
const props = { ...this.props };
props.className += ` ${props.prefixCls}-root`;
return this.renderRoot(props);
return (
<Provider store={this.store}>
{this.renderRoot(props)}
</Provider>
);
},
});

Expand Down
29 changes: 17 additions & 12 deletions src/MenuItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
import KeyCode from 'rc-util/lib/KeyCode';
import classNames from 'classnames';
import { connect } from 'mini-store';
import { noop } from './util';

/* eslint react/no-is-mounted:0 */
Expand Down Expand Up @@ -43,6 +44,13 @@ const MenuItem = createReactClass({
}
},

componentDidMount() {
// invoke customized ref to expose component to mixin
if (this.props.manualRef) {
this.props.manualRef(this);
}
},

onKeyDown(e) {
const keyCode = e.keyCode;
if (keyCode === KeyCode.ENTER) {
Expand Down Expand Up @@ -76,8 +84,7 @@ const MenuItem = createReactClass({
},

onClick(e) {
const { eventKey, multiple, onClick, onSelect, onDeselect } = this.props;
const selected = this.isSelected();
const { eventKey, multiple, onClick, onSelect, onDeselect, isSelected } = this.props;
const info = {
key: eventKey,
keyPath: [eventKey],
Expand All @@ -86,12 +93,12 @@ const MenuItem = createReactClass({
};
onClick(info);
if (multiple) {
if (selected) {
if (isSelected) {
onDeselect(info);
} else {
onSelect(info);
}
} else if (!selected) {
} else if (!isSelected) {
onSelect(info);
}
},
Expand All @@ -112,24 +119,19 @@ const MenuItem = createReactClass({
return `${this.getPrefixCls()}-disabled`;
},

isSelected() {
return this.props.selectedKeys.indexOf(this.props.eventKey) !== -1;
},

render() {
const props = this.props;
const selected = this.isSelected();
const className = classNames(this.getPrefixCls(), props.className, {
[this.getActiveClassName()]: !props.disabled && props.active,
[this.getSelectedClassName()]: selected,
[this.getSelectedClassName()]: props.isSelected,
[this.getDisabledClassName()]: props.disabled,
});
const attrs = {
...props.attribute,
title: props.title,
className,
role: 'menuitem',
'aria-selected': selected,
'aria-selected': props.isSelected,
'aria-disabled': props.disabled,
};
let mouseEvent = {};
Expand Down Expand Up @@ -160,4 +162,7 @@ const MenuItem = createReactClass({

MenuItem.isMenuItem = 1;

export default MenuItem;
export default connect(({ activeKey, selectedKeys }, { eventKey, subMenuKey }) => ({
active: activeKey[subMenuKey] === eventKey,
isSelected: selectedKeys.indexOf(eventKey) !== -1,
}))(MenuItem);
2 changes: 1 addition & 1 deletion src/MenuItemGroup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const MenuItemGroup = createReactClass({

renderInnerMenuItem(item, subIndex) {
const { renderMenuItem, index } = this.props;
return renderMenuItem(item, index, subIndex);
return renderMenuItem(item, index, subIndex, this.props.subMenuKey);
},

render() {
Expand Down
Loading