From e1d27b9db37d0f942404789a592f844789c37c08 Mon Sep 17 00:00:00 2001 From: guifu Date: Thu, 19 Apr 2018 16:20:34 +0800 Subject: [PATCH] chore: fix lint --- examples/selectedKeys.js | 3 +- package.json | 1 - src/Menu.jsx | 8 +-- src/MenuItem.jsx | 18 +++-- src/MenuItemGroup.jsx | 1 + src/SubMenu.jsx | 47 +++++++------ src/SubPopupMenu.js | 138 +++++++++++++++++++++------------------ 7 files changed, 117 insertions(+), 99 deletions(-) diff --git a/examples/selectedKeys.js b/examples/selectedKeys.js index e5a257eb..ae1c1b4c 100644 --- a/examples/selectedKeys.js +++ b/examples/selectedKeys.js @@ -139,7 +139,6 @@ class Test extends React.Component {
{this.getMenu()}
); } -}; - +} ReactDOM.render(, document.getElementById('__react-content')); diff --git a/package.json b/package.json index 16ae8de3..cc4eaf2c 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,6 @@ "dependencies": { "babel-runtime": "6.x", "classnames": "2.x", - "create-react-class": "^15.5.2", "dom-scroll-into-view": "1.x", "mini-store": "^1.1.0", "prop-types": "^15.5.6", diff --git a/src/Menu.jsx b/src/Menu.jsx index 3cadcc10..eb15dc03 100644 --- a/src/Menu.jsx +++ b/src/Menu.jsx @@ -184,7 +184,7 @@ export default class Menu extends React.Component { } render() { - let { children, ...props } = this.props; + let { ...props } = this.props; props.className += ` ${props.prefixCls}-root`; props = { ...props, @@ -194,11 +194,11 @@ export default class Menu extends React.Component { onSelect: this.onSelect, openTransitionName: this.getOpenTransitionName(), parentMenu: this, - } + }; return ( - this.innerMenu = c}>{children} + this.innerMenu = c}>{this.props.children} ); } -}; +} diff --git a/src/MenuItem.jsx b/src/MenuItem.jsx index c3a9a3a4..3920b7b5 100644 --- a/src/MenuItem.jsx +++ b/src/MenuItem.jsx @@ -26,23 +26,20 @@ export class MenuItem extends React.Component { onDestroy: PropTypes.func, onMouseEnter: PropTypes.func, onMouseLeave: PropTypes.func, + multiple: PropTypes.bool, + isSelected: PropTypes.bool, + manualRef: PropTypes.func, }; static defaultProps = { onSelect: noop, onMouseEnter: noop, onMouseLeave: noop, + manualRef: noop, }; constructor(props) { super(props); - } - - componentWillUnmount() { - const props = this.props; - if (props.onDestroy) { - props.onDestroy(props.eventKey); - } } componentDidMount() { @@ -60,6 +57,13 @@ export class MenuItem extends React.Component { this.callRef(); } + componentWillUnmount() { + const props = this.props; + if (props.onDestroy) { + props.onDestroy(props.eventKey); + } + } + onKeyDown = (e) => { const keyCode = e.keyCode; if (keyCode === KeyCode.ENTER) { diff --git a/src/MenuItemGroup.jsx b/src/MenuItemGroup.jsx index 479468b8..1a1e4184 100644 --- a/src/MenuItemGroup.jsx +++ b/src/MenuItemGroup.jsx @@ -6,6 +6,7 @@ class MenuItemGroup extends React.Component { renderMenuItem: PropTypes.func, index: PropTypes.number, className: PropTypes.string, + subMenuKey: PropTypes.string, rootPrefixCls: PropTypes.string, }; diff --git a/src/SubMenu.jsx b/src/SubMenu.jsx index 3d398c00..9c9c6bae 100644 --- a/src/SubMenu.jsx +++ b/src/SubMenu.jsx @@ -58,6 +58,9 @@ export class SubMenu extends React.Component { onTitleMouseLeave: PropTypes.func, onTitleClick: PropTypes.func, isOpen: PropTypes.bool, + store: PropTypes.object, + mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']), + manualRef: PropTypes.func, }; static defaultProps = { @@ -66,6 +69,8 @@ export class SubMenu extends React.Component { onTitleMouseEnter: noop, onTitleMouseLeave: noop, onTitleClick: noop, + manualRef: noop, + mode: 'vertical', title: '', }; @@ -90,20 +95,6 @@ export class SubMenu extends React.Component { this.componentDidUpdate(); } - adjustWidth = () => { - /* istanbul ignore if */ - if (!this.subMenuTitle || !this.menuInstance) { - return; - } - const popupMenu = ReactDOM.findDOMNode(this.menuInstance); - if (popupMenu.offsetWidth >= this.subMenuTitle.offsetWidth) { - return; - } - - /* istanbul ignore next */ - popupMenu.style.minWidth = `${this.subMenuTitle.offsetWidth}px`; - }; - componentDidUpdate() { const { mode, parentMenu, manualRef } = this.props; @@ -134,9 +125,9 @@ export class SubMenu extends React.Component { if (this.mouseenterTimeout) { clearTimeout(this.mouseenterTimeout); } - }; + } - onDestroy(key) { + onDestroy = (key) => { this.props.onDestroy(key); }; @@ -325,6 +316,24 @@ export class SubMenu extends React.Component { return this.props.openKeys.indexOf(this.props.eventKey) !== -1; } + adjustWidth = () => { + /* istanbul ignore if */ + if (!this.subMenuTitle || !this.menuInstance) { + return; + } + const popupMenu = ReactDOM.findDOMNode(this.menuInstance); + if (popupMenu.offsetWidth >= this.subMenuTitle.offsetWidth) { + return; + } + + /* istanbul ignore next */ + popupMenu.style.minWidth = `${this.subMenuTitle.offsetWidth}px`; + }; + + saveSubMenuTitle = (subMenuTitle) => { + this.subMenuTitle = subMenuTitle; + } + renderChildren(children) { const props = this.props; const baseProps = { @@ -394,10 +403,6 @@ export class SubMenu extends React.Component { ); } - saveSubMenuTitle = (subMenuTitle) => { - this.subMenuTitle = subMenuTitle; - } - render() { const props = this.props; const isOpen = props.isOpen; @@ -490,7 +495,7 @@ export class SubMenu extends React.Component { ); } -}; +} const connected = connect(({ openKeys, activeKey, selectedKeys }, { eventKey, subMenuKey }) => ({ isOpen: openKeys.indexOf(eventKey) > -1, diff --git a/src/SubPopupMenu.js b/src/SubPopupMenu.js index d2f43c17..dfdb783a 100644 --- a/src/SubPopupMenu.js +++ b/src/SubPopupMenu.js @@ -1,11 +1,10 @@ import React from 'react'; import PropTypes from 'prop-types'; -import Animate from 'rc-animate'; import { connect } from 'mini-store'; import KeyCode from 'rc-util/lib/KeyCode'; import createChainedFunction from 'rc-util/lib/createChainedFunction'; import classNames from 'classnames'; -import { getKeyFromChildrenIndex, loopMenuItem } from './util'; +import { getKeyFromChildrenIndex, loopMenuItem, noop } from './util'; import DOMWrap from './DOMWrap'; function allDisabled(arr) { @@ -70,6 +69,11 @@ export class SubPopupMenu extends React.Component { visible: PropTypes.bool, children: PropTypes.any, parentMenu: PropTypes.object, + eventKey: PropTypes.string, + store: PropTypes.shape({ + getState: PropTypes.func, + setState: PropTypes.func, + }), // adding in refactor focusable: PropTypes.bool, @@ -80,10 +84,11 @@ export class SubPopupMenu extends React.Component { selectedKeys: PropTypes.arrayOf(PropTypes.string), defaultSelectedKeys: PropTypes.arrayOf(PropTypes.string), defaultOpenKeys: PropTypes.arrayOf(PropTypes.string), - openKeys: PropTypes.arrayOf(PropTypes.string), level: PropTypes.number, mode: PropTypes.oneOf(['horizontal', 'vertical', 'vertical-left', 'vertical-right', 'inline']), + triggerSubMenuAction: PropTypes.oneOf(['click', 'hover']), inlineIndent: PropTypes.number, + manualRef: PropTypes.func, }; static defaultProps = { @@ -95,6 +100,7 @@ export class SubPopupMenu extends React.Component { visible: true, focusable: true, style: {}, + manualRef: noop, }; constructor(props) { @@ -108,13 +114,8 @@ export class SubPopupMenu extends React.Component { }); } - componentWillReceiveProps(nextProps) { - const originalActiveKey = 'activeKey' in nextProps ? nextProps.activeKey : - this.getStore().getState().activeKey[this.getEventKey()]; - const activeKey = getActiveKey(nextProps, originalActiveKey); - if (activeKey !== originalActiveKey) { - updateActiveKey(this.getStore(), this.getEventKey(), activeKey); - } + componentWillMount() { + this.instanceArray = []; } componentDidMount() { @@ -124,12 +125,17 @@ export class SubPopupMenu extends React.Component { } } - shouldComponentUpdate(nextProps) { - return this.props.visible || nextProps.visible; + componentWillReceiveProps(nextProps) { + const originalActiveKey = 'activeKey' in nextProps ? nextProps.activeKey : + this.getStore().getState().activeKey[this.getEventKey()]; + const activeKey = getActiveKey(nextProps, originalActiveKey); + if (activeKey !== originalActiveKey) { + updateActiveKey(this.getStore(), this.getEventKey(), activeKey); + } } - componentWillMount() { - this.instanceArray = []; + shouldComponentUpdate(nextProps) { + return this.props.visible || nextProps.visible; } // all keyboard events callbacks run from here at first @@ -165,19 +171,6 @@ export class SubPopupMenu extends React.Component { updateActiveKey(this.getStore(), this.getEventKey(), hover ? key : null); }; - getEventKey = () => { - // when eventKey not available ,it's menu and return menu id '0-menu-' - return this.props.eventKey || '0-menu-'; - }; - - getStore = () => { - return this.props.store; - }; - - getFlatInstanceArray = () => { - return this.instanceArray; - }; - onDeselect = (selectInfo) => { this.props.onDeselect(selectInfo); }; @@ -199,10 +192,64 @@ export class SubPopupMenu extends React.Component { this.props.onDestroy(key); }; + getFlatInstanceArray = () => { + return this.instanceArray; + }; + + getStore = () => { + return this.props.store; + }; + + getEventKey = () => { + // when eventKey not available ,it's menu and return menu id '0-menu-' + return this.props.eventKey || '0-menu-'; + }; + getOpenTransitionName = () => { return this.props.openTransitionName; }; + step = (direction) => { + let children = this.getFlatInstanceArray(); + const activeKey = this.getStore().getState().activeKey[this.getEventKey()]; + const len = children.length; + if (!len) { + return null; + } + if (direction < 0) { + children = children.concat().reverse(); + } + // find current activeIndex + let activeIndex = -1; + children.every((c, ci) => { + if (c && c.props.eventKey === activeKey) { + activeIndex = ci; + return false; + } + return true; + }); + if ( + !this.props.defaultActiveFirst && activeIndex !== -1 + && + allDisabled(children.slice(activeIndex, len - 1)) + ) { + return undefined; + } + const start = (activeIndex + 1) % len; + let i = start; + + do { + const child = children[i]; + if (!child || child.props.disabled) { + i = (i + 1) % len; + } else { + return child; + } + } while (i !== start); + + return null; + }; + renderCommonMenuItem = (child, i, extraProps) => { const state = this.getStore().getState(); const props = this.props; @@ -294,43 +341,6 @@ export class SubPopupMenu extends React.Component { /*eslint-enable */ ); } - - step(direction) { - let children = this.getFlatInstanceArray(); - const activeKey = this.getStore().getState().activeKey[this.getEventKey()]; - const len = children.length; - if (!len) { - return null; - } - if (direction < 0) { - children = children.concat().reverse(); - } - // find current activeIndex - let activeIndex = -1; - children.every((c, ci) => { - if (c && c.props.eventKey === activeKey) { - activeIndex = ci; - return false; - } - return true; - }); - if (!this.props.defaultActiveFirst && activeIndex !== -1 && allDisabled(children.slice(activeIndex, len - 1))) { - return undefined; - } - const start = (activeIndex + 1) % len; - let i = start; - - do { - const child = children[i]; - if (!child || child.props.disabled) { - i = (i + 1) % len; - } else { - return child; - } - } while (i !== start); - - return null; - } } export default connect()(SubPopupMenu);