UNPKG

1.88 kBJavaScriptView Raw
1/**
2* This source code is quoted from rc-tabs.
3* homepage: https://github.com/react-component/tabs
4*/
5import React from 'react';
6import PropTypes from 'prop-types';
7import classnames from 'classnames';
8
9export default class TabBarSwipeableTabs extends React.Component {
10 render() {
11 const props = this.props;
12 const children = props.panels;
13 const activeKey = props.activeKey;
14 const rst = [];
15 const clsPrefix = props.clsPrefix;
16
17 const _flexWidth = `${1 / props.pageSize * 100}%`;
18 const tabStyle = {
19 WebkitFlexBasis: _flexWidth,
20 flexBasis: _flexWidth,
21 };
22
23 React.Children.forEach(children, (child) => {
24 if (!child) {
25 return;
26 }
27 const key = child.key;
28 const cls = classnames(`${clsPrefix}-tab`, {
29 [`${clsPrefix}-tab-active`]: activeKey === key,
30 [`${clsPrefix}-tab-disabled`]: child.props.disabled,
31 });
32 let events = {};
33 if (!child.props.disabled) {
34 events = {
35 onClick: this.props.onTabClick.bind(this, key),
36 };
37 }
38 const refProps = {};
39 if (activeKey === key) {
40 refProps.ref = this.props.saveRef('activeTab');
41 }
42 rst.push(<div
43 role="tab"
44 style={tabStyle}
45 aria-disabled={child.props.disabled ? 'true' : 'false'}
46 aria-selected={activeKey === key ? 'true' : 'false'}
47 {...events}
48 className={cls}
49 key={key}
50 {...refProps}
51 >
52 {child.props.tab}
53 </div>);
54 });
55
56 return rst;
57 }
58}
59
60TabBarSwipeableTabs.propTypes = {
61 pageSize: PropTypes.number,
62 onTabClick: PropTypes.func,
63 saveRef: PropTypes.func,
64 destroyInactiveTabPane: PropTypes.bool,
65 clsPrefix: PropTypes.string,
66 activeKey: PropTypes.string,
67 panels: PropTypes.node,
68};
69
70TabBarSwipeableTabs.defaultProps = {
71 pageSize: 5,
72 onTabClick: () => {},
73 saveRef: () => {},
74};