gi-component 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gi-component",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.5",
5
5
  "description": "Vue3中基于Element Plus二次封装基础组件库",
6
6
  "author": "lin",
7
7
  "license": "MIT",
@@ -2,7 +2,8 @@
2
2
  <el-table-column v-bind="columnProps">
3
3
  <!-- 处理render函数 -->
4
4
  <template v-if="column.render" v-slot="scope">
5
- <component :is="column.render(scope)" />
5
+ <template v-if="typeof column.render(scope) === 'string'">{{ column.render(scope) }}</template>
6
+ <component v-else :is="column.render(scope)" />
6
7
  </template>
7
8
 
8
9
  <!-- 处理插槽内容 -->
@@ -12,17 +13,9 @@
12
13
 
13
14
  <!-- 递归渲染子列 -->
14
15
  <template v-if="column.children && column.children.length > 0">
15
- <TableColumn
16
- v-for="child in column.children"
17
- :key="child.prop || child.label"
18
- :column="child"
19
- >
16
+ <TableColumn v-for="child in column.children" :key="child.prop || child.label" :column="child">
20
17
  <!-- 将所有插槽传递给子组件 -->
21
- <template
22
- v-for="(_, slotName) in $slots"
23
- :key="slotName"
24
- #[slotName]="scope"
25
- >
18
+ <template v-for="(_, slotName) in $slots" :key="slotName" #[slotName]="scope">
26
19
  <slot :name="slotName" v-bind="scope" />
27
20
  </template>
28
21
  </TableColumn>
@@ -2,15 +2,15 @@ import type {
2
2
  TableProps as ElTableProps,
3
3
  PaginationProps,
4
4
  TableColumnCtx,
5
- TableColumnInstance
5
+ TableColumnInstance,
6
6
  } from 'element-plus';
7
7
  import type { ExtractPropTypes, VNode } from 'vue';
8
8
 
9
- export interface TableColumnItem
10
- extends Omit<TableColumnInstance['$props'], never> {
9
+ type DefaultRow = Record<PropertyKey, any>
10
+ export interface TableColumnItem<T extends DefaultRow = DefaultRow> extends Omit<TableColumnInstance['$props'], never> {
11
11
  slotName?: string;
12
12
  children?: TableColumnItem[];
13
- render?: (scope: TableColumnCtx<any>) => VNode | VNode[] | string;
13
+ render?: (scope: { $index: number; cellIndex: number; column: TableColumnItem<T>; row: T }) => VNode | VNode[] | string;
14
14
  }
15
15
 
16
16
  export interface TableProps
@@ -1 +1,2 @@
1
1
  export * from './useBemClass';
2
+ export * from './useTable';
@@ -1,3 +1,92 @@
1
- export function useTable() {
2
1
 
2
+ import { ElMessageBox } from 'element-plus'
3
+ import { reactive, ref, type Ref } from 'vue'
4
+
5
+ interface Options<T, U> {
6
+ onSuccess?: () => void
7
+ immediate?: boolean
8
+ rowKey?: keyof T
9
+ }
10
+
11
+ interface ApiResult<T> {
12
+ code: number
13
+ data: T
14
+ message: string
15
+ success: boolean
16
+ }
17
+
18
+ interface PageResult<T> {
19
+ list: T[]
20
+ total: number
21
+ }
22
+
23
+ type Api<T> = (params: PaginationParams) => Promise<ApiResult<PageResult<T[]>>> | Promise<ApiResult<T[]>>
24
+
25
+ interface PaginationParams { page: number; size: number; }
26
+ export function useTable<T extends U, U = T>(api: Api<T>, options: Options<T, U>) {
27
+ const { onSuccess, immediate = true, rowKey = 'id' } = options || {}
28
+
29
+ const loading = ref(false)
30
+ const tableData: Ref<U[]> = ref([])
31
+
32
+ const pagination = reactive({
33
+ currentPage: 1,
34
+ pageSize: 20,
35
+ total: 0,
36
+ onCurrentChange: (size: number) => {
37
+ pagination.currentPage = size
38
+ getTableData()
39
+ },
40
+ onSizeChange: (size: number) => {
41
+ pagination.pageSize = size
42
+ getTableData()
43
+ },
44
+ })
45
+
46
+ function setTotal(total: number) {
47
+ pagination.total = total
48
+ }
49
+
50
+ async function getTableData() {
51
+ try {
52
+ loading.value = true
53
+ const res = await api({ page: pagination.currentPage, size: pagination.pageSize })
54
+ // 处理返回的数据结构可能是分页或数组的情况
55
+ const data = !Array.isArray(res.data) ? res.data.list : res.data
56
+ tableData.value = data as T[]
57
+ // 设置总数据量
58
+ const total = !Array.isArray(res.data) ? res.data.total : data.length
59
+ setTotal(total)
60
+ onSuccess?.()
61
+ } finally {
62
+ loading.value = false
63
+ }
64
+ }
65
+
66
+ // 是否立即触发请求
67
+ immediate && getTableData()
68
+
69
+ function search() {
70
+ pagination.currentPage = 1
71
+ getTableData()
72
+ }
73
+
74
+ function refresh() {
75
+ getTableData()
76
+ }
77
+
78
+ return {
79
+ /** 表格数据 */
80
+ tableData,
81
+ /** 获取表格数据 */
82
+ getTableData,
83
+ /** 分页数据 */
84
+ pagination,
85
+ /** 加载状态 */
86
+ loading,
87
+ /** 搜索 */
88
+ search,
89
+ /** 刷新 */
90
+ refresh,
91
+ }
3
92
  }
package/packages/index.ts CHANGED
@@ -18,6 +18,7 @@ export * from './components/dialog';
18
18
  export * from './components/edit-table';
19
19
  export * from './components/form';
20
20
  export * from './components/table';
21
+ export * from './components/tabs';
21
22
  export * from './hooks';
22
23
  export * from './utils';
23
24