update
This commit is contained in:
parent
10fe490879
commit
8b2313f817
@ -9,9 +9,34 @@ import {
|
|||||||
AlertDialogTitle,
|
AlertDialogTitle,
|
||||||
AlertDialogTrigger,
|
AlertDialogTrigger,
|
||||||
} from '@/components/ui/alert-dialog';
|
} from '@/components/ui/alert-dialog';
|
||||||
import { useMemo, useRef, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
type useConfirmOptions = {
|
type useConfirmOptions = {
|
||||||
|
confrimProps: ConfirmProps;
|
||||||
|
};
|
||||||
|
type Fn = () => void;
|
||||||
|
export const useConfirm = (opts?: useConfirmOptions) => {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
type ConfirmOptions = {
|
||||||
|
onOk?: Fn;
|
||||||
|
onCancel?: Fn;
|
||||||
|
};
|
||||||
|
const confirm = (opts?: ConfirmOptions) => {
|
||||||
|
setOpen(true);
|
||||||
|
};
|
||||||
|
const module = useMemo(() => {
|
||||||
|
return <Confirm {...opts?.confrimProps} hasTrigger={false} open={open} setOpen={setOpen} />;
|
||||||
|
}, [open]);
|
||||||
|
return {
|
||||||
|
module: module,
|
||||||
|
open,
|
||||||
|
setOpen,
|
||||||
|
confirm,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type ConfirmProps = {
|
||||||
|
children?: React.ReactNode;
|
||||||
tip?: React.ReactNode;
|
tip?: React.ReactNode;
|
||||||
title?: string;
|
title?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
@ -19,87 +44,55 @@ type useConfirmOptions = {
|
|||||||
onCancelText?: string;
|
onCancelText?: string;
|
||||||
onOk?: Fn;
|
onOk?: Fn;
|
||||||
onCancle?: Fn;
|
onCancle?: Fn;
|
||||||
children?: React.ReactNode;
|
hasTrigger?: boolean;
|
||||||
|
open?: boolean;
|
||||||
|
setOpen?: (open: boolean) => void;
|
||||||
|
footer?: React.ReactNode;
|
||||||
};
|
};
|
||||||
type Fn = () => void;
|
export const Confirm = (props: ConfirmProps) => {
|
||||||
export const useConfirm = (opts?: useConfirmOptions) => {
|
const [isOpen, setIsOpen] = useState(props.open);
|
||||||
const [open, setOpen] = useState(false);
|
const hasTrigger = props.hasTrigger ?? true;
|
||||||
const onOkRef = useRef<Fn>(null);
|
useEffect(() => {
|
||||||
const onCancle = useRef<Fn>(null);
|
setIsOpen(props.open);
|
||||||
type ConfirmOptions = {
|
}, [props.open]);
|
||||||
onOk?: Fn;
|
|
||||||
onCancel?: Fn;
|
|
||||||
};
|
|
||||||
const confirm = (opts: ConfirmOptions) => {
|
|
||||||
setOpen(true);
|
|
||||||
opts.onOk && (onOkRef.current = opts.onOk);
|
|
||||||
opts.onCancel && (onCancle.current = opts.onCancel);
|
|
||||||
};
|
|
||||||
const module = useMemo(() => {
|
|
||||||
if (!open) return null;
|
|
||||||
return (
|
return (
|
||||||
<AlertDialog>
|
<AlertDialog
|
||||||
<AlertDialogTrigger>{opts?.tip ?? '提示'}</AlertDialogTrigger>
|
open={isOpen}
|
||||||
|
onOpenChange={(v) => {
|
||||||
|
setIsOpen(v);
|
||||||
|
props?.setOpen?.(v);
|
||||||
|
}}>
|
||||||
|
{hasTrigger && (
|
||||||
|
<>
|
||||||
|
{props?.children && <AlertDialogTrigger asChild>{props?.children ?? props?.tip ?? '提示'}</AlertDialogTrigger>}
|
||||||
|
{!props?.children && <AlertDialogTrigger>{props?.tip ?? '提示'}</AlertDialogTrigger>}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
<AlertDialogContent>
|
<AlertDialogContent>
|
||||||
<AlertDialogHeader>
|
<AlertDialogHeader>
|
||||||
<AlertDialogTitle>{opts?.title ?? '是否确认删除?'}</AlertDialogTitle>
|
<AlertDialogTitle>{props?.title ?? '是否确认删除?'}</AlertDialogTitle>
|
||||||
<AlertDialogDescription>{opts?.description ?? '此操作无法撤销,是否继续。'}</AlertDialogDescription>
|
<AlertDialogDescription>{props?.description ?? '此操作无法撤销,是否继续。'}</AlertDialogDescription>
|
||||||
</AlertDialogHeader>
|
</AlertDialogHeader>
|
||||||
<AlertDialogFooter>
|
<AlertDialogFooter>
|
||||||
|
{props?.footer && <div className='flex gap-2'>{props?.footer}</div>}
|
||||||
|
{!props?.footer && (
|
||||||
|
<>
|
||||||
<AlertDialogCancel
|
<AlertDialogCancel
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
setOpen(false);
|
props?.onCancle?.();
|
||||||
onCancle.current && onCancle.current();
|
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
}}>
|
}}>
|
||||||
{opts?.onCancelText ?? '取消'}
|
{props?.onCancelText ?? '取消'}
|
||||||
</AlertDialogCancel>
|
</AlertDialogCancel>
|
||||||
<AlertDialogAction
|
<AlertDialogAction
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
setOpen(false);
|
props?.onOk?.();
|
||||||
onOkRef.current && onOkRef.current();
|
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
}}>
|
}}>
|
||||||
{opts?.onOkText ?? '确定'}
|
{props?.onOkText ?? '确定'}
|
||||||
</AlertDialogAction>
|
</AlertDialogAction>{' '}
|
||||||
</AlertDialogFooter>
|
</>
|
||||||
</AlertDialogContent>
|
)}
|
||||||
</AlertDialog>
|
|
||||||
);
|
|
||||||
}, [open]);
|
|
||||||
return {
|
|
||||||
module,
|
|
||||||
open,
|
|
||||||
setOpen,
|
|
||||||
confirm,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const Confirm = (opts?: useConfirmOptions) => {
|
|
||||||
return (
|
|
||||||
<AlertDialog>
|
|
||||||
{opts?.children && <AlertDialogTrigger asChild>{opts?.children ?? opts?.tip ?? '提示'}</AlertDialogTrigger>}
|
|
||||||
{!opts?.children && <AlertDialogTrigger>{opts?.tip ?? '提示'}</AlertDialogTrigger>}
|
|
||||||
<AlertDialogContent>
|
|
||||||
<AlertDialogHeader>
|
|
||||||
<AlertDialogTitle>{opts?.title ?? '是否确认删除?'}</AlertDialogTitle>
|
|
||||||
<AlertDialogDescription>{opts?.description ?? '此操作无法撤销,是否继续。'}</AlertDialogDescription>
|
|
||||||
</AlertDialogHeader>
|
|
||||||
<AlertDialogFooter>
|
|
||||||
<AlertDialogCancel
|
|
||||||
onClick={(e) => {
|
|
||||||
opts?.onCancle?.();
|
|
||||||
e.stopPropagation();
|
|
||||||
}}>
|
|
||||||
{opts?.onCancelText ?? '取消'}
|
|
||||||
</AlertDialogCancel>
|
|
||||||
<AlertDialogAction
|
|
||||||
onClick={(e) => {
|
|
||||||
opts?.onOk?.();
|
|
||||||
e.stopPropagation();
|
|
||||||
}}>
|
|
||||||
{opts?.onOkText ?? '确定'}
|
|
||||||
</AlertDialogAction>
|
|
||||||
</AlertDialogFooter>
|
</AlertDialogFooter>
|
||||||
</AlertDialogContent>
|
</AlertDialogContent>
|
||||||
</AlertDialog>
|
</AlertDialog>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user