1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.Tabs = exports.StateType = undefined;
|
7 |
|
8 | var _extends2 = require('babel-runtime/helpers/extends');
|
9 |
|
10 | var _extends3 = _interopRequireDefault(_extends2);
|
11 |
|
12 | var _createClass2 = require('babel-runtime/helpers/createClass');
|
13 |
|
14 | var _createClass3 = _interopRequireDefault(_createClass2);
|
15 |
|
16 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
17 |
|
18 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
19 |
|
20 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
21 |
|
22 | var _inherits3 = _interopRequireDefault(_inherits2);
|
23 |
|
24 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
25 |
|
26 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
27 |
|
28 | var _react = require('react');
|
29 |
|
30 | var _react2 = _interopRequireDefault(_react);
|
31 |
|
32 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
33 |
|
34 | var StateType = exports.StateType = function StateType() {
|
35 | (0, _classCallCheck3['default'])(this, StateType);
|
36 | };
|
37 |
|
38 | var instanceId = 0;
|
39 |
|
40 | var Tabs = exports.Tabs = function (_React$PureComponent) {
|
41 | (0, _inherits3['default'])(Tabs, _React$PureComponent);
|
42 |
|
43 | function Tabs(props) {
|
44 | (0, _classCallCheck3['default'])(this, Tabs);
|
45 |
|
46 | var _this = (0, _possibleConstructorReturn3['default'])(this, (Tabs.__proto__ || Object.getPrototypeOf(Tabs)).call(this, props));
|
47 |
|
48 | _this.tabCache = {};
|
49 | _this.isTabVertical = function () {
|
50 | var direction = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props.tabDirection;
|
51 | return direction === 'vertical';
|
52 | };
|
53 | _this.shouldRenderTab = function (idx) {
|
54 | var _this$props$prerender = _this.props.prerenderingSiblingsNumber,
|
55 | prerenderingSiblingsNumber = _this$props$prerender === undefined ? 0 : _this$props$prerender;
|
56 | var _this$state$currentTa = _this.state.currentTab,
|
57 | currentTab = _this$state$currentTa === undefined ? 0 : _this$state$currentTa;
|
58 |
|
59 | return currentTab - prerenderingSiblingsNumber <= idx && idx <= currentTab + prerenderingSiblingsNumber;
|
60 | };
|
61 | _this.getOffsetIndex = function (current, width) {
|
62 | var threshold = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _this.props.distanceToChangeTab || 0;
|
63 |
|
64 | var ratio = Math.abs(current / width);
|
65 | var direction = ratio > _this.state.currentTab ? '<' : '>';
|
66 | var index = Math.floor(ratio);
|
67 | switch (direction) {
|
68 | case '<':
|
69 | return ratio - index > threshold ? index + 1 : index;
|
70 | case '>':
|
71 | return 1 - ratio + index > threshold ? index : index + 1;
|
72 | default:
|
73 | return Math.round(ratio);
|
74 | }
|
75 | };
|
76 | _this.getSubElements = function () {
|
77 | var children = _this.props.children;
|
78 |
|
79 | var subElements = {};
|
80 | return function () {
|
81 | var defaultPrefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '$i$-';
|
82 | var allPrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '$ALL$';
|
83 |
|
84 | if (Array.isArray(children)) {
|
85 | children.forEach(function (child, index) {
|
86 | if (child.key) {
|
87 | subElements[child.key] = child;
|
88 | }
|
89 | subElements['' + defaultPrefix + index] = child;
|
90 | });
|
91 | } else if (children) {
|
92 | subElements[allPrefix] = children;
|
93 | }
|
94 | return subElements;
|
95 | };
|
96 | };
|
97 | _this.state = {
|
98 | currentTab: _this.getTabIndex(props)
|
99 | };
|
100 | _this.nextCurrentTab = _this.state.currentTab;
|
101 | _this.instanceId = instanceId++;
|
102 | return _this;
|
103 | }
|
104 |
|
105 | (0, _createClass3['default'])(Tabs, [{
|
106 | key: 'getTabIndex',
|
107 | value: function getTabIndex(props) {
|
108 | var page = props.page,
|
109 | initialPage = props.initialPage,
|
110 | tabs = props.tabs;
|
111 |
|
112 | var param = (page !== undefined ? page : initialPage) || 0;
|
113 | var index = 0;
|
114 | if (typeof param === 'string') {
|
115 | tabs.forEach(function (t, i) {
|
116 | if (t.key === param) {
|
117 | index = i;
|
118 | }
|
119 | });
|
120 | } else {
|
121 | index = param || 0;
|
122 | }
|
123 | return index < 0 ? 0 : index;
|
124 | }
|
125 | }, {
|
126 | key: 'componentWillReceiveProps',
|
127 | value: function componentWillReceiveProps(nextProps) {
|
128 | if (this.props.page !== nextProps.page && nextProps.page !== undefined) {
|
129 | this.goToTab(this.getTabIndex(nextProps), true, {}, nextProps);
|
130 | }
|
131 | }
|
132 | }, {
|
133 | key: 'componentDidMount',
|
134 | value: function componentDidMount() {
|
135 | this.prevCurrentTab = this.state.currentTab;
|
136 | }
|
137 | }, {
|
138 | key: 'componentDidUpdate',
|
139 | value: function componentDidUpdate() {
|
140 | this.prevCurrentTab = this.state.currentTab;
|
141 | }
|
142 | }, {
|
143 | key: 'goToTab',
|
144 | value: function goToTab(index) {
|
145 | var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
146 | var newState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
147 | var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : this.props;
|
148 |
|
149 | if (!force && this.nextCurrentTab === index) {
|
150 | return false;
|
151 | }
|
152 | this.nextCurrentTab = index;
|
153 | var tabs = props.tabs,
|
154 | onChange = props.onChange;
|
155 |
|
156 | if (index >= 0 && index < tabs.length) {
|
157 | if (!force) {
|
158 | onChange && onChange(tabs[index], index);
|
159 | if (props.page !== undefined) {
|
160 | return false;
|
161 | }
|
162 | }
|
163 | this.setState((0, _extends3['default'])({ currentTab: index }, newState));
|
164 | }
|
165 | return true;
|
166 | }
|
167 | }, {
|
168 | key: 'tabClickGoToTab',
|
169 | value: function tabClickGoToTab(index) {
|
170 | this.goToTab(index);
|
171 | }
|
172 | }, {
|
173 | key: 'getTabBarBaseProps',
|
174 | value: function getTabBarBaseProps() {
|
175 | var currentTab = this.state.currentTab;
|
176 | var _props = this.props,
|
177 | animated = _props.animated,
|
178 | onTabClick = _props.onTabClick,
|
179 | tabBarActiveTextColor = _props.tabBarActiveTextColor,
|
180 | tabBarBackgroundColor = _props.tabBarBackgroundColor,
|
181 | tabBarInactiveTextColor = _props.tabBarInactiveTextColor,
|
182 | tabBarPosition = _props.tabBarPosition,
|
183 | tabBarTextStyle = _props.tabBarTextStyle,
|
184 | tabBarUnderlineStyle = _props.tabBarUnderlineStyle,
|
185 | tabs = _props.tabs;
|
186 |
|
187 | return {
|
188 | activeTab: currentTab,
|
189 | animated: !!animated,
|
190 | goToTab: this.tabClickGoToTab.bind(this),
|
191 | onTabClick: onTabClick,
|
192 | tabBarActiveTextColor: tabBarActiveTextColor,
|
193 | tabBarBackgroundColor: tabBarBackgroundColor,
|
194 | tabBarInactiveTextColor: tabBarInactiveTextColor,
|
195 | tabBarPosition: tabBarPosition,
|
196 | tabBarTextStyle: tabBarTextStyle,
|
197 | tabBarUnderlineStyle: tabBarUnderlineStyle,
|
198 | tabs: tabs,
|
199 | instanceId: this.instanceId
|
200 | };
|
201 | }
|
202 | }, {
|
203 | key: 'renderTabBar',
|
204 | value: function renderTabBar(tabBarProps, DefaultTabBar) {
|
205 | var renderTabBar = this.props.renderTabBar;
|
206 |
|
207 | if (renderTabBar === false) {
|
208 | return null;
|
209 | } else if (renderTabBar) {
|
210 |
|
211 | return renderTabBar(tabBarProps);
|
212 | } else {
|
213 | return _react2['default'].createElement(DefaultTabBar, tabBarProps);
|
214 | }
|
215 | }
|
216 | }, {
|
217 | key: 'getSubElement',
|
218 | value: function getSubElement(tab, index, subElements) {
|
219 | var defaultPrefix = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '$i$-';
|
220 | var allPrefix = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '$ALL$';
|
221 |
|
222 | var key = tab.key || '' + defaultPrefix + index;
|
223 | var elements = subElements(defaultPrefix, allPrefix);
|
224 | var component = elements[key] || elements[allPrefix];
|
225 | if (component instanceof Function) {
|
226 | component = component(tab, index);
|
227 | }
|
228 | return component || null;
|
229 | }
|
230 | }]);
|
231 | return Tabs;
|
232 | }(_react2['default'].PureComponent);
|
233 |
|
234 | Tabs.defaultProps = {
|
235 | tabBarPosition: 'top',
|
236 | initialPage: 0,
|
237 | swipeable: true,
|
238 | animated: true,
|
239 | prerenderingSiblingsNumber: 1,
|
240 | tabs: [],
|
241 | destroyInactiveTab: false,
|
242 | usePaged: true,
|
243 | tabDirection: 'horizontal',
|
244 | distanceToChangeTab: .3
|
245 | }; |
\ | No newline at end of file |