Skip to content

Commit

Permalink
Merge pull request #13 from jsxiaosi/fix/type-check
Browse files Browse the repository at this point in the history
fix(components): 🐛 table ts type error
  • Loading branch information
jsxiaosi authored Dec 28, 2024
2 parents faadedd + 53dd6fb commit 08c5316
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"vite-plugin-svg-icons": "^2.0.1",
"vitest": "^2.1.8",
"vue-eslint-parser": "^9.4.3",
"vue-tsc": "^2.1.10",
"vue-tsc": "^2.2.0",
"xlsx": "^0.18.5"
},
"pnpm": {
Expand Down
89 changes: 47 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/components/Table/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
import { ElTable } from 'element-plus';
import type { TableProps } from 'element-plus';
import TableChild from './src/components/TableChild';
import type { TableColumnProps } from './types/table';
import type { TableColumnProps, TableSlotType } from './types/table';
const props = defineProps<TableProps<T> & { option: TableColumnProps<T>[] }>();
defineSlots<TableSlotType<T>>();
</script>

<template>
<div>
<ElTable v-bind="{ ...props, ...$attrs }" :data="data" :border="border" fit show-header>
<TableChild v-for="(item, index) in option" :key="index" :item="item">
<template v-for="soitem in Object.keys($slots)" #[soitem]="data">
<slot :name="soitem" v-bind="data" />
<template v-for="slotsItem in Object.keys($slots)" #[slotsItem]="data">
<slot :name="slotsItem" v-bind="data" />
</template>
</TableChild>
</ElTable>
Expand Down
7 changes: 4 additions & 3 deletions src/components/Table/src/components/TableChild.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getSlot } from '@/utils/slotsHelper';
import { ElTableColumn } from 'element-plus';
import { defineComponent } from 'vue';
import type { TableColumnCtx } from 'element-plus';
import type { TableColumnInstance } from 'element-plus';
import type { SetupContext, VNode } from 'vue';
import type { TableColumnProps } from '../../types/table';

Expand All @@ -10,17 +10,18 @@ interface RenderType {
header?: (scope: Recordable) => VNode[] | VNode | null;
}

// TODO: header 存在children时,slots 无法渲染
const TableChild = defineComponent(
<T extends object = any>(props: { item: TableColumnProps<T> }, { slots }: SetupContext) => {
const { item } = props;

function eLComponent(childrenRender: RenderType | null | undefined = null) {
const { children, ...reItem } = item;
if (childrenRender?.default) {
return <ElTableColumn {...(reItem as TableColumnCtx<Recordable>)}>{childrenRender}</ElTableColumn>;
return <ElTableColumn {...(reItem as TableColumnInstance)}>{childrenRender}</ElTableColumn>;
} else {
return (
<ElTableColumn {...(reItem as TableColumnCtx<Recordable>)}>
<ElTableColumn {...(reItem as TableColumnInstance)}>
{childrenRender?.header}
{children?.map(child => <TableChild item={child} />)}
</ElTableColumn>
Expand Down
16 changes: 15 additions & 1 deletion src/components/Table/types/table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TableColumnCtx } from 'element-plus';
import type { RenderRowData, TableColumnCtx } from 'element-plus';

export interface TableColumnProps<T extends Recordable = Recordable>
extends Partial<Omit<TableColumnCtx<T>, 'prop' | 'children'>> {
Expand All @@ -8,3 +8,17 @@ export interface TableColumnProps<T extends Recordable = Recordable>
render_header?: (row: any) => JSX.Element;
children?: TableColumnProps<T>[];
}

// export type TableSlotType<T extends object = object> = {
// [key in keyof T as `${string & key}_header`]: (props: any) => void;
// };

export type TableSlotType<T extends object = object> = {
[key in keyof T]: (props: RenderRowData<T>) => void;
} & {
[key in keyof T as `${string & key}_header`]: (
props: Omit<RenderRowData<T>, 'row' | 'treeNode' | 'expanded'> & {
customItem: TableColumnProps<T>;
},
) => void;
};
1 change: 1 addition & 0 deletions src/views/components/table-page/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<span>表格</span>
</div>
</template>

<Table :data="tabList" border row-key="date" :option="option">
<template #name_header="slotData">
<span>{{ `插槽:${slotData.customItem.label}` }}</span>
Expand Down

0 comments on commit 08c5316

Please sign in to comment.