refactor: tabs to ts

pull/2992/head
Amour1688 2020-10-13 17:46:31 +08:00
parent c2994a6629
commit dc4f2b2dcb
3 changed files with 41 additions and 24 deletions

View File

@ -1,22 +1,27 @@
import { defineComponent, PropType } from 'vue';
import { tuple } from '../_util/type';
import UpOutlined from '@ant-design/icons-vue/UpOutlined'; import UpOutlined from '@ant-design/icons-vue/UpOutlined';
import DownOutlined from '@ant-design/icons-vue/DownOutlined'; import DownOutlined from '@ant-design/icons-vue/DownOutlined';
import LeftOutlined from '@ant-design/icons-vue/LeftOutlined'; import LeftOutlined from '@ant-design/icons-vue/LeftOutlined';
import RightOutlined from '@ant-design/icons-vue/RightOutlined'; import RightOutlined from '@ant-design/icons-vue/RightOutlined';
import ScrollableInkTabBar from '../vc-tabs/src/ScrollableInkTabBar'; import ScrollableInkTabBar from '../vc-tabs/src/ScrollableInkTabBar';
import PropTypes, { withUndefined } from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
const TabBar = { const TabBar = defineComponent({
name: 'TabBar', name: 'TabBar',
inheritAttrs: false, inheritAttrs: false,
props: { props: {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
tabBarStyle: PropTypes.object, tabBarStyle: PropTypes.style,
tabBarExtraContent: PropTypes.any, tabBarExtraContent: PropTypes.VNodeChild,
type: PropTypes.oneOf(['line', 'card', 'editable-card']), type: PropTypes.oneOf(tuple('line', 'card', 'editable-card')),
tabPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('top'), tabPosition: PropTypes.oneOf(tuple('top', 'right', 'bottom', 'left')).def('top'),
tabBarPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']), tabBarPosition: PropTypes.oneOf(tuple('top', 'right', 'bottom', 'left')),
size: PropTypes.oneOf(['default', 'small', 'large']), size: PropTypes.oneOf(tuple('default', 'small', 'large')),
animated: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])), animated: {
type: [Boolean, Object] as PropType<boolean | { inkBar: boolean; tabPane: boolean }>,
default: undefined,
},
renderTabBar: PropTypes.func, renderTabBar: PropTypes.func,
panels: PropTypes.array.def([]), panels: PropTypes.array.def([]),
activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), activeKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
@ -56,7 +61,7 @@ const TabBar = {
); );
// Additional className for style usage // Additional className for style usage
const cls = { const cls = {
[this.$attrs.class]: this.$attrs.class, [this.$attrs.class as string]: this.$attrs.class,
[`${prefixCls}-${tabPosition}-bar`]: true, [`${prefixCls}-${tabPosition}-bar`]: true,
[`${prefixCls}-${size}-bar`]: !!size, [`${prefixCls}-${size}-bar`]: !!size,
[`${prefixCls}-card-bar`]: type && type.indexOf('card') >= 0, [`${prefixCls}-card-bar`]: type && type.indexOf('card') >= 0,
@ -80,6 +85,6 @@ const TabBar = {
return <ScrollableInkTabBar {...renderProps} />; return <ScrollableInkTabBar {...renderProps} />;
} }
}, },
}; });
export default TabBar; export default TabBar;

View File

@ -1,3 +1,4 @@
import { App } from 'vue';
import Tabs from './tabs'; import Tabs from './tabs';
import TabPane from '../vc-tabs/src/TabPane'; import TabPane from '../vc-tabs/src/TabPane';
import TabContent from '../vc-tabs/src/TabContent'; import TabContent from '../vc-tabs/src/TabContent';
@ -6,7 +7,7 @@ Tabs.TabPane = { ...TabPane, name: 'ATabPane', __ANT_TAB_PANE: true };
Tabs.TabContent = { ...TabContent, name: 'ATabContent' }; Tabs.TabContent = { ...TabContent, name: 'ATabContent' };
/* istanbul ignore next */ /* istanbul ignore next */
Tabs.install = function(app) { Tabs.install = function(app: App) {
app.component(Tabs.name, Tabs); app.component(Tabs.name, Tabs);
app.component(Tabs.TabPane.name, Tabs.TabPane); app.component(Tabs.TabPane.name, Tabs.TabPane);
app.component(Tabs.TabContent.name, Tabs.TabContent); app.component(Tabs.TabContent.name, Tabs.TabContent);

View File

@ -1,4 +1,5 @@
import { defineComponent, inject } from 'vue'; import { defineComponent, inject, PropType } from 'vue';
import { tuple } from '../_util/type';
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined'; import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
import PlusOutlined from '@ant-design/icons-vue/PlusOutlined'; import PlusOutlined from '@ant-design/icons-vue/PlusOutlined';
import VcTabs, { TabPane } from '../vc-tabs/src'; import VcTabs, { TabPane } from '../vc-tabs/src';
@ -30,19 +31,29 @@ export default defineComponent({
tabBarStyle: PropTypes.object, tabBarStyle: PropTypes.object,
tabBarExtraContent: PropTypes.any, tabBarExtraContent: PropTypes.any,
destroyInactiveTabPane: PropTypes.looseBool.def(false), destroyInactiveTabPane: PropTypes.looseBool.def(false),
type: PropTypes.oneOf(['line', 'card', 'editable-card']), type: PropTypes.oneOf(tuple('line', 'card', 'editable-card')),
tabPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('top'), tabPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).def('top'),
size: PropTypes.oneOf(['default', 'small', 'large']), size: PropTypes.oneOf(['default', 'small', 'large']),
animated: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])), animated: withUndefined(PropTypes.oneOfType([PropTypes.looseBool, PropTypes.object])),
tabBarGutter: PropTypes.number, tabBarGutter: PropTypes.number,
renderTabBar: PropTypes.func, renderTabBar: PropTypes.func,
onChange: PropTypes.func, onChange: {
type: Function as PropType<(activeKey: string) => void>,
},
onTabClick: PropTypes.func, onTabClick: PropTypes.func,
onPrevClick: PropTypes.func, onPrevClick: {
onNextClick: PropTypes.func, type: Function as PropType<(e: MouseEvent) => void>,
onEdit: PropTypes.func, },
'onUpdate:activeKey': PropTypes.func, onNextClick: {
type: Function as PropType<(e: MouseEvent) => void>,
},
onEdit: {
type: Function as PropType<
(targetKey: string | MouseEvent, action: 'add' | 'remove') => void
>,
},
}, },
emits: ['update:activeKey', 'edit', 'change'],
setup() { setup() {
return { return {
configProvider: inject('configProvider', defaultConfigProvider), configProvider: inject('configProvider', defaultConfigProvider),
@ -56,17 +67,17 @@ export default defineComponent({
} }
}, },
methods: { methods: {
removeTab(targetKey, e) { removeTab(targetKey: string, e: MouseEvent) {
e.stopPropagation(); e.stopPropagation();
if (isValid(targetKey)) { if (isValid(targetKey)) {
this.$emit('edit', targetKey, 'remove'); this.$emit('edit', targetKey, 'remove');
} }
}, },
handleChange(activeKey) { handleChange(activeKey: string) {
this.$emit('update:activeKey', activeKey); this.$emit('update:activeKey', activeKey);
this.$emit('change', activeKey); this.$emit('change', activeKey);
}, },
createNewTab(targetKey) { createNewTab(targetKey: MouseEvent) {
this.$emit('edit', targetKey, 'add'); this.$emit('edit', targetKey, 'add');
}, },
}, },
@ -95,7 +106,7 @@ export default defineComponent({
tabPaneAnimated = 'animated' in props ? tabPaneAnimated : false; tabPaneAnimated = 'animated' in props ? tabPaneAnimated : false;
} }
const cls = { const cls = {
[className]: className, [className as string]: className,
[`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right', [`${prefixCls}-vertical`]: tabPosition === 'left' || tabPosition === 'right',
[`${prefixCls}-${size}`]: !!size, [`${prefixCls}-${size}`]: !!size,
[`${prefixCls}-card`]: type.indexOf('card') >= 0, [`${prefixCls}-card`]: type.indexOf('card') >= 0,
@ -107,7 +118,7 @@ export default defineComponent({
if (type === 'editable-card') { if (type === 'editable-card') {
childrenWithClose = []; childrenWithClose = [];
children.forEach((child, index) => { children.forEach((child, index) => {
const props = getPropsData(child); const props = getPropsData(child) as any;
let closable = props.closable; let closable = props.closable;
closable = typeof closable === 'undefined' ? true : closable; closable = typeof closable === 'undefined' ? true : closable;
const closeIcon = closable ? ( const closeIcon = closable ? (