2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../../_util/vue-types';
|
2020-03-07 11:45:13 +00:00
|
|
|
import {
|
|
|
|
setTransform,
|
|
|
|
isTransform3dSupported,
|
|
|
|
getLeft,
|
|
|
|
getStyle,
|
|
|
|
getTop,
|
|
|
|
getActiveIndex,
|
|
|
|
} from './utils';
|
2019-01-12 03:33:27 +00:00
|
|
|
import BaseMixin from '../../_util/BaseMixin';
|
2017-11-30 11:11:42 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function componentDidUpdate(component, init) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { styles = {}, panels, activeKey, direction } = component.$props;
|
2019-01-12 03:33:27 +00:00
|
|
|
const rootNode = component.getRef('root');
|
|
|
|
const wrapNode = component.getRef('nav') || rootNode;
|
|
|
|
const inkBarNode = component.getRef('inkBar');
|
|
|
|
const activeTab = component.getRef('activeTab');
|
|
|
|
const inkBarNodeStyle = inkBarNode.style;
|
|
|
|
const tabBarPosition = component.$props.tabBarPosition;
|
|
|
|
const activeIndex = getActiveIndex(panels, activeKey);
|
2017-11-30 11:11:42 +00:00
|
|
|
if (init) {
|
|
|
|
// prevent mount animation
|
2019-01-12 03:33:27 +00:00
|
|
|
inkBarNodeStyle.display = 'none';
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
|
|
|
if (activeTab) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const tabNode = activeTab;
|
2020-03-07 11:45:13 +00:00
|
|
|
const transformSupported = isTransform3dSupported(inkBarNodeStyle);
|
2018-12-14 15:31:02 +00:00
|
|
|
|
|
|
|
// Reset current style
|
2019-01-12 03:33:27 +00:00
|
|
|
setTransform(inkBarNodeStyle, '');
|
|
|
|
inkBarNodeStyle.width = '';
|
|
|
|
inkBarNodeStyle.height = '';
|
|
|
|
inkBarNodeStyle.left = '';
|
|
|
|
inkBarNodeStyle.top = '';
|
|
|
|
inkBarNodeStyle.bottom = '';
|
|
|
|
inkBarNodeStyle.right = '';
|
2018-12-14 15:31:02 +00:00
|
|
|
|
2017-11-30 11:11:42 +00:00
|
|
|
if (tabBarPosition === 'top' || tabBarPosition === 'bottom') {
|
2019-01-12 03:33:27 +00:00
|
|
|
let left = getLeft(tabNode, wrapNode);
|
|
|
|
let width = tabNode.offsetWidth;
|
2017-11-30 11:11:42 +00:00
|
|
|
// If tabNode'width width equal to wrapNode'width when tabBarPosition is top or bottom
|
|
|
|
// It means no css working, then ink bar should not have width until css is loaded
|
|
|
|
// Fix https://github.com/ant-design/ant-design/issues/7564
|
2018-04-07 10:52:02 +00:00
|
|
|
if (width === rootNode.offsetWidth) {
|
2019-01-12 03:33:27 +00:00
|
|
|
width = 0;
|
2017-11-30 11:11:42 +00:00
|
|
|
} else if (styles.inkBar && styles.inkBar.width !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
width = parseFloat(styles.inkBar.width, 10);
|
2017-11-30 11:11:42 +00:00
|
|
|
if (width) {
|
2019-01-12 03:33:27 +00:00
|
|
|
left += (tabNode.offsetWidth - width) / 2;
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
if (direction === 'rtl') {
|
|
|
|
left = getStyle(tabNode, 'margin-left') - left;
|
|
|
|
}
|
2017-11-30 11:11:42 +00:00
|
|
|
// use 3d gpu to optimize render
|
|
|
|
if (transformSupported) {
|
2019-01-12 03:33:27 +00:00
|
|
|
setTransform(inkBarNodeStyle, `translate3d(${left}px,0,0)`);
|
2017-11-30 11:11:42 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
inkBarNodeStyle.left = `${left}px`;
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
inkBarNodeStyle.width = `${width}px`;
|
2017-11-30 11:11:42 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
let top = getTop(tabNode, wrapNode, true);
|
|
|
|
let height = tabNode.offsetHeight;
|
2017-11-30 11:11:42 +00:00
|
|
|
if (styles.inkBar && styles.inkBar.height !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
height = parseFloat(styles.inkBar.height, 10);
|
2017-11-30 11:11:42 +00:00
|
|
|
if (height) {
|
2019-01-12 03:33:27 +00:00
|
|
|
top += (tabNode.offsetHeight - height) / 2;
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (transformSupported) {
|
2019-01-12 03:33:27 +00:00
|
|
|
setTransform(inkBarNodeStyle, `translate3d(0,${top}px,0)`);
|
|
|
|
inkBarNodeStyle.top = '0';
|
2017-11-30 11:11:42 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
inkBarNodeStyle.top = `${top}px`;
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
inkBarNodeStyle.height = `${height}px`;
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
inkBarNodeStyle.display = activeIndex !== -1 ? 'block' : 'none';
|
2017-11-30 11:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2018-09-05 13:28:54 +00:00
|
|
|
name: 'InkTabBarNode',
|
|
|
|
mixins: [BaseMixin],
|
2020-06-24 08:07:03 +00:00
|
|
|
inheritAttrs: false,
|
2017-11-30 11:11:42 +00:00
|
|
|
props: {
|
|
|
|
inkBarAnimated: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
direction: PropTypes.string,
|
2017-11-30 11:11:42 +00:00
|
|
|
prefixCls: String,
|
|
|
|
styles: Object,
|
2018-09-05 13:28:54 +00:00
|
|
|
tabBarPosition: String,
|
|
|
|
saveRef: PropTypes.func.def(() => {}),
|
|
|
|
getRef: PropTypes.func.def(() => {}),
|
2018-12-14 15:31:02 +00:00
|
|
|
panels: PropTypes.array,
|
2019-01-12 03:33:27 +00:00
|
|
|
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
updated() {
|
2020-06-24 08:07:03 +00:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
componentDidUpdate(this);
|
|
|
|
});
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2020-06-24 08:07:03 +00:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
componentDidUpdate(this, true);
|
|
|
|
});
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
|
|
|
const { prefixCls, styles = {}, inkBarAnimated } = this;
|
|
|
|
const className = `${prefixCls}-ink-bar`;
|
2018-09-05 13:28:54 +00:00
|
|
|
const classes = {
|
|
|
|
[className]: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
[inkBarAnimated ? `${className}-animated` : `${className}-no-animated`]: true,
|
|
|
|
};
|
2020-06-24 15:45:36 +00:00
|
|
|
return <div style={styles.inkBar} class={classes} key="inkBar" ref={this.saveRef('inkBar')} />;
|
2017-11-30 11:11:42 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|