OneScript

alert

显示提示框。

alert(options) 显示系统提示框。点击任意按钮后,先执行该按钮的 action,再调用 onDismiss。

import { Button, Modifier, Text, TextField, useState } from "onescript/views"

function FileRow() {
  const [deleting, setDeleting] = useState(false)
  const [renaming, setRenaming] = useState(false)
  const [name, setName] = useState("notes.txt")

  return (
    <Text modifier={Modifier
      .alert({
        title: "删除这个文件?",
        message: "此操作无法撤销。",
        isPresented: deleting,
        onDismiss: () => setDeleting(false),
        actions: [
          <Button
            role="destructive"
            action={() => remove()}
          >
            删除
          </Button>,
          <Button
            role="cancel"
            action={() => {}}
          >
            取消
          </Button>,
        ],
      })
      .alert({
        title: "重命名",
        isPresented: renaming,
        onDismiss: () => setRenaming(false),
        actions: [
          <TextField
            value={name}
            onChange={setName}
            prompt="文件名"
          />,
          <Button action={() => rename(name)}>保存</Button>,
          <Button
            role="cancel"
            action={() => {}}
          >
            取消
          </Button>,
        ],
      })}>
      {name}
    </Text>
  )
}
参数类型说明
titlestring标题。
messagestring可选。标题下方的说明文字。
isPresentedboolean是否显示。
onDismiss() => void提示框关闭时调用,需在其中把 isPresented 设为 false。
actionsViewChild按钮与输入框。不传按钮时系统显示一个"好"按钮。
  • actions 只接受标签为纯文本的 Button 和 TextField,且它们不能带 modifier。
  • 用 role 区分按钮:"cancel" 为取消按钮,"destructive" 以红色显示。

How is this guide?

最后更新于