From 4770ed4708bd0aef1318129f1f8d1f622aeb68e2 Mon Sep 17 00:00:00 2001 From: tanjinzhou <415800467@qq.com> Date: Thu, 27 Aug 2020 17:28:18 +0800 Subject: [PATCH] perf: update tabs --- .../__tests__/__snapshots__/demo.test.js.snap | 9 ++- components/tabs/tabs.jsx | 2 +- components/vc-tabs/src/Tabs.jsx | 66 +++++++++---------- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/components/tabs/__tests__/__snapshots__/demo.test.js.snap b/components/tabs/__tests__/__snapshots__/demo.test.js.snap index b479497c2..b1f4b3018 100644 --- a/components/tabs/__tests__/__snapshots__/demo.test.js.snap +++ b/components/tabs/__tests__/__snapshots__/demo.test.js.snap @@ -124,7 +124,8 @@ exports[`renders ./antdv-demo/docs/tabs/demo/card-top.md correctly 1`] = ` exports[`renders ./antdv-demo/docs/tabs/demo/custom-add-trigger.md correctly 1`] = `
+ ADD +
@@ -242,7 +243,8 @@ exports[`renders ./antdv-demo/docs/tabs/demo/editable-card.md correctly 1`] = `
-
+ +
@@ -290,7 +292,8 @@ exports[`renders ./antdv-demo/docs/tabs/demo/extra.md correctly 1`] = `
+ Extra Action +
diff --git a/components/tabs/tabs.jsx b/components/tabs/tabs.jsx index 24963b25c..47a78a840 100644 --- a/components/tabs/tabs.jsx +++ b/components/tabs/tabs.jsx @@ -172,6 +172,6 @@ export default { onChange: this.handleChange, class: cls, }; - return ; + return ; }, }; diff --git a/components/vc-tabs/src/Tabs.jsx b/components/vc-tabs/src/Tabs.jsx index 9789179fe..31b5d12dd 100644 --- a/components/vc-tabs/src/Tabs.jsx +++ b/components/vc-tabs/src/Tabs.jsx @@ -1,9 +1,8 @@ -import { provide } from 'vue'; +import { provide, reactive, watchEffect } from 'vue'; import BaseMixin from '../../_util/BaseMixin'; import PropTypes from '../../_util/vue-types'; import raf from 'raf'; import KeyCode from './KeyCode'; -import { getOptionProps } from '../../_util/props-util'; import { cloneElement } from '../../_util/vnode'; import Sentinel from './Sentinel'; import isValid from '../../_util/isValid'; @@ -40,43 +39,42 @@ export default { tabBarPosition: PropTypes.string.def('top'), activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), - __propsSymbol__: PropTypes.any, direction: PropTypes.string.def('ltr'), tabBarGutter: PropTypes.number, }, - data() { - provide('sentinelContext', this); - const props = getOptionProps(this); + setup(props) { let activeKey; - if ('activeKey' in props) { + if (props.activeKey !== undefined) { activeKey = props.activeKey; - } else if ('defaultActiveKey' in props) { + } else if (props.defaultActiveKey !== undefined) { activeKey = props.defaultActiveKey; } else { activeKey = getDefaultActiveKey(props); } + const state = reactive({ + _activeKey: activeKey, + }); + watchEffect( + () => { + if (props.activeKey !== undefined) { + state._activeKey = props.activeKey; + } else if (!activeKeyIsValid(props, state._activeKey)) { + // https://github.com/ant-design/ant-design/issues/7093 + state._activeKey = getDefaultActiveKey(props); + } + }, + { + flush: 'sync', + }, + ); + return { state }; + }, + created() { this.panelSentinelStart = undefined; this.panelSentinelEnd = undefined; this.sentinelStart = undefined; this.sentinelEnd = undefined; - return { - _activeKey: activeKey, - }; - }, - watch: { - __propsSymbol__() { - const nextProps = getOptionProps(this); - if ('activeKey' in nextProps) { - this.setState({ - _activeKey: nextProps.activeKey, - }); - } else if (!activeKeyIsValid(nextProps, this.$data._activeKey)) { - // https://github.com/ant-design/ant-design/issues/7093 - this.setState({ - _activeKey: getDefaultActiveKey(nextProps), - }); - } - }, + provide('sentinelContext', this); }, beforeUnmount() { this.destroy = true; @@ -133,12 +131,10 @@ export default { }, setActiveKey(activeKey) { - if (this.$data._activeKey !== activeKey) { - const props = getOptionProps(this); - if (!('activeKey' in props)) { - this.setState({ - _activeKey: activeKey, - }); + if (this.state._activeKey !== activeKey) { + const props = this.$props; + if (props.activeKey === undefined) { + this.state._activeKey = activeKey; } this.__emit('update:activeKey', activeKey); this.__emit('change', activeKey); @@ -146,7 +142,7 @@ export default { }, getNextActiveKey(next) { - const activeKey = this.$data._activeKey; + const activeKey = this.state._activeKey; const children = []; this.$props.children.forEach(c => { if (c && !c.disabled && c.disabled !== '') { @@ -206,7 +202,7 @@ export default { navWrapper, tabBarPosition, panels: props.children, - activeKey: this.$data._activeKey, + activeKey: this.state._activeKey, direction, tabBarGutter, onKeydown: this.onNavKeyDown, @@ -216,7 +212,7 @@ export default { const tabContent = cloneElement(renderTabContent(), { prefixCls, tabBarPosition, - activeKey: this.$data._activeKey, + activeKey: this.state._activeKey, destroyInactiveTabPane, direction, onChange: this.setActiveKey,