Skip to content

Commit 4770ed4

Browse files
committed
perf: update tabs
1 parent 5e3d9fe commit 4770ed4

File tree

3 files changed

+38
-39
lines changed

3 files changed

+38
-39
lines changed

components/tabs/__tests__/__snapshots__/demo.test.js.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ exports[`renders ./antdv-demo/docs/tabs/demo/card-top.md correctly 1`] = `
124124
exports[`renders ./antdv-demo/docs/tabs/demo/custom-add-trigger.md correctly 1`] = `
125125
<div>
126126
<div style="margin-bottom: 16px;"><button class="ant-btn" type="button">
127-
<!----><span>ADD</span></button></div>
127+
<!----><span>ADD</span>
128+
</button></div>
128129
<div class="ant-tabs-card ant-tabs-editable-card ant-tabs-no-animation ant-tabs ant-tabs-top">
129130
<div role="tablist" class="ant-tabs-bar ant-tabs-top-bar ant-tabs-card-bar" tabindex="0">
130131
<div class="ant-tabs-nav-container"><span unselectable="unselectable" class="ant-tabs-tab-prev ant-tabs-tab-btn-disabled"><span class="ant-tabs-tab-prev-icon"><span role="img" aria-label="left" class="anticon anticon-left ant-tabs-tab-prev-icon-target"><svg class="" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 000 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></span></span></span><span unselectable="unselectable" class="ant-tabs-tab-next ant-tabs-tab-btn-disabled"><span class="ant-tabs-tab-next-icon"><span role="img" aria-label="right" class="anticon anticon-right ant-tabs-tab-next-icon-target"><svg class="" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg></span></span></span>
@@ -242,7 +243,8 @@ exports[`renders ./antdv-demo/docs/tabs/demo/editable-card.md correctly 1`] = `
242243
<div class="ant-tabs-card ant-tabs-editable-card ant-tabs-no-animation ant-tabs ant-tabs-top">
243244
<div role="tablist" class="ant-tabs-bar ant-tabs-top-bar ant-tabs-card-bar" tabindex="0">
244245
<div class="ant-tabs-extra-content" style="float: right;"><span><span tabindex="-1" role="img" aria-label="plus" class="anticon anticon-plus ant-tabs-new-tab"><svg class="" data-icon="plus" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><defs><style></style></defs><path d="M482 152h60q8 0 8 8v704q0 8-8 8h-60q-8 0-8-8V160q0-8 8-8z"></path><path d="M176 474h672q8 0 8 8v60q0 8-8 8H176q-8 0-8-8v-60q0-8 8-8z"></path></svg></span>
245-
<!----></span></div>
246+
<!----></span>
247+
</div>
246248
<div class="ant-tabs-nav-container"><span unselectable="unselectable" class="ant-tabs-tab-prev ant-tabs-tab-btn-disabled"><span class="ant-tabs-tab-prev-icon"><span role="img" aria-label="left" class="anticon anticon-left ant-tabs-tab-prev-icon-target"><svg class="" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 000 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></span></span></span><span unselectable="unselectable" class="ant-tabs-tab-next ant-tabs-tab-btn-disabled"><span class="ant-tabs-tab-next-icon"><span role="img" aria-label="right" class="anticon anticon-right ant-tabs-tab-next-icon-target"><svg class="" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg></span></span></span>
247249
<div class="ant-tabs-nav-wrap">
248250
<div class="ant-tabs-nav-scroll">
@@ -290,7 +292,8 @@ exports[`renders ./antdv-demo/docs/tabs/demo/extra.md correctly 1`] = `
290292
<div class="ant-tabs-line ant-tabs ant-tabs-top">
291293
<div role="tablist" class="ant-tabs-bar ant-tabs-top-bar" tabindex="0">
292294
<div class="ant-tabs-extra-content" style="float: right;"><button class="ant-btn" type="button">
293-
<!----><span>Extra Action</span></button></div>
295+
<!----><span>Extra Action</span>
296+
</button></div>
294297
<div class="ant-tabs-nav-container"><span unselectable="unselectable" class="ant-tabs-tab-prev ant-tabs-tab-btn-disabled"><span class="ant-tabs-tab-prev-icon"><span role="img" aria-label="left" class="anticon anticon-left ant-tabs-tab-prev-icon-target"><svg class="" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 000 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></span></span></span><span unselectable="unselectable" class="ant-tabs-tab-next ant-tabs-tab-btn-disabled"><span class="ant-tabs-tab-next-icon"><span role="img" aria-label="right" class="anticon anticon-right ant-tabs-tab-next-icon-target"><svg class="" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896" focusable="false"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg></span></span></span>
295298
<div class="ant-tabs-nav-wrap">
296299
<div class="ant-tabs-nav-scroll">

components/tabs/tabs.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,6 @@ export default {
172172
onChange: this.handleChange,
173173
class: cls,
174174
};
175-
return <VcTabs {...tabsProps} __propsSymbol__={Symbol()} />;
175+
return <VcTabs {...tabsProps} />;
176176
},
177177
};

components/vc-tabs/src/Tabs.jsx

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { provide } from 'vue';
1+
import { provide, reactive, watchEffect } from 'vue';
22
import BaseMixin from '../../_util/BaseMixin';
33
import PropTypes from '../../_util/vue-types';
44
import raf from 'raf';
55
import KeyCode from './KeyCode';
6-
import { getOptionProps } from '../../_util/props-util';
76
import { cloneElement } from '../../_util/vnode';
87
import Sentinel from './Sentinel';
98
import isValid from '../../_util/isValid';
@@ -40,43 +39,42 @@ export default {
4039
tabBarPosition: PropTypes.string.def('top'),
4140
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
4241
defaultActiveKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
43-
__propsSymbol__: PropTypes.any,
4442
direction: PropTypes.string.def('ltr'),
4543
tabBarGutter: PropTypes.number,
4644
},
47-
data() {
48-
provide('sentinelContext', this);
49-
const props = getOptionProps(this);
45+
setup(props) {
5046
let activeKey;
51-
if ('activeKey' in props) {
47+
if (props.activeKey !== undefined) {
5248
activeKey = props.activeKey;
53-
} else if ('defaultActiveKey' in props) {
49+
} else if (props.defaultActiveKey !== undefined) {
5450
activeKey = props.defaultActiveKey;
5551
} else {
5652
activeKey = getDefaultActiveKey(props);
5753
}
54+
const state = reactive({
55+
_activeKey: activeKey,
56+
});
57+
watchEffect(
58+
() => {
59+
if (props.activeKey !== undefined) {
60+
state._activeKey = props.activeKey;
61+
} else if (!activeKeyIsValid(props, state._activeKey)) {
62+
// https://github.com/ant-design/ant-design/issues/7093
63+
state._activeKey = getDefaultActiveKey(props);
64+
}
65+
},
66+
{
67+
flush: 'sync',
68+
},
69+
);
70+
return { state };
71+
},
72+
created() {
5873
this.panelSentinelStart = undefined;
5974
this.panelSentinelEnd = undefined;
6075
this.sentinelStart = undefined;
6176
this.sentinelEnd = undefined;
62-
return {
63-
_activeKey: activeKey,
64-
};
65-
},
66-
watch: {
67-
__propsSymbol__() {
68-
const nextProps = getOptionProps(this);
69-
if ('activeKey' in nextProps) {
70-
this.setState({
71-
_activeKey: nextProps.activeKey,
72-
});
73-
} else if (!activeKeyIsValid(nextProps, this.$data._activeKey)) {
74-
// https://github.com/ant-design/ant-design/issues/7093
75-
this.setState({
76-
_activeKey: getDefaultActiveKey(nextProps),
77-
});
78-
}
79-
},
77+
provide('sentinelContext', this);
8078
},
8179
beforeUnmount() {
8280
this.destroy = true;
@@ -133,20 +131,18 @@ export default {
133131
},
134132

135133
setActiveKey(activeKey) {
136-
if (this.$data._activeKey !== activeKey) {
137-
const props = getOptionProps(this);
138-
if (!('activeKey' in props)) {
139-
this.setState({
140-
_activeKey: activeKey,
141-
});
134+
if (this.state._activeKey !== activeKey) {
135+
const props = this.$props;
136+
if (props.activeKey === undefined) {
137+
this.state._activeKey = activeKey;
142138
}
143139
this.__emit('update:activeKey', activeKey);
144140
this.__emit('change', activeKey);
145141
}
146142
},
147143

148144
getNextActiveKey(next) {
149-
const activeKey = this.$data._activeKey;
145+
const activeKey = this.state._activeKey;
150146
const children = [];
151147
this.$props.children.forEach(c => {
152148
if (c && !c.disabled && c.disabled !== '') {
@@ -206,7 +202,7 @@ export default {
206202
navWrapper,
207203
tabBarPosition,
208204
panels: props.children,
209-
activeKey: this.$data._activeKey,
205+
activeKey: this.state._activeKey,
210206
direction,
211207
tabBarGutter,
212208
onKeydown: this.onNavKeyDown,
@@ -216,7 +212,7 @@ export default {
216212
const tabContent = cloneElement(renderTabContent(), {
217213
prefixCls,
218214
tabBarPosition,
219-
activeKey: this.$data._activeKey,
215+
activeKey: this.state._activeKey,
220216
destroyInactiveTabPane,
221217
direction,
222218
onChange: this.setActiveKey,

0 commit comments

Comments
 (0)