UNPKG

9.91 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 } from 'react';
10import PropTypes from 'prop-types';
11import classNames from 'classnames';
12import ConfigProvider from '../config-provider';
13import Menu from '../menu';
14
15/**
16 * Nav
17 * @description 继承自 `Menu` 的能力请查看 `Menu` 文档
18 */
19var Nav = (_temp = _class = function (_Component) {
20 _inherits(Nav, _Component);
21
22 function Nav(props) {
23 _classCallCheck(this, Nav);
24
25 var _this = _possibleConstructorReturn(this, _Component.call(this, props));
26
27 _this.getMenuRef = _this.getMenuRef.bind(_this);
28 return _this;
29 }
30
31 Nav.prototype.getChildContext = function getChildContext() {
32 var _props = this.props,
33 prefix = _props.prefix,
34 direction = _props.direction,
35 mode = _props.mode,
36 iconOnly = _props.iconOnly,
37 iconOnlyWidth = _props.iconOnlyWidth,
38 iconTextOnly = _props.iconTextOnly,
39 hasTooltip = _props.hasTooltip,
40 hasArrow = _props.hasArrow;
41 var isCollapse = this.context.isCollapse;
42
43
44 return {
45 prefix: prefix,
46 mode: direction === 'hoz' ? 'popup' : mode,
47 iconOnly: 'iconOnly' in this.props ? iconOnly : isCollapse,
48 iconOnlyWidth: 'iconOnlyWidth' in this.props ? iconOnlyWidth : undefined,
49 iconTextOnly: iconTextOnly,
50 hasTooltip: hasTooltip,
51 hasArrow: hasArrow
52 };
53 };
54
55 Nav.prototype.getMenuRef = function getMenuRef(ref) {
56 this.menu = ref;
57 };
58
59 Nav.prototype.render = function render() {
60 var _classNames, _classNames2;
61
62 // eslint-disable-next-line
63 var _props2 = this.props,
64 prefix = _props2.prefix,
65 className = _props2.className,
66 style = _props2.style,
67 children = _props2.children,
68 type = _props2.type,
69 direction = _props2.direction,
70 activeDirection = _props2.activeDirection,
71 mode = _props2.mode,
72 triggerType = _props2.triggerType,
73 inlineIndent = _props2.inlineIndent,
74 openMode = _props2.openMode,
75 popupAlign = _props2.popupAlign,
76 popupClassName = _props2.popupClassName,
77 iconOnly = _props2.iconOnly,
78 iconOnlyWidth = _props2.iconOnlyWidth,
79 iconTextOnly = _props2.iconTextOnly,
80 hasArrow = _props2.hasArrow,
81 hasTooltip = _props2.hasTooltip,
82 embeddable = _props2.embeddable,
83 _popupProps = _props2.popupProps,
84 rtl = _props2.rtl,
85 others = _objectWithoutProperties(_props2, ['prefix', 'className', 'style', 'children', 'type', 'direction', 'activeDirection', 'mode', 'triggerType', 'inlineIndent', 'openMode', 'popupAlign', 'popupClassName', 'iconOnly', 'iconOnlyWidth', 'iconTextOnly', 'hasArrow', 'hasTooltip', 'embeddable', 'popupProps', 'rtl']);
86
87 var isCollapse = this.context.isCollapse;
88
89
90 var newIconOnly = 'iconOnly' in this.props ? iconOnly : isCollapse;
91
92 var realActiveDirection = activeDirection;
93 if (realActiveDirection && (direction === 'hoz' && (realActiveDirection === 'left' || realActiveDirection === 'right') || direction === 'ver' && (realActiveDirection === 'top' || realActiveDirection === 'bottom'))) {
94 realActiveDirection = null;
95 }
96
97 if (!newIconOnly && realActiveDirection === undefined) {
98 realActiveDirection = direction === 'hoz' ? 'bottom' : type === 'line' ? 'right' : 'left';
99 }
100
101 var cls = classNames((_classNames = {}, _classNames[prefix + 'nav'] = true, _classNames['' + prefix + type] = type, _classNames[prefix + 'active'] = realActiveDirection, _classNames['' + prefix + realActiveDirection] = realActiveDirection, _classNames[prefix + 'icon-only'] = newIconOnly, _classNames[prefix + 'icon-only-text'] = newIconOnly && iconTextOnly, _classNames[prefix + 'custom-icon-only-width'] = newIconOnly && 'iconOnlyWidth' in this.props, _classNames[prefix + 'no-arrow'] = !hasArrow, _classNames[prefix + 'nav-embeddable'] = embeddable, _classNames[className] = !!className, _classNames));
102 var newStyle = newIconOnly ? _extends({}, style, { width: iconOnlyWidth || 58 }) : style;
103
104 var props = {
105 prefix: prefix,
106 direction: direction,
107 openMode: openMode,
108 triggerType: triggerType,
109 mode: direction === 'hoz' ? 'popup' : mode,
110 popupAlign: direction === 'hoz' ? 'follow' : popupAlign,
111 inlineIndent: newIconOnly ? 0 : inlineIndent,
112 hasSelectedIcon: false,
113 popupAutoWidth: true,
114 selectMode: 'single',
115 itemClassName: prefix + 'nav-item',
116 popupClassName: classNames((_classNames2 = {}, _classNames2[cls.replace(prefix + 'icon-only', '').replace(prefix + 'nav-embeddable', '')] = mode === 'popup', _classNames2[prefix + 'icon-only'] = newIconOnly && mode === 'inline', _classNames2[popupClassName] = !!popupClassName, _classNames2)),
117 popupProps: function popupProps() {
118 return _extends({}, _popupProps);
119 }
120 };
121
122 return React.createElement(
123 Menu,
124 _extends({ className: cls, style: newStyle }, props, others, { ref: this.getMenuRef }),
125 children
126 );
127 };
128
129 return Nav;
130}(Component), _class.propTypes = {
131 prefix: PropTypes.string,
132 pure: PropTypes.bool,
133 rtl: PropTypes.bool,
134 className: PropTypes.string,
135 style: PropTypes.object,
136 /**
137 * 导航项和子导航
138 */
139 children: PropTypes.node,
140 /**
141 * 导航类型
142 * @enumdesc 普通, 主要, 次要, 线形
143 */
144 type: PropTypes.oneOf(['normal', 'primary', 'secondary', 'line']),
145 /**
146 * 导航布局
147 * @enumdesc 水平, 垂直
148 */
149 direction: PropTypes.oneOf(['hoz', 'ver']),
150 /**
151 * 横向导航条 items 和 footer 的对齐方向,在 direction 设置为 'hoz' 并且 header 存在时生效
152 */
153 hozAlign: PropTypes.oneOf(['left', 'right']),
154 /**
155 * 设置组件选中状态的 active 边方向
156 * @enumdesc 无, 上, 下, 左, 右
157 * @default 当 direction 为 'hoz' 时,默认值为 'bottom',当 direction 为 'ver' 时,默认值为 'left'
158 */
159 activeDirection: PropTypes.oneOf([null, 'top', 'bottom', 'left', 'right']),
160 /**
161 * 子导航打开的模式(水平导航只支持弹出)
162 * @eumdesc 行内, 弹出
163 */
164 mode: PropTypes.oneOf(['inline', 'popup']),
165 /**
166 * 子导航打开的触发方式
167 */
168 triggerType: PropTypes.oneOf(['click', 'hover']),
169 /**
170 * 内联子导航缩进距离
171 */
172 inlineIndent: PropTypes.number,
173 /**
174 * 首次渲染展开所有的子导航,只在 mode 设置为 'inline' 以及 openMode 设置为 'multiple' 下生效
175 */
176 defaultOpenAll: PropTypes.bool,
177 /**
178 * 内联子导航的展开模式,同时可以展开一个同级子导航还是多个同级子导航,该属性仅在 mode 为 inline 时生效
179 * @eumdesc 一个, 多个
180 */
181 openMode: PropTypes.oneOf(['single', 'multiple']),
182 /**
183 * 当前选中导航项的 key 值
184 */
185 selectedKeys: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
186 /**
187 * 初始选中导航项的 key 值
188 */
189 defaultSelectedKeys: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
190 /**
191 * 选中或取消选中导航项触发的回调函数
192 * @param {Array} selectedKeys 选中的所有导航项的 key
193 * @param {Object} item 选中或取消选中的导航项
194 * @param {Object} extra 额外参数
195 * @param {Boolean} extra.select 是否是选中
196 * @param {Array} extra.key 导航项的 key
197 * @param {Object} extra.label 导航项的文本
198 * @param {Array} extra.keyPath 导航项 key 的路径
199 */
200 onSelect: PropTypes.func,
201 /**
202 * 弹出子导航的对齐方式(水平导航只支持 follow )
203 * @eumdesc Item 顶端对齐, Nav 顶端对齐
204 */
205 popupAlign: PropTypes.oneOf(['follow', 'outside']),
206 /**
207 * 弹出子导航的自定义类名
208 */
209 popupClassName: PropTypes.string,
210 /**
211 * 是否只显示图标
212 */
213 iconOnly: PropTypes.bool,
214 /**
215 * iconOnly 模式下的宽度(仅在 iconOnly=true 时生效) 如果传入了iconOnlyWidth,那么会隐藏文本,例如 Nav.Item 的 label
216 */
217 iconOnlyWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
218 /**
219 * iconOnly模式下是否展示文字(仅在 iconOnly=true 时生效)
220 */
221 iconTextOnly: PropTypes.bool,
222 /**
223 * 是否显示右侧的箭头(仅在 iconOnly=true 时生效)
224 */
225 hasArrow: PropTypes.bool,
226 /**
227 * 是否有 ToolTips (仅在 iconOnly=true 时生效)
228 */
229 hasTooltip: PropTypes.bool,
230 /**
231 * 自定义导航头部
232 */
233 header: PropTypes.node,
234 /**
235 * 自定义导航尾部
236 */
237 footer: PropTypes.node,
238 /**
239 * 是否开启嵌入式模式,一般用于Layout的布局中,开启后没有默认背景、外层border、box-shadow,可以配合`<Nav style={{lineHeight: '100px'}}>` 自定义高度
240 * @version 1.18
241 */
242 embeddable: PropTypes.bool,
243 popupProps: PropTypes.object
244}, _class.defaultProps = {
245 prefix: 'next-',
246 pure: false,
247 type: 'normal',
248 direction: 'ver',
249 hozAlign: 'left',
250 mode: 'inline',
251 triggerType: 'click',
252 inlineIndent: 20,
253 defaultOpenAll: false,
254 openMode: 'multiple',
255 defaultSelectedKeys: [],
256 popupAlign: 'follow',
257 hasTooltip: false,
258 embeddable: false,
259 hasArrow: true,
260 popupProps: {}
261}, _class.childContextTypes = {
262 prefix: PropTypes.string,
263 mode: PropTypes.string,
264 iconOnly: PropTypes.bool,
265 iconOnlyWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
266 iconTextOnly: PropTypes.bool,
267 hasTooltip: PropTypes.bool,
268 hasArrow: PropTypes.bool
269}, _class.contextTypes = {
270 isCollapse: PropTypes.bool
271}, _temp);
272Nav.displayName = 'Nav';
273
274
275export default ConfigProvider.config(Nav);
\No newline at end of file