Pārlūkot izejas kodu

cl-view-group 添加泛型

神仙都没用 1 gadu atpakaļ
vecāks
revīzija
ad841ac140

+ 25 - 18
src/plugins/view/components/group.vue

@@ -279,24 +279,31 @@ function remove(item: ClViewGroup.Item) {
 		type: "warning"
 	})
 		.then(() => {
-			config.service
-				.delete({
-					ids: [item.id]
-				})
-				.then(async () => {
-					ElMessage.success("删除成功");
-
-					// 刷新列表
-					await refresh();
-
-					// 删除当前
-					if (selected.value?.id == item.id) {
-						select();
-					}
-				})
-				.catch((err) => {
-					ElMessage.error(err.message);
-				});
+			function next(params: any) {
+				config.service
+					.delete(params)
+					.then(async () => {
+						ElMessage.success("删除成功");
+
+						// 刷新列表
+						await refresh();
+
+						// 删除当前
+						if (selected.value?.id == item.id) {
+							select();
+						}
+					})
+					.catch((err) => {
+						ElMessage.error(err.message);
+					});
+			}
+
+			// 删除事件
+			if (config.onDelete) {
+				config.onDelete(item, { next });
+			} else {
+				next({ ids: [item.id] });
+			}
 		})
 		.catch(() => null);
 }

+ 2 - 2
src/plugins/view/config.ts

@@ -5,8 +5,8 @@ export default (): ModuleConfig => {
 		label: "视图组件",
 		description: "左右侧布局、顶部详情等",
 		author: "COOL",
-		version: "1.0.3",
-		updateTime: "2024-03-23",
+		version: "1.0.4",
+		updateTime: "2024-03-25",
 		demo: [
 			{
 				name: "左右侧布局",

+ 2 - 2
src/plugins/view/hooks/group.ts

@@ -2,8 +2,8 @@ import { provide, ref } from "vue";
 import { useParent } from "/@/cool";
 import type { ClViewGroup } from "../types";
 
-export function useViewGroup(options?: DeepPartial<ClViewGroup.Options>) {
-	const ViewGroup = ref<ClViewGroup.Ref>();
+export function useViewGroup<T = ClViewGroup.Item>(options?: DeepPartial<ClViewGroup.Options<T>>) {
+	const ViewGroup = ref<ClViewGroup.Ref<T>>();
 	useParent("cl-view-group", ViewGroup);
 
 	if (options) {

+ 13 - 9
src/plugins/view/types/index.d.ts

@@ -5,20 +5,23 @@ export declare namespace ClViewGroup {
 	interface Item {
 		id: any;
 		name: string;
+		children: Item[];
 		[key: string]: any;
 	}
 
-	interface Ref {
-		selected: Item | undefined;
+	type M<T> = T & Item;
+
+	interface Ref<T = Item> {
+		selected: M<T> | undefined;
 		isExpand: boolean;
 		select(data?: any): void;
 		expand(value?: boolean): void;
-		edit(item?: Item): void;
-		remove(item: Item): void;
+		edit(item?: M<T>): void;
+		remove(item: M<T>): void;
 		refresh(params?: any): void;
 	}
 
-	interface Options {
+	interface Options<T = Item> {
 		label: string;
 		title: string;
 		leftWidth: string;
@@ -57,9 +60,10 @@ export declare namespace ClViewGroup {
 		enableAdd?: boolean;
 		enableRefresh?: boolean;
 		custom?: boolean;
-		onSelect?(item: Item): void;
-		onEdit?(item?: Item): DeepPartial<ClForm.Options>;
-		onContextMenu?(item: Item): ClContextMenu.Options;
-		onData?(list: any[]): any[];
+		onSelect?(item: M<T>): void;
+		onEdit?(item?: M<T>): DeepPartial<ClForm.Options>;
+		onContextMenu?(item: M<T>): ClContextMenu.Options;
+		onData?(list: M<T>[]): any[];
+		onDelete?(item: M<T>, { next }: { next(params: any): void }): Promise<void> | void;
 	}
 }