UNPKG

2.22 kBJavaScriptView Raw
1import React, { cloneElement } from 'react';
2import PropTypes from 'prop-types';
3import classnames from 'classnames';
4import { getDataAttr } from './utils';
5
6export default class TabBarRootNode extends React.Component {
7
8 getExtraContentStyle = () => {
9 const { tabBarPosition, direction } = this.props;
10 const topOrBottom = (tabBarPosition === 'top' || tabBarPosition === 'bottom');
11 if (direction === 'rtl') {
12 return topOrBottom ? { float: 'left' } : {};
13 }
14 return topOrBottom ? { float: 'right' } : {};
15 }
16
17 render() {
18 const {
19 clsPrefix, onKeyDown, className, extraContent, style, tabBarPosition, children, direction,
20 ...restProps
21 } = this.props;
22 const cls = classnames(`${clsPrefix}-bar`, {
23 // [className]: !!className,
24 });
25 const topOrBottom = (tabBarPosition === 'top' || tabBarPosition === 'bottom');
26 const extraContentStyle = (extraContent && extraContent.props) ? extraContent.props.style : {};
27 let newChildren = children;
28 if (extraContent) {
29 newChildren = [
30 cloneElement(extraContent, {
31 key: 'extra',
32 style: {
33 ...this.getExtraContentStyle(topOrBottom, direction),
34 ...extraContentStyle,
35 },
36 }),
37 cloneElement(children, { key: 'content' }),
38 ];
39 newChildren = topOrBottom ? newChildren : newChildren.reverse();
40 }
41 return (
42 <div
43 role="tablist"
44 className={cls}
45 tabIndex="0"
46 ref={this.props.saveRef('root')}
47 onKeyDown={onKeyDown}
48 // style={style}
49 {...getDataAttr(restProps)}
50 >
51 {newChildren}
52 </div>
53 );
54 }
55}
56
57TabBarRootNode.propTypes = {
58 clsPrefix: PropTypes.string,
59 className: PropTypes.string,
60 style: PropTypes.object,
61 tabBarPosition: PropTypes.oneOf(['left', 'right', 'top', 'bottom']),
62 children: PropTypes.node,
63 extraContent: PropTypes.node,
64 onKeyDown: PropTypes.func,
65 saveRef: PropTypes.func,
66 direction: PropTypes.oneOf(['ltr', 'rtl']),
67};
68
69TabBarRootNode.defaultProps = {
70 clsPrefix: '',
71 className: '',
72 style: {},
73 tabBarPosition: 'top',
74 extraContent: null,
75 children: null,
76 onKeyDown: () => { },
77 saveRef: () => { },
78};