-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathTabs.tsx
174 lines (151 loc) · 3.83 KB
/
Tabs.tsx
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React from 'react';
import PropTypes from 'prop-types';
import requiredForA11y from 'prop-types-extra/lib/isRequiredForA11y';
import { useUncontrolled } from 'uncontrollable';
import Nav from './Nav';
import NavLink from './NavLink';
import NavItem from './NavItem';
import TabContainer from './TabContainer';
import TabContent from './TabContent';
import TabPane from './TabPane';
import { forEach, map } from './ElementChildren';
import { SelectCallback, TransitionType } from './helpers';
import { EventKey } from './types';
export interface TabsProps extends React.PropsWithChildren<unknown> {
activeKey?: EventKey;
defaultActiveKey?: EventKey;
onSelect?: SelectCallback;
variant?: 'tabs' | 'pills';
transition?: TransitionType;
id?: string;
mountOnEnter?: boolean;
unmountOnExit?: boolean;
}
const propTypes = {
/**
* Mark the Tab with a matching `eventKey` as active.
*
* @controllable onSelect
*/
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/** The default active key that is selected on start */
defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Navigation style
*
* @type {('tabs'| 'pills')}
*/
variant: PropTypes.string,
/**
* Sets a default animation strategy for all children `<TabPane>`s.
* Defaults to `<Fade>` animation, else use `false` to disable or a
* react-transition-group `<Transition/>` component.
*
* @type {Transition | false}
* @default {Fade}
*/
transition: PropTypes.oneOfType([
PropTypes.oneOf([false]),
PropTypes.elementType,
]),
/**
* HTML id attribute, required if no `generateChildId` prop
* is specified.
*
* @type {string}
*/
id: requiredForA11y(PropTypes.string),
/**
* Callback fired when a Tab is selected.
*
* ```js
* function (
* Any eventKey,
* SyntheticEvent event?
* )
* ```
*
* @controllable activeKey
*/
onSelect: PropTypes.func,
/**
* Wait until the first "enter" transition to mount tabs (add them to the DOM)
*/
mountOnEnter: PropTypes.bool,
/**
* Unmount tabs (remove it from the DOM) when it is no longer visible
*/
unmountOnExit: PropTypes.bool,
};
const defaultProps = {
variant: 'tabs',
mountOnEnter: false,
unmountOnExit: false,
};
function getDefaultActiveKey(children) {
let defaultActiveKey;
forEach(children, (child) => {
if (defaultActiveKey == null) {
defaultActiveKey = child.props.eventKey;
}
});
return defaultActiveKey;
}
function renderTab(child) {
const { title, eventKey, disabled, tabClassName, id } = child.props;
if (title == null) {
return null;
}
return (
<NavItem
as={NavLink}
eventKey={eventKey}
disabled={disabled}
id={id}
className={tabClassName}
>
{title}
</NavItem>
);
}
const Tabs = (props: TabsProps) => {
const {
id,
onSelect,
transition,
mountOnEnter,
unmountOnExit,
children,
activeKey = getDefaultActiveKey(children),
...controlledProps
} = useUncontrolled(props, {
activeKey: 'onSelect',
});
return (
<TabContainer
id={id}
activeKey={activeKey}
onSelect={onSelect}
transition={transition}
mountOnEnter={mountOnEnter}
unmountOnExit={unmountOnExit}
>
<Nav {...controlledProps} role="tablist" as="nav">
{map(children, renderTab)}
</Nav>
<TabContent>
{map(children, (child) => {
const childProps = { ...child.props };
delete childProps.title;
delete childProps.disabled;
delete childProps.tabClassName;
return <TabPane {...childProps} />;
})}
</TabContent>
</TabContainer>
);
};
Tabs.propTypes = propTypes as any;
Tabs.defaultProps = defaultProps as any;
Tabs.displayName = 'Tabs';
export default Tabs;