forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub-nav.jsx
95 lines (83 loc) · 2.88 KB
/
sub-nav.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React, { Component, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Menu from '../menu';
import Icon from '../icon';
/**
* Nav.SubNav
* @description 继承自 `Menu.SubMenu` 的能力请查看 `Menu.SubMenu` 文档
*/
class SubNav extends Component {
static menuChildType = 'submenu';
static propTypes = {
/**
* 自定义类名
*/
className: PropTypes.string,
/**
* 自定义图标,可以使用 Icon 的 type,也可以使用组件 `<Icon type="your type" />`
*/
icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/**
* 标签内容
*/
label: PropTypes.node,
/**
* 是否可选
*/
selectable: PropTypes.bool,
/**
* 导航项和子导航
*/
children: PropTypes.node,
/**
* 是否需要提示当前项可展开的 icon,默认是有的
*/
noIcon: PropTypes.bool,
};
static defaultProps = {
selectable: false,
};
static contextTypes = {
prefix: PropTypes.string,
mode: PropTypes.string,
iconOnly: PropTypes.bool,
iconOnlyWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
hasArrow: PropTypes.bool,
};
render() {
const { prefix, iconOnly, iconOnlyWidth, hasArrow, noIcon, mode } = this.context;
const { className, icon, label, children, level, ...others } = this.props;
const cls = classNames({
[`${prefix}nav-sub-nav-item`]: true,
[`${prefix}nav-popup`]: mode === 'popup',
[className]: !!className,
});
let iconEl = typeof icon === 'string' ? <Icon className={`${prefix}nav-icon`} type={icon} /> : icon;
// 这里是为 iconOnly 模式下,添加默认的展开按钮
// 只有在 inline 模式下 或 popup模式的第一层级,才需要添加默认的按钮
if (iconOnly && hasArrow && (mode === 'inline' || (level === 1 && mode === 'popup'))) {
iconEl = (
<Icon
className={`${prefix}nav-icon-only-arrow`}
type={mode === 'popup' ? 'arrow-right' : 'arrow-down'}
/>
);
}
const newLabel = [iconEl ? cloneElement(iconEl, { key: 'icon' }) : null];
const showLabel = !iconOnly || (iconOnly && !iconOnlyWidth);
if (showLabel) {
newLabel.push(<span key="label">{label}</span>);
}
let title;
if (typeof label === 'string') {
title = label;
}
return (
<Menu.SubMenu className={cls} label={newLabel} level={level} title={title} noIcon={noIcon} {...others}>
{children}
</Menu.SubMenu>
);
}
}
export default SubNav;