UNPKG

4.05 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6
7var _class, _temp;
8
9import React, { Component, cloneElement } from 'react';
10import PropTypes from 'prop-types';
11import classNames from 'classnames';
12import Menu from '../menu';
13import Icon from '../icon';
14
15/**
16 * Nav.SubNav
17 * @description 继承自 `Menu.SubMenu` 的能力请查看 `Menu.SubMenu` 文档
18 */
19var SubNav = (_temp = _class = function (_Component) {
20 _inherits(SubNav, _Component);
21
22 function SubNav() {
23 _classCallCheck(this, SubNav);
24
25 return _possibleConstructorReturn(this, _Component.apply(this, arguments));
26 }
27
28 SubNav.prototype.render = function render() {
29 var _classNames;
30
31 var _context = this.context,
32 prefix = _context.prefix,
33 iconOnly = _context.iconOnly,
34 iconOnlyWidth = _context.iconOnlyWidth,
35 hasArrow = _context.hasArrow,
36 noIcon = _context.noIcon,
37 mode = _context.mode;
38
39 var _props = this.props,
40 className = _props.className,
41 icon = _props.icon,
42 label = _props.label,
43 children = _props.children,
44 level = _props.level,
45 others = _objectWithoutProperties(_props, ['className', 'icon', 'label', 'children', 'level']);
46
47 var cls = classNames((_classNames = {}, _classNames[prefix + 'nav-sub-nav-item'] = true, _classNames[prefix + 'nav-popup'] = mode === 'popup', _classNames[className] = !!className, _classNames));
48 var iconEl = typeof icon === 'string' ? React.createElement(Icon, { className: prefix + 'nav-icon', type: icon }) : icon;
49 // 这里是为 iconOnly 模式下,添加默认的展开按钮
50 // 只有在 inline 模式下 或 popup模式的第一层级,才需要添加默认的按钮
51 if (iconOnly && hasArrow && (mode === 'inline' || level === 1 && mode === 'popup')) {
52 iconEl = React.createElement(Icon, {
53 className: prefix + 'nav-icon-only-arrow',
54 type: mode === 'popup' ? 'arrow-right' : 'arrow-down'
55 });
56 }
57
58 var newLabel = [iconEl ? cloneElement(iconEl, { key: 'icon' }) : null];
59
60 var showLabel = !iconOnly || iconOnly && !iconOnlyWidth;
61
62 if (showLabel) {
63 newLabel.push(React.createElement(
64 'span',
65 { key: 'label' },
66 label
67 ));
68 }
69
70 var title = void 0;
71
72 if (typeof label === 'string') {
73 title = label;
74 }
75
76 return React.createElement(
77 Menu.SubMenu,
78 _extends({ className: cls, label: newLabel, level: level, title: title, noIcon: noIcon }, others),
79 children
80 );
81 };
82
83 return SubNav;
84}(Component), _class.menuChildType = 'submenu', _class.propTypes = {
85 /**
86 * 自定义类名
87 */
88 className: PropTypes.string,
89 /**
90 * 自定义图标,可以使用 Icon 的 type,也可以使用组件 `<Icon type="your type" />`
91 */
92 icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
93 /**
94 * 标签内容
95 */
96 label: PropTypes.node,
97 /**
98 * 是否可选
99 */
100 selectable: PropTypes.bool,
101 /**
102 * 导航项和子导航
103 */
104 children: PropTypes.node,
105 /**
106 * 是否需要提示当前项可展开的 icon,默认是有的
107 */
108 noIcon: PropTypes.bool
109}, _class.defaultProps = {
110 selectable: false
111}, _class.contextTypes = {
112 prefix: PropTypes.string,
113 mode: PropTypes.string,
114 iconOnly: PropTypes.bool,
115 iconOnlyWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
116 hasArrow: PropTypes.bool
117}, _temp);
118SubNav.displayName = 'SubNav';
119
120
121export default SubNav;
\No newline at end of file