update 优化 重构抽出一些常用hooks组件简化页面编码

This commit is contained in:
疯狂的狮子Li
2026-04-17 15:46:41 +08:00
parent 6a3394bdc5
commit c7bcf52f03
16 changed files with 202 additions and 154 deletions
+19
View File
@@ -0,0 +1,19 @@
import { addDateRange } from '@/utils/ruoyi';
export function useDateRangeQuery(propName?: string) {
const dateRange = ref<any>(['', '']);
const applyDateRange = <T extends Record<string, any>>(queryParams: T) => {
return addDateRange(queryParams, dateRange.value, propName);
};
const resetDateRange = () => {
dateRange.value = ['', ''];
};
return {
dateRange,
applyDateRange,
resetDateRange
};
}
+44
View File
@@ -0,0 +1,44 @@
import type { Ref } from 'vue';
type TableSortOrder = 'ascending' | 'descending' | null;
interface TableSortState {
prop: string;
order: TableSortOrder;
}
interface UseTableSortQueryOptions<T extends Record<string, any>> {
queryParams: Ref<T>;
defaultSort: TableSortState;
tableRef?: Ref<ElTableInstance | undefined>;
onSortChange?: () => void;
}
export function useTableSortQuery<T extends { orderByColumn?: string; isAsc?: string | null }>(
options: UseTableSortQueryOptions<T>
) {
const { queryParams, defaultSort: initialSort, tableRef, onSortChange } = options;
const defaultSort = ref<TableSortState>({ ...initialSort });
const handleSortChange = (column: { prop: string; order: TableSortOrder }) => {
queryParams.value.orderByColumn = column.prop as T['orderByColumn'];
queryParams.value.isAsc = column.order as T['isAsc'];
onSortChange?.();
};
const resetSort = () => {
queryParams.value.orderByColumn = defaultSort.value.prop as T['orderByColumn'];
queryParams.value.isAsc = defaultSort.value.order as T['isAsc'];
if (tableRef?.value && defaultSort.value.order) {
tableRef.value.sort(defaultSort.value.prop, defaultSort.value.order);
return;
}
onSortChange?.();
};
return {
defaultSort,
handleSortChange,
resetSort
};
}
+39
View File
@@ -0,0 +1,39 @@
import type { Ref } from 'vue';
interface UseTreeTableExpandOptions<T> {
tableRef: Ref<ElTableInstance | undefined>;
data: Ref<T[]>;
initialExpandAll?: boolean;
getChildren?: (row: T) => T[] | undefined;
}
export function useTreeTableExpand<T extends Record<string, any>>(options: UseTreeTableExpandOptions<T>) {
const { tableRef, data, initialExpandAll = true, getChildren = row => row.children as T[] | undefined } = options;
const isExpandAll = ref(initialExpandAll);
const toggleRows = (rows: T[], expanded: boolean) => {
rows.forEach(row => {
tableRef.value?.toggleRowExpansion(row, expanded);
const children = getChildren(row);
if (children?.length) {
toggleRows(children, expanded);
}
});
};
const setExpandAll = (expanded: boolean) => {
isExpandAll.value = expanded;
toggleRows(data.value, expanded);
};
const handleToggleExpandAll = () => {
setExpandAll(!isExpandAll.value);
};
return {
isExpandAll,
setExpandAll,
handleToggleExpandAll
};
}
-31
View File
@@ -1,31 +0,0 @@
import type { Ref } from 'vue';
interface Options {
title?: string;
}
interface Return {
title: Ref<string>;
visible: Ref<boolean>;
openDialog: () => void;
closeDialog: () => void;
}
export default (ops?: Options): Return => {
const visible = ref(false);
const title = ref(ops?.title || '');
const openDialog = () => {
visible.value = true;
};
const closeDialog = () => {
visible.value = false;
};
return {
title,
visible,
openDialog,
closeDialog
};
};