/
TabPane.tsx
192 lines (161 loc) · 4.65 KB
/
TabPane.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import classNames from 'classnames';
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { useBootstrapPrefix } from './ThemeProvider';
import TabContext from './TabContext';
import SelectableContext, { makeEventKey } from './SelectableContext';
import Fade from './Fade';
import {
BsPrefixProps,
BsPrefixRefForwardingComponent,
TransitionCallbacks,
TransitionType,
} from './helpers';
export interface TabPaneProps extends TransitionCallbacks, BsPrefixProps {
eventKey?: any;
active?: boolean;
transition?: TransitionType;
mountOnEnter?: boolean;
unmountOnExit?: boolean;
}
type TabPane = BsPrefixRefForwardingComponent<'div', TabPaneProps>;
const propTypes = {
/**
* @default 'tab-pane'
*/
bsPrefix: PropTypes.string,
as: PropTypes.elementType,
/**
* A key that associates the `TabPane` with it's controlling `NavLink`.
*/
eventKey: PropTypes.any,
/**
* Toggles the active state of the TabPane, this is generally controlled by a
* TabContainer.
*/
active: PropTypes.bool,
/**
* Use animation when showing or hiding `<TabPane>`s. Defaults to `<Fade>`
* animation, else use `false` to disable or a react-transition-group
* `<Transition/>` component.
*/
transition: PropTypes.oneOfType([PropTypes.bool, PropTypes.elementType]),
/**
* Transition onEnter callback when animation is not `false`
*/
onEnter: PropTypes.func,
/**
* Transition onEntering callback when animation is not `false`
*/
onEntering: PropTypes.func,
/**
* Transition onEntered callback when animation is not `false`
*/
onEntered: PropTypes.func,
/**
* Transition onExit callback when animation is not `false`
*/
onExit: PropTypes.func,
/**
* Transition onExiting callback when animation is not `false`
*/
onExiting: PropTypes.func,
/**
* Transition onExited callback when animation is not `false`
*/
onExited: PropTypes.func,
/**
* Wait until the first "enter" transition to mount the tab (add it to the DOM)
*/
mountOnEnter: PropTypes.bool,
/**
* Unmount the tab (remove it from the DOM) when it is no longer visible
*/
unmountOnExit: PropTypes.bool,
/** @ignore * */
id: PropTypes.string,
/** @ignore * */
'aria-labelledby': PropTypes.string,
};
function useTabContext(props: TabPaneProps) {
const context = useContext(TabContext);
if (!context) return props;
const { activeKey, getControlledId, getControllerId, ...rest } = context;
const shouldTransition =
props.transition !== false && rest.transition !== false;
const key = makeEventKey(props.eventKey);
return {
...props,
active:
props.active == null && key != null
? makeEventKey(activeKey) === key
: props.active,
id: getControlledId(props.eventKey),
'aria-labelledby': getControllerId(props.eventKey),
transition:
shouldTransition && (props.transition || rest.transition || Fade),
mountOnEnter:
props.mountOnEnter != null ? props.mountOnEnter : rest.mountOnEnter,
unmountOnExit:
props.unmountOnExit != null ? props.unmountOnExit : rest.unmountOnExit,
};
}
const TabPane: TabPane = React.forwardRef((props: TabPaneProps, ref) => {
const {
bsPrefix,
className,
active,
onEnter,
onEntering,
onEntered,
onExit,
onExiting,
onExited,
mountOnEnter,
unmountOnExit,
transition: Transition,
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as: Component = 'div',
eventKey: _,
...rest
} = useTabContext(props);
const prefix = useBootstrapPrefix(bsPrefix, 'tab-pane');
if (!active && !Transition && unmountOnExit) return null;
let pane = (
<Component
{...rest}
ref={ref}
role="tabpanel"
aria-hidden={!active}
className={classNames(className, prefix, { active })}
/>
);
if (Transition)
pane = (
<Transition
in={active}
onEnter={onEnter}
onEntering={onEntering}
onEntered={onEntered}
onExit={onExit}
onExiting={onExiting}
onExited={onExited}
mountOnEnter={mountOnEnter}
unmountOnExit={unmountOnExit}
>
{pane}
</Transition>
);
// We provide an empty the TabContext so `<Nav>`s in `<TabPane>`s don't
// conflict with the top level one.
return (
<TabContext.Provider value={null}>
<SelectableContext.Provider value={null}>
{pane}
</SelectableContext.Provider>
</TabContext.Provider>
);
});
TabPane.displayName = 'TabPane';
TabPane.propTypes = propTypes;
export default TabPane;