feat: 完善训练
This commit is contained in:
92
hooks/useDialog.ts
Normal file
92
hooks/useDialog.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { Ionicons } from '@expo/vector-icons';
|
||||
import { useState } from 'react';
|
||||
|
||||
export interface DialogConfig {
|
||||
title: string;
|
||||
message?: string;
|
||||
confirmText?: string;
|
||||
cancelText?: string;
|
||||
destructive?: boolean;
|
||||
icon?: keyof typeof Ionicons.glyphMap;
|
||||
iconColor?: string;
|
||||
}
|
||||
|
||||
export interface ActionSheetConfig {
|
||||
title?: string;
|
||||
subtitle?: string;
|
||||
cancelText?: string;
|
||||
}
|
||||
|
||||
export interface ActionSheetOption {
|
||||
id: string;
|
||||
title: string;
|
||||
subtitle?: string;
|
||||
icon?: keyof typeof Ionicons.glyphMap;
|
||||
iconColor?: string;
|
||||
destructive?: boolean;
|
||||
onPress: () => void;
|
||||
}
|
||||
|
||||
export function useDialog() {
|
||||
// 确认弹窗状态
|
||||
const [confirmDialog, setConfirmDialog] = useState<{
|
||||
visible: boolean;
|
||||
config: DialogConfig;
|
||||
onConfirm: () => void;
|
||||
}>({
|
||||
visible: false,
|
||||
config: { title: '' },
|
||||
onConfirm: () => {},
|
||||
});
|
||||
|
||||
// ActionSheet状态
|
||||
const [actionSheet, setActionSheet] = useState<{
|
||||
visible: boolean;
|
||||
config: ActionSheetConfig;
|
||||
options: ActionSheetOption[];
|
||||
}>({
|
||||
visible: false,
|
||||
config: {},
|
||||
options: [],
|
||||
});
|
||||
|
||||
// 显示确认弹窗
|
||||
const showConfirm = (config: DialogConfig, onConfirm: () => void) => {
|
||||
setConfirmDialog({
|
||||
visible: true,
|
||||
config,
|
||||
onConfirm,
|
||||
});
|
||||
};
|
||||
|
||||
// 显示ActionSheet
|
||||
const showActionSheet = (config: ActionSheetConfig, options: ActionSheetOption[]) => {
|
||||
setActionSheet({
|
||||
visible: true,
|
||||
config,
|
||||
options,
|
||||
});
|
||||
};
|
||||
|
||||
// 关闭确认弹窗
|
||||
const hideConfirm = () => {
|
||||
setConfirmDialog(prev => ({ ...prev, visible: false }));
|
||||
};
|
||||
|
||||
// 关闭ActionSheet
|
||||
const hideActionSheet = () => {
|
||||
setActionSheet(prev => ({ ...prev, visible: false }));
|
||||
};
|
||||
|
||||
return {
|
||||
// 确认弹窗
|
||||
confirmDialog,
|
||||
showConfirm,
|
||||
hideConfirm,
|
||||
|
||||
// ActionSheet
|
||||
actionSheet,
|
||||
showActionSheet,
|
||||
hideActionSheet,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user