UNPKG

8.26 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _react = require('react');
8
9var _react2 = _interopRequireDefault(_react);
10
11var _propTypes = require('prop-types');
12
13var _propTypes2 = _interopRequireDefault(_propTypes);
14
15var _classnames2 = require('classnames');
16
17var _classnames3 = _interopRequireDefault(_classnames2);
18
19var _utils = require('./utils');
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
22
23function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
24
25function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
30
31function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); } /**
32 * This source code is quoted from rc-tabs.
33 * homepage: https://github.com/react-component/tabs
34 */
35
36
37function _componentDidUpdate(component, init) {
38 var _component$props = component.props,
39 styles = _component$props.styles,
40 panels = _component$props.panels,
41 activeKey = _component$props.activeKey,
42 direction = _component$props.direction;
43
44 var rootNode = component.props.getRef('root');
45 var wrapNode = component.props.getRef('nav') || rootNode;
46 var inkBarNode = component.props.getRef('inkBar');
47 var activeTab = component.props.getRef('activeTab');
48 var inkBarNodeStyle = inkBarNode.style;
49 var tabBarPosition = component.props.tabBarPosition;
50 var activeIndex = (0, _utils.getActiveIndex)(panels, activeKey);
51 if (init) {
52 // prevent mount animation
53 inkBarNodeStyle.display = 'none';
54 }
55 if (activeTab) {
56 var tabNode = activeTab;
57 var transformSupported = (0, _utils.isTransformSupported)(inkBarNodeStyle);
58
59 // Reset current style
60 (0, _utils.setTransform)(inkBarNodeStyle, '');
61 inkBarNodeStyle.width = '';
62 inkBarNodeStyle.height = '';
63 inkBarNodeStyle.left = '';
64 inkBarNodeStyle.top = '';
65 inkBarNodeStyle.bottom = '';
66 inkBarNodeStyle.right = '';
67
68 if (tabBarPosition === 'top' || tabBarPosition === 'bottom') {
69 var left = (0, _utils.getLeft)(tabNode, wrapNode);
70 var width = tabNode.offsetWidth;
71
72 // If tabNode'width width equal to wrapNode'width when tabBarPosition is top or bottom
73 // It means no css working, then ink bar should not have width until css is loaded
74 // Fix https://github.com/ant-design/ant-design/issues/7564
75 if (width === rootNode.offsetWidth) {
76 width = 0;
77 } else if (styles.inkBar && styles.inkBar.width !== undefined) {
78 width = parseFloat(styles.inkBar.width, 10);
79 if (width) {
80 left += (tabNode.offsetWidth - width) / 2;
81 }
82 }
83 if (direction === 'rtl') {
84 left = (0, _utils.getStyle)(tabNode, 'margin-left') - left;
85 }
86 // use 3d gpu to optimize render
87 if (transformSupported) {
88 (0, _utils.setTransform)(inkBarNodeStyle, 'translate3d(' + left + 'px,0,0)');
89 } else {
90 inkBarNodeStyle.left = left + 'px';
91 }
92 inkBarNodeStyle.width = width + 'px';
93 } else {
94 var top = (0, _utils.getTop)(tabNode, wrapNode, true);
95 var height = tabNode.offsetHeight;
96 if (styles.inkBar && styles.inkBar.height !== undefined) {
97 height = parseFloat(styles.inkBar.height, 10);
98 if (height) {
99 top += (tabNode.offsetHeight - height) / 2;
100 }
101 }
102 if (transformSupported) {
103 (0, _utils.setTransform)(inkBarNodeStyle, 'translate3d(0,' + top + 'px,0)');
104 inkBarNodeStyle.top = '0';
105 } else {
106 inkBarNodeStyle.top = top + 'px';
107 }
108 inkBarNodeStyle.height = height + 'px';
109 }
110 }
111 inkBarNodeStyle.display = activeIndex !== -1 ? 'block' : 'none';
112}
113
114var InkTabBarNode = function (_React$Component) {
115 _inherits(InkTabBarNode, _React$Component);
116
117 function InkTabBarNode() {
118 _classCallCheck(this, InkTabBarNode);
119
120 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
121 }
122
123 InkTabBarNode.prototype.componentDidMount = function componentDidMount() {
124 var _this2 = this;
125
126 this.timeout = setTimeout(function () {
127 _componentDidUpdate(_this2, true);
128 }, 0);
129 };
130
131 InkTabBarNode.prototype.componentDidUpdate = function componentDidUpdate() {
132 _componentDidUpdate(this);
133 };
134
135 InkTabBarNode.prototype.componentWillUnmount = function componentWillUnmount() {
136 clearTimeout(this.timeout);
137 };
138
139 InkTabBarNode.prototype.render = function render() {
140 var _classnames;
141
142 var _props = this.props,
143 clsPrefix = _props.clsPrefix,
144 styles = _props.styles,
145 inkBarAnimated = _props.inkBarAnimated;
146
147 var className = clsPrefix + '-ink-bar';
148 var classes = (0, _classnames3["default"])((_classnames = {}, _defineProperty(_classnames, className, true), _defineProperty(_classnames, inkBarAnimated ? className + '-animated' : className + '-no-animated', true), _classnames));
149 return _react2["default"].createElement('div', {
150 style: styles.inkBar,
151 className: classes,
152 key: 'inkBar',
153 ref: this.props.saveRef('inkBar')
154 });
155 };
156
157 return InkTabBarNode;
158}(_react2["default"].Component);
159
160exports["default"] = InkTabBarNode;
161
162
163InkTabBarNode.propTypes = {
164 clsPrefix: _propTypes2["default"].string,
165 styles: _propTypes2["default"].object,
166 inkBarAnimated: _propTypes2["default"].bool,
167 saveRef: _propTypes2["default"].func,
168 direction: _propTypes2["default"].string
169};
170
171InkTabBarNode.defaultProps = {
172 clsPrefix: '',
173 inkBarAnimated: true,
174 styles: {},
175 saveRef: function saveRef() {}
176};
177module.exports = exports['default'];
\No newline at end of file