Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

ConfirmationDialog

用于呈现确认提示的 SwiftUI ConfirmationDialog 组件。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.1

Expo UI ConfirmationDialog 与官方 SwiftUI confirmationDialog API 相对应,可呈现类似操作表的对话框,其中包含标题、操作选项和可选消息。

ConfirmationDialog 询问用户是否确认执行破坏性操作ConfirmationDialog 询问用户是否确认执行破坏性操作

安装

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基本确认对话框

使用 ConfirmationDialog.Trigger 定义可见元素,并使用 ConfirmationDialog.Actions 提供对话框按钮。

BasicConfirmationDialogExample.tsx
import { useState } from 'react'; import { Host, ConfirmationDialog, Button, } from '@expo/ui/swift-ui'; export default function BasicConfirmationDialogExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <ConfirmationDialog title="Are you sure?" isPresented={isPresented} onIsPresentedChange={setIsPresented} titleVisibility="visible"> <ConfirmationDialog.Trigger> <Button label="Show dialog" onPress={() => setIsPresented(true)} /> </ConfirmationDialog.Trigger> <ConfirmationDialog.Actions> <Button label="Confirm" onPress={() => setIsPresented(false)} /> <Button label="Cancel" role="cancel" /> </ConfirmationDialog.Actions> </ConfirmationDialog> </Host> ); }

破坏性操作确认

在 ConfirmationDialog.Actions 内的 Button 上使用 role="destructive",将其设置为破坏性操作样式。

DestructiveConfirmationDialogExample.tsx
import { useState } from 'react'; import { Host, ConfirmationDialog, Button, Text, } from '@expo/ui/swift-ui'; export default function DestructiveConfirmationDialogExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <ConfirmationDialog title="Delete Item?" isPresented={isPresented} onIsPresentedChange={setIsPresented} titleVisibility="visible"> <ConfirmationDialog.Trigger> <Button label="Delete" role="destructive" onPress={() => setIsPresented(true)} /> </ConfirmationDialog.Trigger> <ConfirmationDialog.Actions> <Button label="Delete" role="destructive" onPress={() => { console.log('Deleted'); setIsPresented(false); }} /> <Button label="Cancel" role="cancel" /> </ConfirmationDialog.Actions> <ConfirmationDialog.Message> <Text>This action cannot be undone.</Text> </ConfirmationDialog.Message> </ConfirmationDialog> </Host> ); }

添加消息和多个操作选项

使用 ConfirmationDialog.Message 在标题下方显示描述性消息,并添加多个操作按钮以提供不同选项。

MultiActionConfirmationDialogExample.tsx
import { useState } from 'react'; import { Host, ConfirmationDialog, Button, Text, } from '@expo/ui/swift-ui'; export default function MultiActionConfirmationDialogExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <ConfirmationDialog title="Save Changes?" isPresented={isPresented} onIsPresentedChange={setIsPresented} titleVisibility="visible"> <ConfirmationDialog.Trigger> <Button label="Close document" onPress={() => setIsPresented(true)} /> </ConfirmationDialog.Trigger> <ConfirmationDialog.Actions> <Button label="Save" onPress={() => console.log('Saved')} /> <Button label="Discard" role="destructive" onPress={() => console.log('Discarded')} /> <Button label="Cancel" role="cancel" /> </ConfirmationDialog.Actions> <ConfirmationDialog.Message> <Text> You have unsaved changes. What would you like to do? </Text> </ConfirmationDialog.Message> </ConfirmationDialog> </Host> ); }

隐藏标题

设置 titleVisibility="hidden" 可隐藏对话框标题,同时仍显示操作选项和消息。出于无障碍考虑,仍应提供 title。

HiddenTitleConfirmationDialogExample.tsx
import { useState } from 'react'; import { Host, ConfirmationDialog, Button, Text, } from '@expo/ui/swift-ui'; export default function HiddenTitleConfirmationDialogExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <ConfirmationDialog title="Hidden Title" isPresented={isPresented} onIsPresentedChange={setIsPresented} titleVisibility="hidden"> <ConfirmationDialog.Trigger> <Button label="Show dialog" onPress={() => setIsPresented(true)} /> </ConfirmationDialog.Trigger> <ConfirmationDialog.Actions> <Button label="OK" onPress={() => setIsPresented(false)} /> <Button label="Cancel" role="cancel" /> </ConfirmationDialog.Actions> <ConfirmationDialog.Message> <Text>Only the message and actions are visible.</Text> </ConfirmationDialog.Message> </ConfirmationDialog> </Host> ); }

API

import { ConfirmationDialog } from '@expo/ui/swift-ui';

Component

ConfirmationDialog

iOS
tvOS

Type: React.Element<ConfirmationDialogProps>

ConfirmationDialog presents a confirmation dialog with a title, optional message, and action buttons.

Props of the ConfirmationDialog component.

ConfirmationDialogProps

children

iOS
tvOS
Type: ReactNode

The contents of the confirmation dialog. Should include ConfirmationDialog.Trigger, ConfirmationDialog.Actions, and optionally ConfirmationDialog.Message.

isPresented

iOS
tvOS
Optional • Type: boolean

Whether the confirmation dialog is presented.

onIsPresentedChange

iOS
tvOS
Optional • Type: (isPresented: boolean) => void

A callback that is called when the isPresented state changes.

title

iOS
tvOS
Type: string

The title of the confirmation dialog.

titleVisibility

iOS
tvOS
Optional • Literal type: string • Default: 'automatic'

The visibility of the dialog title.

Acceptable values are: 'automatic' | 'visible' | 'hidden'