UNPKG

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