Skip to content

Commit

Permalink
feat(table): 新增支持配置组件
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Aug 25, 2023
1 parent 644fa4f commit bd6fae9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
40 changes: 40 additions & 0 deletions packages/table/src/ComponentColumn.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<template>
<TMagicTableColumn
show-overflow-tooltip
:label="config.label"
:width="config.width"
:fixed="config.fixed"
:sortable="config.sortable"
:prop="config.prop"
>
<template v-slot="scope">
<component :is="config.component" v-bind="componentProps(scope.row)"></component>
</template>
</TMagicTableColumn>
</template>

<script lang="ts" setup>
import { TMagicTableColumn } from '@tmagic/design';
import { ColumnConfig } from './schema';
defineOptions({
name: 'MTableColumn',
});
const props = withDefaults(
defineProps<{
config: ColumnConfig;
}>(),
{
config: () => ({}),
},
);
const componentProps = (row: any) => {
if (typeof props.config.props === 'function') {
return props.config.props(row) || {};
}
return props.config.props || {};
};
</script>
5 changes: 5 additions & 0 deletions packages/table/src/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<ExpandColumn :config="item" :key="columnIndex"></ExpandColumn>
</template>

<template v-else-if="item.type === 'component'">
<ComponentColumn :config="item" :key="columnIndex"></ComponentColumn>
</template>

<template v-else-if="item.selection">
<component
width="40"
Expand Down Expand Up @@ -64,6 +68,7 @@ import { cloneDeep } from 'lodash-es';
import { getConfig, TMagicTable } from '@tmagic/design';
import ActionsColumn from './ActionsColumn.vue';
import ComponentColumn from './ComponentColumn.vue';
import ExpandColumn from './ExpandColumn.vue';
import PopoverColumn from './PopoverColumn.vue';
import TextColumn from './TextColumn.vue';
Expand Down
2 changes: 1 addition & 1 deletion packages/table/src/TextColumn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<template #content>
<div>{{ formatter(config, scope.row) }}</div>
</template>
<TMagicButton text type="primary">扩展配置</TMagicButton>
<TMagicButton text type="primary">{{ config.buttonText || '扩展配置' }}</TMagicButton>
</el-tooltip>
<TMagicTag
Expand Down
6 changes: 4 additions & 2 deletions packages/table/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type ColumnConfig = {
fixed?: 'left' | 'right' | boolean;
width?: number | string;
actions?: ColumnActionConfig[];
type?: 'popover' | 'expand' | string | ((value: any, row: any) => string);
type?: 'popover' | 'expand' | 'component' | string | ((value: any, row: any) => string);
text?: string;
prop?: string;
showHeader?: boolean;
Expand All @@ -58,8 +58,10 @@ export type ColumnConfig = {
handler?: (row: any) => void;
/** 当type为expand时有效,展开为html */
expandContent?: (row: any, prop?: string) => string;
/** 当type为expand时有效,展开为vue组件 */
/** 当type为expand时,展开为vue组件;当type为component时显示的组件 */
component?: any;
/** 当type为expand时有效,展开的vue组件props */
props?: any;
/** 当type为tip时有效,显示文案 */
buttonText?: string;
};

0 comments on commit bd6fae9

Please sign in to comment.