UNPKG

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