1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
3 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
4 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
5 | import _inherits from 'babel-runtime/helpers/inherits';
|
6 | import classNames from 'classnames';
|
7 | import React from 'react';
|
8 | import PropTypes from 'prop-types';
|
9 | import elementType from 'prop-types-extra/lib/elementType';
|
10 |
|
11 | import { bsClass as setBsClass, prefix, splitBsPropsAndOmit } from './utils/bootstrapUtils';
|
12 |
|
13 | var propTypes = {
|
14 | componentClass: elementType,
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 | animation: PropTypes.oneOfType([PropTypes.bool, elementType]),
|
22 |
|
23 | |
24 |
|
25 |
|
26 | mountOnEnter: PropTypes.bool,
|
27 |
|
28 | |
29 |
|
30 |
|
31 | unmountOnExit: PropTypes.bool
|
32 | };
|
33 |
|
34 | var defaultProps = {
|
35 | componentClass: 'div',
|
36 | animation: true,
|
37 | mountOnEnter: false,
|
38 | unmountOnExit: false
|
39 | };
|
40 |
|
41 | var contextTypes = {
|
42 | $bs_tabContainer: PropTypes.shape({
|
43 | activeKey: PropTypes.any
|
44 | })
|
45 | };
|
46 |
|
47 | var childContextTypes = {
|
48 | $bs_tabContent: PropTypes.shape({
|
49 | bsClass: PropTypes.string,
|
50 | animation: PropTypes.oneOfType([PropTypes.bool, elementType]),
|
51 | activeKey: PropTypes.any,
|
52 | mountOnEnter: PropTypes.bool,
|
53 | unmountOnExit: PropTypes.bool,
|
54 | onPaneEnter: PropTypes.func.isRequired,
|
55 | onPaneExited: PropTypes.func.isRequired,
|
56 | exiting: PropTypes.bool.isRequired
|
57 | })
|
58 | };
|
59 |
|
60 | var TabContent = function (_React$Component) {
|
61 | _inherits(TabContent, _React$Component);
|
62 |
|
63 | function TabContent(props, context) {
|
64 | _classCallCheck(this, TabContent);
|
65 |
|
66 | var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
|
67 |
|
68 | _this.handlePaneEnter = _this.handlePaneEnter.bind(_this);
|
69 | _this.handlePaneExited = _this.handlePaneExited.bind(_this);
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | _this.state = {
|
75 | activeKey: null,
|
76 | activeChild: null
|
77 | };
|
78 | return _this;
|
79 | }
|
80 |
|
81 | TabContent.prototype.getChildContext = function getChildContext() {
|
82 | var _props = this.props,
|
83 | bsClass = _props.bsClass,
|
84 | animation = _props.animation,
|
85 | mountOnEnter = _props.mountOnEnter,
|
86 | unmountOnExit = _props.unmountOnExit;
|
87 |
|
88 |
|
89 | var stateActiveKey = this.state.activeKey;
|
90 | var containerActiveKey = this.getContainerActiveKey();
|
91 |
|
92 | var activeKey = stateActiveKey != null ? stateActiveKey : containerActiveKey;
|
93 | var exiting = stateActiveKey != null && stateActiveKey !== containerActiveKey;
|
94 |
|
95 | return {
|
96 | $bs_tabContent: {
|
97 | bsClass: bsClass,
|
98 | animation: animation,
|
99 | activeKey: activeKey,
|
100 | mountOnEnter: mountOnEnter,
|
101 | unmountOnExit: unmountOnExit,
|
102 | onPaneEnter: this.handlePaneEnter,
|
103 | onPaneExited: this.handlePaneExited,
|
104 | exiting: exiting
|
105 | }
|
106 | };
|
107 | };
|
108 |
|
109 | TabContent.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
110 | if (!nextProps.animation && this.state.activeChild) {
|
111 | this.setState({ activeKey: null, activeChild: null });
|
112 | }
|
113 | };
|
114 |
|
115 | TabContent.prototype.componentWillUnmount = function componentWillUnmount() {
|
116 | this.isUnmounted = true;
|
117 | };
|
118 |
|
119 | TabContent.prototype.getContainerActiveKey = function getContainerActiveKey() {
|
120 | var tabContainer = this.context.$bs_tabContainer;
|
121 | return tabContainer && tabContainer.activeKey;
|
122 | };
|
123 |
|
124 | TabContent.prototype.handlePaneEnter = function handlePaneEnter(child, childKey) {
|
125 | if (!this.props.animation) {
|
126 | return false;
|
127 | }
|
128 |
|
129 |
|
130 | if (childKey !== this.getContainerActiveKey()) {
|
131 | return false;
|
132 | }
|
133 |
|
134 | this.setState({
|
135 | activeKey: childKey,
|
136 | activeChild: child
|
137 | });
|
138 |
|
139 | return true;
|
140 | };
|
141 |
|
142 | TabContent.prototype.handlePaneExited = function handlePaneExited(child) {
|
143 |
|
144 | if (this.isUnmounted) {
|
145 | return;
|
146 | }
|
147 |
|
148 | this.setState(function (_ref) {
|
149 | var activeChild = _ref.activeChild;
|
150 |
|
151 | if (activeChild !== child) {
|
152 | return null;
|
153 | }
|
154 |
|
155 | return {
|
156 | activeKey: null,
|
157 | activeChild: null
|
158 | };
|
159 | });
|
160 | };
|
161 |
|
162 | TabContent.prototype.render = function render() {
|
163 | var _props2 = this.props,
|
164 | Component = _props2.componentClass,
|
165 | className = _props2.className,
|
166 | props = _objectWithoutProperties(_props2, ['componentClass', 'className']);
|
167 |
|
168 | var _splitBsPropsAndOmit = splitBsPropsAndOmit(props, ['animation', 'mountOnEnter', 'unmountOnExit']),
|
169 | bsProps = _splitBsPropsAndOmit[0],
|
170 | elementProps = _splitBsPropsAndOmit[1];
|
171 |
|
172 | return React.createElement(Component, _extends({}, elementProps, {
|
173 | className: classNames(className, prefix(bsProps, 'content'))
|
174 | }));
|
175 | };
|
176 |
|
177 | return TabContent;
|
178 | }(React.Component);
|
179 |
|
180 | TabContent.propTypes = propTypes;
|
181 | TabContent.defaultProps = defaultProps;
|
182 | TabContent.contextTypes = contextTypes;
|
183 | TabContent.childContextTypes = childContextTypes;
|
184 |
|
185 | export default setBsClass('tab', TabContent); |
\ | No newline at end of file |