组件
确认对话框 Confirm Dialog
由 Dialog 和 Button 组合而成的开箱即用确认对话框。
基础示例
Loading…
pnpm dlx wui@latest add @wui/confirm-dialogRegistry 依赖
confirm-dialog 将 dialog 和 button 声明为 registry 依赖,因此 CLI
会通过一条命令解析并安装这三个组件(以及共享的 cn 工具函数)。
"use client"
import * as React from "react"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
export interface ConfirmDialogProps {
/** The element that opens the dialog. Rendered inside the trigger via `asChild`. */
trigger: React.ReactNode
/** Heading shown at the top of the dialog. */
title: string
/** Optional supporting text under the title. */
description?: string
/** Label for the confirm button. @default "Confirm" */
confirmLabel?: string
/** Label for the cancel button. @default "Cancel" */
cancelLabel?: string
/** Visual style of the confirm button. @default "default" */
variant?: "default" | "destructive"
/** Called when the user clicks the confirm button. */
onConfirm?: () => void
}
/**
* A ready-made confirmation dialog composed from `Dialog` and `Button`.
* Demonstrates a component that depends on other registry items.
*/
function ConfirmDialog({
trigger,
title,
description,
confirmLabel = "Confirm",
cancelLabel = "Cancel",
variant = "default",
onConfirm,
}: ConfirmDialogProps) {
return (
<Dialog>
<DialogTrigger asChild>{trigger}</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
{description ? (
<DialogDescription>{description}</DialogDescription>
) : null}
</DialogHeader>
<DialogFooter>
<DialogClose asChild>
<Button variant="outline">{cancelLabel}</Button>
</DialogClose>
<DialogClose asChild>
<Button variant={variant} onClick={onConfirm}>
{confirmLabel}
</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
export { ConfirmDialog }
组件作用
ConfirmDialog 将常见的“确定要继续吗?”交互封装成一个声明式组件,免去每次需要确认操作时
重复组合 Dialog 和 Button。它适合用于删除等具有破坏性或不可逆的操作。
组件属性
| Prop | Type | Default | Description |
|---|---|---|---|
| trigger * | ReactNode | — | The element that opens the dialog. Rendered inside the trigger via `asChild`. |
| title * | string | — | Heading shown at the top of the dialog. |
| description | string | — | Optional supporting text under the title. |
| confirmLabel | string | Confirm | Label for the confirm button. |
| cancelLabel | string | Cancel | Label for the cancel button. |
| variant | "default" | "destructive" | default | Visual style of the confirm button. |
| onConfirm | (() => void) | — | Called when the user clicks the confirm button. |
事件
| Prop | Type | Default | Description |
|---|---|---|---|
| onConfirm | () => void | — | 用户点击确认按钮时调用。 |
扩展使用
组件源码会直接保存在你的项目中,你可以自由扩展,例如为确认按钮添加异步加载状态、
透传 onOpenChange,或调整底部操作区域的布局。