gi-component 0.0.3 → 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.
@@ -1,89 +1,89 @@
1
- <template>
2
- <div :class="b('table')">
3
- <el-table v-bind="tableProps" ref="tableRef" :data="props.data as any[]">
4
- <TableColumn v-for="item in props.columns" :key="item.prop || item.label" :column="item">
5
- <!-- 将所有插槽传递给子组件 -->
6
- <template v-for="(_, slotName) in $slots" :key="slotName" #[slotName]="scope">
7
- <slot :name="slotName" v-bind="scope" />
8
- </template>
9
- </TableColumn>
10
- </el-table>
11
-
12
- <el-row justify="end" :class="b('table-pagination')">
13
- <el-pagination v-bind="paginationProps" v-model:current-page="paginationProps.currentPage"
14
- v-model:page-size="paginationProps.pageSize" @size-change="handleSizeChange"
15
- @current-change="handleCurrentChange" />
16
- </el-row>
17
- </div>
18
- </template>
19
-
20
- <script lang="ts" setup>
21
- import type { TableProps } from './type';
22
- import { computed, useAttrs, useTemplateRef } from 'vue';
23
- import { useBemClass } from '../../../hooks';
24
- import TableColumn from './TableColumn.vue';
25
-
26
- const props = withDefaults(defineProps<TableProps>(), {
27
- columns: () => [],
28
- pagination: () => ({})
29
- });
30
-
31
- const attrs = useAttrs();
32
- const { b } = useBemClass();
33
- const tableRef = useTemplateRef('tableRef');
34
-
35
- const tableProps = computed(() => {
36
- return {
37
- ...attrs,
38
- ...props,
39
- columns: undefined,
40
- pagination: undefined
41
- };
42
- });
43
-
44
- const paginationProps = computed(() => {
45
- return {
46
- background: true,
47
- layout: 'prev, pager, next, sizes, total',
48
- pageSizes: [10, 20, 50, 100],
49
- ...props.pagination
50
- };
51
- });
52
-
53
- function handleSizeChange(size: number) {
54
- // @ts-ignore
55
- props.pagination.pageSize = size;
56
- }
57
-
58
- function handleCurrentChange(page: number) {
59
- // @ts-ignore
60
- props.pagination.currentPage = page;
61
- }
62
-
63
- defineExpose({
64
- tableRef
65
- })
66
- </script>
67
-
68
- <style lang="scss" scoped>
69
- @use '../../../styles/var.scss' as a;
70
-
71
- :deep(.el-pagination__rightwrapper) {
72
- flex: auto;
73
- }
74
-
75
- .#{a.$prefix}-table {
76
- height: 100%;
77
- overflow: hidden;
78
- display: flex;
79
- flex-direction: column;
80
-
81
- :deep(.el-table) {
82
- flex: 1;
83
- }
84
- }
85
-
86
- .#{a.$prefix}-table-pagination {
87
- margin-top: 10px;
88
- }
89
- </style>
1
+ <template>
2
+ <div :class="b('table')">
3
+ <el-table v-bind="tableProps" ref="tableRef" :data="props.data as any[]">
4
+ <TableColumn v-for="item in props.columns" :key="item.prop || item.label" :column="item">
5
+ <!-- 将所有插槽传递给子组件 -->
6
+ <template v-for="(_, slotName) in $slots" :key="slotName" #[slotName]="scope">
7
+ <slot :name="slotName" v-bind="scope" />
8
+ </template>
9
+ </TableColumn>
10
+ </el-table>
11
+
12
+ <el-row justify="end" :class="b('table-pagination')">
13
+ <el-pagination v-bind="paginationProps" v-model:current-page="paginationProps.currentPage"
14
+ v-model:page-size="paginationProps.pageSize" @size-change="handleSizeChange"
15
+ @current-change="handleCurrentChange" />
16
+ </el-row>
17
+ </div>
18
+ </template>
19
+
20
+ <script lang="ts" setup>
21
+ import type { TableProps } from './type';
22
+ import { computed, useAttrs, useTemplateRef } from 'vue';
23
+ import { useBemClass } from '../../../hooks';
24
+ import TableColumn from './TableColumn.vue';
25
+
26
+ const props = withDefaults(defineProps<TableProps>(), {
27
+ columns: () => [],
28
+ pagination: () => ({})
29
+ });
30
+
31
+ const attrs = useAttrs();
32
+ const { b } = useBemClass();
33
+ const tableRef = useTemplateRef('tableRef');
34
+
35
+ const tableProps = computed(() => {
36
+ return {
37
+ ...attrs,
38
+ ...props,
39
+ columns: undefined,
40
+ pagination: undefined
41
+ };
42
+ });
43
+
44
+ const paginationProps = computed(() => {
45
+ return {
46
+ background: true,
47
+ layout: 'prev, pager, next, sizes, total',
48
+ pageSizes: [10, 20, 50, 100],
49
+ ...props.pagination
50
+ };
51
+ });
52
+
53
+ function handleSizeChange(size: number) {
54
+ // @ts-ignore
55
+ props.pagination.pageSize = size;
56
+ }
57
+
58
+ function handleCurrentChange(page: number) {
59
+ // @ts-ignore
60
+ props.pagination.currentPage = page;
61
+ }
62
+
63
+ defineExpose({
64
+ tableRef
65
+ })
66
+ </script>
67
+
68
+ <style lang="scss" scoped>
69
+ @use '../../../styles/var.scss' as a;
70
+
71
+ :deep(.el-pagination__rightwrapper) {
72
+ flex: auto;
73
+ }
74
+
75
+ .#{a.$prefix}-table {
76
+ height: 100%;
77
+ overflow: hidden;
78
+ display: flex;
79
+ flex-direction: column;
80
+
81
+ :deep(.el-table) {
82
+ flex: 1;
83
+ }
84
+ }
85
+
86
+ .#{a.$prefix}-table-pagination {
87
+ margin-top: 10px;
88
+ }
89
+ </style>
@@ -1,22 +1,22 @@
1
- import type {
2
- TableProps as ElTableProps,
3
- PaginationProps,
4
- TableColumnCtx,
5
- TableColumnInstance
6
- } from 'element-plus';
7
- import type { ExtractPropTypes, VNode } from 'vue';
8
-
9
- export interface TableColumnItem
10
- extends Omit<TableColumnInstance['$props'], never> {
11
- slotName?: string;
12
- children?: TableColumnItem[];
13
- render?: (scope: TableColumnCtx<any>) => VNode | VNode[] | string;
14
- }
15
-
16
- export interface TableProps
17
- extends ExtractPropTypes<
18
- ElTableProps<Record<string | number | symbol, any>>
19
- > {
20
- columns?: TableColumnItem[];
21
- pagination?: Partial<PaginationProps>;
22
- }
1
+ import type {
2
+ TableProps as ElTableProps,
3
+ PaginationProps,
4
+ TableColumnCtx,
5
+ TableColumnInstance,
6
+ } from 'element-plus';
7
+ import type { ExtractPropTypes, VNode } from 'vue';
8
+
9
+ type DefaultRow = Record<PropertyKey, any>
10
+ export interface TableColumnItem<T extends DefaultRow = DefaultRow> extends Omit<TableColumnInstance['$props'], never> {
11
+ slotName?: string;
12
+ children?: TableColumnItem[];
13
+ render?: (scope: { $index: number; cellIndex: number; column: TableColumnItem<T>; row: T }) => VNode | VNode[] | string;
14
+ }
15
+
16
+ export interface TableProps
17
+ extends ExtractPropTypes<
18
+ ElTableProps<Record<string | number | symbol, any>>
19
+ > {
20
+ columns?: TableColumnItem[];
21
+ pagination?: Partial<PaginationProps>;
22
+ }
@@ -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