Files
what-to-eat/apps/web/app/utils/dialog.ts
T

48 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { actionSheetController, alertController } from '@ionic/vue'
import { yunleHaptic } from './yunleHost'
/**
* iOS 风格的弹窗:替代 @capacitor/dialog(Web 上会退化成 window.alert / confirm / prompt)
*/
export async function showAlert(header: string, message: string): Promise<void> {
const alert = await alertController.create({ header, message, buttons: ['好'] })
await alert.present()
await alert.onDidDismiss()
}
/**
* 破坏性操作的确认:底部操作表,确认项为红色
*/
export async function confirmDestructive(options: { header: string, confirmText: string }): Promise<boolean> {
void yunleHaptic('medium')
const sheet = await actionSheetController.create({
header: options.header,
buttons: [
{ text: options.confirmText, role: 'destructive' },
{ text: '取消', role: 'cancel' },
],
})
await sheet.present()
const { role } = await sheet.onDidDismiss()
return role === 'destructive'
}
/**
* 多行文本输入;取消时返回 undefined
*/
export async function promptText(options: { header: string, message?: string, placeholder?: string, okText: string }): Promise<string | undefined> {
const alert = await alertController.create({
header: options.header,
message: options.message,
inputs: [{ name: 'value', type: 'textarea', placeholder: options.placeholder }],
buttons: [
{ text: '取消', role: 'cancel' },
{ text: options.okText, role: 'confirm' },
],
})
await alert.present()
const { role, data } = await alert.onDidDismiss<{ values: { value?: string } }>()
return role === 'confirm' ? data?.values.value ?? '' : undefined
}