forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabContainer.js
More file actions
103 lines (85 loc) · 2.54 KB
/
TabContainer.js
File metadata and controls
103 lines (85 loc) · 2.54 KB
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
import React, { PropTypes } from 'react';
import uncontrollable from 'uncontrollable';
const TAB = 'tab';
const PANE = 'pane';
const idPropType = PropTypes.oneOfType([
PropTypes.string, PropTypes.number,
]);
const propTypes = {
/**
* HTML id attribute, required if no `generateChildId` prop
* is specified.
*/
id(props, ...args) {
let error = null;
if (!props.generateChildId) {
error = idPropType(props, ...args);
if (!error && !props.id) {
error = new Error(
'In order to properly initialize Tabs in a way that is accessible ' +
'to assistive technologies (such as screen readers) an `id` or a ' +
'`generateChildId` prop to TabContainer is required'
);
}
}
return error;
},
/**
* A function that takes an `eventKey` and `type` and returns a unique id for
* child tab `<NavItem>`s and `<TabPane>`s. The function _must_ be a pure
* function, meaning it should always return the _same_ id for the same set
* of inputs. The default value requires that an `id` to be set for the
* `<TabContainer>`.
*
* The `type` argument will either be `"tab"` or `"pane"`.
*
* @defaultValue (eventKey, type) => `${this.props.id}-${type}-${key}`
*/
generateChildId: PropTypes.func,
/**
* A callback fired when a tab is selected.
*
* @controllable activeKey
*/
onSelect: PropTypes.func,
/**
* The `eventKey` of the currently active tab.
*
* @controllable onSelect
*/
activeKey: PropTypes.any,
};
const childContextTypes = {
$bs_tabContainer: React.PropTypes.shape({
activeKey: PropTypes.any,
onSelect: PropTypes.func.isRequired,
getTabId: PropTypes.func.isRequired,
getPaneId: PropTypes.func.isRequired,
}),
};
class TabContainer extends React.Component {
getChildContext() {
const { activeKey, onSelect, generateChildId, id } = this.props;
const getId =
generateChildId ||
((key, type) => (id ? `${id}-${type}-${key}` : null));
return {
$bs_tabContainer: {
activeKey,
onSelect,
getTabId: key => getId(key, TAB),
getPaneId: key => getId(key, PANE),
},
};
}
render() {
const { children, ...props } = this.props;
delete props.generateChildId;
delete props.onSelect;
delete props.activeKey;
return React.cloneElement(React.Children.only(children), props);
}
}
TabContainer.propTypes = propTypes;
TabContainer.childContextTypes = childContextTypes;
export default uncontrollable(TabContainer, { activeKey: 'onSelect' });