Skip to content

Commit

Permalink
fix(Menu): 0-1 is not parent of 0-10, close alibaba-fusion#1701
Browse files Browse the repository at this point in the history
  • Loading branch information
youluna committed Mar 20, 2020
1 parent b1e4610 commit 9b2b74a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
27 changes: 10 additions & 17 deletions src/menu/view/popup-item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Overlay from '../../overlay';
import { func, obj, dom } from '../../util';
import Item from './item';
import SelectableItem from './selectable-item';
import { getChildSelected } from './util';

const { bindCtx } = func;
const { setStyle } = dom;
Expand Down Expand Up @@ -68,21 +69,6 @@ export default class PopupItem extends Component {
return openKeys.indexOf(_key) > -1;
}

getChildSelected() {
const { _key, root } = this.props;
const { selectMode } = root.props;
const { selectedKeys } = root.state;

const _keyPos = root.k2n[_key].pos;

return (
!!selectMode &&
selectedKeys.some(
key => root.k2n[key] && root.k2n[key].pos.indexOf(_keyPos) === 0
)
);
}

getPopupProps() {
let { popupProps } = this.props.root.props;
if (typeof popupProps === 'function') {
Expand Down Expand Up @@ -152,10 +138,17 @@ export default class PopupItem extends Component {

renderItem(selectable, children, others) {
const { _key, root, level, inlineLevel, label, className } = this.props;
const { prefix } = root.props;
const { prefix, selectMode } = root.props;
const NewItem = selectable ? SelectableItem : Item;
const open = this.getOpen();
const isChildSelected = this.getChildSelected();

const { selectedKeys } = root.state;
const isChildSelected = getChildSelected({
_key,
root,
selectMode,
selectedKeys,
});

const itemProps = {
'aria-haspopup': true,
Expand Down
25 changes: 9 additions & 16 deletions src/menu/view/sub-menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { func, obj } from '../../util';
import Item from './item';
import SelectabelItem from './selectable-item';
import PopupItem from './popup-item';
import { getChildSelected } from './util';

const { Expand } = Animate;
const { bindCtx } = func;
Expand Down Expand Up @@ -88,21 +89,6 @@ export default class SubMenu extends Component {
return openKeys.indexOf(_key) > -1;
}

getChildSelected() {
const { _key, root } = this.props;
const { selectMode } = root.props;
const { selectedKeys } = root.state;

const _keyPos = root.k2n[_key].pos;

return (
!!selectMode &&
selectedKeys.some(
key => root.k2n[key] && root.k2n[key].pos.indexOf(_keyPos) === 0
)
);
}

handleMouseEnter(e) {
this.handleOpen(true);

Expand Down Expand Up @@ -172,7 +158,14 @@ export default class SubMenu extends Component {
} = root.props;
const triggerType = propsTriggerType || rootTriggerType;
const open = this.getOpen();
const isChildSelected = this.getChildSelected();

const { selectedKeys } = root.state;
const isChildSelected = getChildSelected({
_key,
root,
selectMode,
selectedKeys,
});

const others = obj.pickOthers(
Object.keys(SubMenu.propTypes),
Expand Down
20 changes: 20 additions & 0 deletions src/menu/view/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,23 @@ export const getWidth = elem => {
}
return width || 0;
};
/**
* 如果 key 在 SelectedKeys 的选中链上(例如 SelectedKeys 是['0-1-2'], key是 0-1 ),那么返回true
*
* selectMode?: string; 当前的选择模式,一般为 multiple single
* selectedKeys?: string[]; 选中的key值
* root?: any;
* _key?: string; 待测试的key值
*
* @return bool 当前元素是否有孩子被选中
*/
export const getChildSelected = ({ selectMode, selectedKeys, root, _key }) => {
const _keyPos = `${root.k2n[_key].pos}-`;

return (
!!selectMode &&
selectedKeys.some(
key => root.k2n[key] && root.k2n[key].pos.indexOf(_keyPos) === 0
)
);
};

0 comments on commit 9b2b74a

Please sign in to comment.