UNPKG

5.6 kBJavaScriptView Raw
1import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
3import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
4import _assertThisInitialized from "@babel/runtime-corejs2/helpers/esm/assertThisInitialized";
5import classNames from 'classnames';
6import React from 'react';
7import PropTypes from 'prop-types';
8import elementType from 'prop-types-extra/lib/elementType';
9import { bsClass as setBsClass, prefix, splitBsPropsAndOmit } from './utils/bootstrapUtils';
10var propTypes = {
11 componentClass: elementType,
12
13 /**
14 * Sets a default animation strategy for all children `<TabPane>`s. Use
15 * `false` to disable, `true` to enable the default `<Fade>` animation or
16 * a react-transition-group v2 `<Transition/>` component.
17 */
18 animation: PropTypes.oneOfType([PropTypes.bool, elementType]),
19
20 /**
21 * Wait until the first "enter" transition to mount tabs (add them to the DOM)
22 */
23 mountOnEnter: PropTypes.bool,
24
25 /**
26 * Unmount tabs (remove it from the DOM) when they are no longer visible
27 */
28 unmountOnExit: PropTypes.bool
29};
30var defaultProps = {
31 componentClass: 'div',
32 animation: true,
33 mountOnEnter: false,
34 unmountOnExit: false
35};
36var contextTypes = {
37 $bs_tabContainer: PropTypes.shape({
38 activeKey: PropTypes.any
39 })
40};
41var childContextTypes = {
42 $bs_tabContent: PropTypes.shape({
43 bsClass: PropTypes.string,
44 animation: PropTypes.oneOfType([PropTypes.bool, elementType]),
45 activeKey: PropTypes.any,
46 mountOnEnter: PropTypes.bool,
47 unmountOnExit: PropTypes.bool,
48 onPaneEnter: PropTypes.func.isRequired,
49 onPaneExited: PropTypes.func.isRequired,
50 exiting: PropTypes.bool.isRequired
51 })
52};
53
54var TabContent =
55/*#__PURE__*/
56function (_React$Component) {
57 _inheritsLoose(TabContent, _React$Component);
58
59 function TabContent(props, context) {
60 var _this;
61
62 _this = _React$Component.call(this, props, context) || this;
63 _this.handlePaneEnter = _this.handlePaneEnter.bind(_assertThisInitialized(_assertThisInitialized(_this)));
64 _this.handlePaneExited = _this.handlePaneExited.bind(_assertThisInitialized(_assertThisInitialized(_this))); // Active entries in state will be `null` unless `animation` is set. Need
65 // to track active child in case keys swap and the active child changes
66 // but the active key does not.
67
68 _this.state = {
69 activeKey: null,
70 activeChild: null
71 };
72 return _this;
73 }
74
75 var _proto = TabContent.prototype;
76
77 _proto.getChildContext = function getChildContext() {
78 var _this$props = this.props,
79 bsClass = _this$props.bsClass,
80 animation = _this$props.animation,
81 mountOnEnter = _this$props.mountOnEnter,
82 unmountOnExit = _this$props.unmountOnExit;
83 var stateActiveKey = this.state.activeKey;
84 var containerActiveKey = this.getContainerActiveKey();
85 var activeKey = stateActiveKey != null ? stateActiveKey : containerActiveKey;
86 var exiting = stateActiveKey != null && stateActiveKey !== containerActiveKey;
87 return {
88 $bs_tabContent: {
89 bsClass: bsClass,
90 animation: animation,
91 activeKey: activeKey,
92 mountOnEnter: mountOnEnter,
93 unmountOnExit: unmountOnExit,
94 onPaneEnter: this.handlePaneEnter,
95 onPaneExited: this.handlePaneExited,
96 exiting: exiting
97 }
98 };
99 };
100
101 _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
102 if (!nextProps.animation && this.state.activeChild) {
103 this.setState({
104 activeKey: null,
105 activeChild: null
106 });
107 }
108 };
109
110 _proto.componentWillUnmount = function componentWillUnmount() {
111 this.isUnmounted = true;
112 };
113
114 _proto.getContainerActiveKey = function getContainerActiveKey() {
115 var tabContainer = this.context.$bs_tabContainer;
116 return tabContainer && tabContainer.activeKey;
117 };
118
119 _proto.handlePaneEnter = function handlePaneEnter(child, childKey) {
120 if (!this.props.animation) {
121 return false;
122 } // It's possible that this child should be transitioning out.
123
124
125 if (childKey !== this.getContainerActiveKey()) {
126 return false;
127 }
128
129 this.setState({
130 activeKey: childKey,
131 activeChild: child
132 });
133 return true;
134 };
135
136 _proto.handlePaneExited = function handlePaneExited(child) {
137 // This might happen as everything is unmounting.
138 if (this.isUnmounted) {
139 return;
140 }
141
142 this.setState(function (_ref) {
143 var activeChild = _ref.activeChild;
144
145 if (activeChild !== child) {
146 return null;
147 }
148
149 return {
150 activeKey: null,
151 activeChild: null
152 };
153 });
154 };
155
156 _proto.render = function render() {
157 var _this$props2 = this.props,
158 Component = _this$props2.componentClass,
159 className = _this$props2.className,
160 props = _objectWithoutPropertiesLoose(_this$props2, ["componentClass", "className"]);
161
162 var _splitBsPropsAndOmit = splitBsPropsAndOmit(props, ['animation', 'mountOnEnter', 'unmountOnExit']),
163 bsProps = _splitBsPropsAndOmit[0],
164 elementProps = _splitBsPropsAndOmit[1];
165
166 return React.createElement(Component, _extends({}, elementProps, {
167 className: classNames(className, prefix(bsProps, 'content'))
168 }));
169 };
170
171 return TabContent;
172}(React.Component);
173
174TabContent.propTypes = propTypes;
175TabContent.defaultProps = defaultProps;
176TabContent.contextTypes = contextTypes;
177TabContent.childContextTypes = childContextTypes;
178export default setBsClass('tab', TabContent);
\No newline at end of file