组件

ActionSheet

ActionSheet.show 展示一组操作:iOS 上是操作表(UIAlertController),Android 上是列表对话框。它返回一个 Promise,解析为所选操作的序号。

import { ActionSheet } from "nsh/ui/mobile";

#用法

import { mount, signal, Text, View } from "nsh";
import { ActionSheet, Button } from "nsh/ui/mobile";

function App() {
  const chosen = signal("–");
  const share = async () => {
    const options = ["Cancel", "Copy link", "Save", "Delete"];
    const i = await ActionSheet.show({
      title: "Share the photo",
      options,
      cancelButtonIndex: 0,
      destructiveButtonIndexes: [3],
    });
    chosen.set(options[i] ?? "dismissed");
  };
  return (
    <View style={{ flex: 1, padding: 16, paddingTop: 48, gap: 12 }}>
      <Button title="Share…" onPress={share} />
      <Text>{chosen()}</Text>
    </View>
  );
}

mount(App);

#API

ActionSheet.show(options: ActionSheetOptions): Promise<number>

Promise 解析为所选操作在 options.options 中的序号。如果用户没有选择就关闭了操作表(点击外部、按返回键),则解析为 cancelButtonIndex;没有取消操作时解析为 -1。它不会被 reject。

#ActionSheetOptions

字段类型默认值说明
optionsstring[]必填各操作的文字,按顺序排列。
cancelButtonIndexnumber | null无取消操作的序号。
destructiveButtonIndexesnumber[] | null无破坏性操作的序号(iOS 上显示为红色)。
titlestring | null无显示在操作上方。
messagestring | null无显示在标题下方。

#平台说明

  • iOS:取消操作单独显示在操作表底部。在 iPad 和 Mac Catalyst 上操作表以 popover 形式出现,取消操作会被隐藏:点击 popover 外部即可关闭,Promise 解析为 cancelButtonIndex(或 -1)。
  • Android:操作以列表形式显示在对话框中。取消操作不出现在列表里,点击外部或按返回键即为取消。列表对话框无法显示 message:没有 title 时,message 会作为标题显示;有 title 时 message 不显示。破坏性操作与其他操作外观相同。

#相关内容

  • Alert:最多三个按钮的对话框。
  • Sheet:放置自定义内容的底部面板,两个平台外观一致。
  • SelectList:从众多选项中选择一个。