refactor: card to ts

pull/2992/head
Amour1688 2020-10-12 19:19:10 +08:00
parent 712f2b1599
commit 3f31590ad4
6 changed files with 64 additions and 34 deletions

View File

@ -1,4 +1,5 @@
import { inject, isVNode } from 'vue'; import { inject, isVNode, defineComponent, VNodeTypes, PropType, VNode } from 'vue';
import { tuple } from '../_util/type';
import Tabs from '../tabs'; import Tabs from '../tabs';
import Row from '../row'; import Row from '../row';
import Col from '../col'; import Col from '../col';
@ -8,28 +9,47 @@ import BaseMixin from '../_util/BaseMixin';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
import isPlainObject from 'lodash-es/isPlainObject'; import isPlainObject from 'lodash-es/isPlainObject';
export interface CardTabListType {
key: string;
tab: VNodeTypes;
slots?: { tab: string };
disabled?: boolean;
}
export type CardType = 'inner';
const CardSize = tuple('default', 'small');
export type CardSizeType = typeof CardSize[number];
const { TabPane } = Tabs; const { TabPane } = Tabs;
export default {
const Card = defineComponent({
name: 'ACard', name: 'ACard',
mixins: [BaseMixin], mixins: [BaseMixin],
props: { props: {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
title: PropTypes.any, title: PropTypes.VNodeChild,
extra: PropTypes.any, extra: PropTypes.VNodeChild,
bordered: PropTypes.looseBool.def(true), bordered: PropTypes.looseBool.def(true),
bodyStyle: PropTypes.object, bodyStyle: PropTypes.style,
headStyle: PropTypes.object, headStyle: PropTypes.style,
loading: PropTypes.looseBool.def(false), loading: PropTypes.looseBool.def(false),
hoverable: PropTypes.looseBool.def(false), hoverable: PropTypes.looseBool.def(false),
type: PropTypes.string, type: PropTypes.string,
size: PropTypes.oneOf(['default', 'small']), size: {
actions: PropTypes.any, type: String as PropType<CardSizeType>,
tabList: PropTypes.array, validator: (val: CardSizeType) => CardSize.includes(val),
tabBarExtraContent: PropTypes.any, },
actions: PropTypes.VNodeChild,
tabList: {
type: Array as PropType<CardTabListType[]>,
},
tabBarExtraContent: PropTypes.VNodeChild,
activeTabKey: PropTypes.string, activeTabKey: PropTypes.string,
defaultActiveTabKey: PropTypes.string, defaultActiveTabKey: PropTypes.string,
cover: PropTypes.any, cover: PropTypes.VNodeChild,
onTabChange: PropTypes.func, onTabChange: {
type: Function as PropType<(key: string) => void>,
},
}, },
setup() { setup() {
return { return {
@ -42,7 +62,7 @@ export default {
}; };
}, },
methods: { methods: {
getAction(actions) { getAction(actions: VNodeTypes[]) {
const actionList = actions.map((action, index) => const actionList = actions.map((action, index) =>
(isVNode(action) && !isEmptyElement(action)) || !isVNode(action) ? ( (isVNode(action) && !isEmptyElement(action)) || !isVNode(action) ? (
<li style={{ width: `${100 / actions.length}%` }} key={`action-${index}`}> <li style={{ width: `${100 / actions.length}%` }} key={`action-${index}`}>
@ -52,13 +72,13 @@ export default {
); );
return actionList; return actionList;
}, },
triggerTabChange(key) { triggerTabChange(key: string) {
this.$emit('tabChange', key); this.$emit('tabChange', key);
}, },
isContainGrid(obj = []) { isContainGrid(obj: VNode[] = []) {
let containGrid; let containGrid;
obj.forEach(element => { obj.forEach(element => {
if (element && isPlainObject(element.type) && element.type.__ANT_CARD_GRID) { if (element && isPlainObject(element.type) && (element.type as any).__ANT_CARD_GRID) {
containGrid = true; containGrid = true;
} }
}); });
@ -160,8 +180,8 @@ export default {
tabList && tabList.length ? ( tabList && tabList.length ? (
<Tabs {...tabsProps}> <Tabs {...tabsProps}>
{tabList.map(item => { {tabList.map(item => {
const { tab: temp, scopedSlots = {}, slots = {} } = item; const { tab: temp, slots } = item as CardTabListType;
const name = slots.tab || scopedSlots.tab; const name = slots?.tab;
const tab = temp !== undefined ? temp : $slots[name] ? $slots[name](item) : null; const tab = temp !== undefined ? temp : $slots[name] ? $slots[name](item) : null;
return <TabPane tab={tab} key={item.key} disabled={item.disabled} />; return <TabPane tab={tab} key={item.key} disabled={item.disabled} />;
})} })}
@ -203,4 +223,6 @@ export default {
</div> </div>
); );
}, },
}; });
export default Card;

View File

@ -1,9 +1,9 @@
import { inject } from 'vue'; import { defineComponent, inject } from 'vue';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
import { getSlot } from '../_util/props-util'; import { getSlot } from '../_util/props-util';
export default { export default defineComponent({
name: 'ACardGrid', name: 'ACardGrid',
__ANT_CARD_GRID: true, __ANT_CARD_GRID: true,
props: { props: {
@ -18,7 +18,7 @@ export default {
render() { render() {
const { prefixCls: customizePrefixCls, hoverable = true } = this.$props; const { prefixCls: customizePrefixCls, hoverable = true } = this.$props;
const getPrefixCls = this.configProvider.getPrefixCls; const { getPrefixCls } = this.configProvider;
const prefixCls = getPrefixCls('card', customizePrefixCls); const prefixCls = getPrefixCls('card', customizePrefixCls);
const classString = { const classString = {
@ -27,4 +27,4 @@ export default {
}; };
return <div class={classString}>{getSlot(this)}</div>; return <div class={classString}>{getSlot(this)}</div>;
}, },
}; });

View File

@ -1,15 +1,21 @@
import { inject } from 'vue'; import { defineComponent, inject, PropType, VNodeTypes } from 'vue';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import { getComponent } from '../_util/props-util'; import { getComponent } from '../_util/props-util';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
export default { export default defineComponent({
name: 'ACardMeta', name: 'ACardMeta',
props: { props: {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
title: PropTypes.any, title: {
description: PropTypes.any, type: Object as PropType<VNodeTypes>,
avatar: PropTypes.any, },
description: {
type: Object as PropType<VNodeTypes>,
},
avatar: {
type: Object as PropType<VNodeTypes>,
},
}, },
setup() { setup() {
return { return {
@ -19,7 +25,7 @@ export default {
render() { render() {
const { prefixCls: customizePrefixCls } = this.$props; const { prefixCls: customizePrefixCls } = this.$props;
const getPrefixCls = this.configProvider.getPrefixCls; const { getPrefixCls } = this.configProvider;
const prefixCls = getPrefixCls('card', customizePrefixCls); const prefixCls = getPrefixCls('card', customizePrefixCls);
const classString = { const classString = {
@ -49,4 +55,4 @@ export default {
</div> </div>
); );
}, },
}; });

View File

@ -1,11 +1,13 @@
import { App } from 'vue';
import Card from './Card'; import Card from './Card';
import Meta from './Meta'; import Meta from './Meta';
import Grid from './Grid'; import Grid from './Grid';
Card.Meta = Meta; Card.Meta = Meta;
Card.Grid = Grid; Card.Grid = Grid;
/* istanbul ignore next */ /* istanbul ignore next */
Card.install = function(app) { Card.install = function(app: App) {
app.component(Card.name, Card); app.component(Card.name, Card);
app.component(Meta.name, Meta); app.component(Meta.name, Meta);
app.component(Grid.name, Grid); app.component(Grid.name, Grid);

View File

@ -1,4 +1,4 @@
import { inject } from 'vue'; import { defineComponent, inject } from 'vue';
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';
@ -18,7 +18,7 @@ import isValid from '../_util/isValid';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
import TabBar from './TabBar'; import TabBar from './TabBar';
export default { export default defineComponent({
TabPane, TabPane,
name: 'ATabs', name: 'ATabs',
inheritAttrs: false, inheritAttrs: false,
@ -174,4 +174,4 @@ export default {
}; };
return <VcTabs {...tabsProps} />; return <VcTabs {...tabsProps} />;
}, },
}; });