useActionSheet
显示操作菜单
何时使用
当需要使用操作菜单
API
const { tapItem, show } = useActionSheet(initialOption);
参数说明
initialOption: ActionSheetOption
初始操作菜单配置(若指定后面可与新的配置合并)
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
alertText | 警示文案 | string | - |
itemList | 按钮的文字数组,数组长度最大为 6 | string[] | - |
itemColor | 按钮的文字颜色 | string | - |
返回值说明
返回值 | 说明 | 类型 |
---|---|---|
show | 显示操作菜单 | PromiseOptionalAction<ActionSheetOption, ActionSheetTapItem> |
tapItem | 当前选中数据 | ActionSheetTapItem |
tapItem
返回值 | 说明 | 类型 |
---|---|---|
tapIndex | 用户点击的按钮序号,从上到下的顺序,从 0 开始 | number |
tapItem | 用户点击的按钮序号对应 itemList 的字符串 | string |
代码演示
- React
- Vue
feedback/useActionSheet/index
import React from 'react';
import { useActionSheet, useToast } from 'taro-hooks';
import { escapeState } from '@taro-hooks/shared';
import { useEffect } from '@taro-hooks/core';
import DemoContent from '@src/components/DemoContent';
import { Button } from '@taroify/core';
export default () => {
const { show, tapItem } = useActionSheet({ itemList: ['A', 'B', 'C'] });
const { show: showToast } = useToast({ duration: 2000, mask: true });
useEffect(() => {
if (escapeState(tapItem)) {
const { tapIndex, tapItem: item } = escapeState(tapItem);
showToast({ title: '${tapIndex}/${item}' });
}
}, [tapItem]);
const handleChangeOption = () => {
show({
itemColor: '#C5D9E8',
itemList: ['taro', 'hooks', 'taro-hooks'],
});
};
return (
<DemoContent>
<Button
block
color="primary"
className="gap"
onClick={() => show()}
shape="square"
>
展示带初始配置的ActionSheet
</Button>
<Button
block
color="primary"
className="gap"
onClick={handleChangeOption}
shape="square"
>
展示新配置的ActionSheet
</Button>
</DemoContent>
);
};
feedback/useActionSheet/index
<template>
<demo-content>
<nut-button shape="square" type="primary" class="gap" block @click="show()"
>展示带初始配置的ActionSheet</nut-button
>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="handleChangeOption()"
>展示新配置的ActionSheet</nut-button
>
</demo-content>
</template>
<script setup lang="ts">
import { useActionSheet, useToast } from 'taro-hooks';
import { escapeState } from '@taro-hooks/shared';
import { useEffect } from '@taro-hooks/core';
const { show, tapItem } = useActionSheet({ itemList: ['A', 'B', 'C'] });
const { show: showToast } = useToast({ duration: 2000, mask: true });
useEffect(() => {
if (escapeState(tapItem)) {
const { tapIndex, tapItem: item } = escapeState(tapItem);
showToast({ title: '${tapIndex}/${item}' });
}
}, [tapItem]);
const handleChangeOption = () => {
show({
itemColor: '#C5D9E8',
itemList: ['taro', 'hooks', 'taro-hooks'],
});
};
</script>
Hook 支持度
微信小程序 | H5 | ReactNative |
---|---|---|
✔️ | ✔️ | ✔️ |