UNPKG

3.71 kBJavaScriptView Raw
1import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
2import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
3import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
4import _inherits from 'babel-runtime/helpers/inherits';
5import React from 'react';
6import PropTypes from 'prop-types';
7import uncontrollable from 'uncontrollable';
8
9var TAB = 'tab';
10var PANE = 'pane';
11
12var idPropType = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
13
14var propTypes = {
15 /**
16 * HTML id attribute, required if no `generateChildId` prop
17 * is specified.
18 */
19 id: function id(props) {
20 var error = null;
21
22 if (!props.generateChildId) {
23 for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
24 args[_key - 1] = arguments[_key];
25 }
26
27 error = idPropType.apply(undefined, [props].concat(args));
28
29 if (!error && !props.id) {
30 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');
31 }
32 }
33
34 return error;
35 },
36
37
38 /**
39 * A function that takes an `eventKey` and `type` and returns a unique id for
40 * child tab `<NavItem>`s and `<TabPane>`s. The function _must_ be a pure
41 * function, meaning it should always return the _same_ id for the same set
42 * of inputs. The default value requires that an `id` to be set for the
43 * `<TabContainer>`.
44 *
45 * The `type` argument will either be `"tab"` or `"pane"`.
46 *
47 * @defaultValue (eventKey, type) => `${this.props.id}-${type}-${key}`
48 */
49 generateChildId: PropTypes.func,
50
51 /**
52 * A callback fired when a tab is selected.
53 *
54 * @controllable activeKey
55 */
56 onSelect: PropTypes.func,
57
58 /**
59 * The `eventKey` of the currently active tab.
60 *
61 * @controllable onSelect
62 */
63 activeKey: PropTypes.any
64};
65
66var childContextTypes = {
67 $bs_tabContainer: PropTypes.shape({
68 activeKey: PropTypes.any,
69 onSelect: PropTypes.func.isRequired,
70 getTabId: PropTypes.func.isRequired,
71 getPaneId: PropTypes.func.isRequired
72 })
73};
74
75var TabContainer = function (_React$Component) {
76 _inherits(TabContainer, _React$Component);
77
78 function TabContainer() {
79 _classCallCheck(this, TabContainer);
80
81 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
82 }
83
84 TabContainer.prototype.getChildContext = function getChildContext() {
85 var _props = this.props,
86 activeKey = _props.activeKey,
87 onSelect = _props.onSelect,
88 generateChildId = _props.generateChildId,
89 id = _props.id;
90
91
92 var getId = generateChildId || function (key, type) {
93 return id ? id + '-' + type + '-' + key : null;
94 };
95
96 return {
97 $bs_tabContainer: {
98 activeKey: activeKey,
99 onSelect: onSelect,
100 getTabId: function getTabId(key) {
101 return getId(key, TAB);
102 },
103 getPaneId: function getPaneId(key) {
104 return getId(key, PANE);
105 }
106 }
107 };
108 };
109
110 TabContainer.prototype.render = function render() {
111 var _props2 = this.props,
112 children = _props2.children,
113 props = _objectWithoutProperties(_props2, ['children']);
114
115 delete props.generateChildId;
116 delete props.onSelect;
117 delete props.activeKey;
118
119 return React.cloneElement(React.Children.only(children), props);
120 };
121
122 return TabContainer;
123}(React.Component);
124
125TabContainer.propTypes = propTypes;
126TabContainer.childContextTypes = childContextTypes;
127
128export default uncontrollable(TabContainer, { activeKey: 'onSelect' });
\No newline at end of file