ActionSheet 动作面板
从底部弹出的模态框,提供和当前场景相关的 2 个以上的操作动作,也支持提供标题和描述。内置固定的展示样式、不支持特别灵活的修改。规则
提供清晰的退出按钮。
可高亮破坏性操作,e.g. 将『删除』处理成红色文本。
不要放置过多内容,避免面板纵向滚动。
代码演示
/* eslint global-require: 0 */
import { ActionSheet, Button, Toast, Icon } from 'antd-mobile';
// fix touch to scroll background page on iOS
// https://github.com/ant-design/ant-design-mobile/issues/307
// https://github.com/ant-design/ant-design-mobile/issues/163
const isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent);
let wrapProps;
if (isIPhone) {
wrapProps = {
onTouchStart: e => e.preventDefault(),
};
}
const iconList = [
{ icon: <img src="https://zos.alipayobjects.com/rmsportal/WmEzpOsElbbvgmrexFSH.png" alt="icon" />, title: '发送给朋友' },
{ icon: <img src="https://zos.alipayobjects.com/rmsportal/HssPJKvrjEByyVWJIFwl.png" alt="icon" />, title: '新浪微博' },
{ icon: <img src="https://zos.alipayobjects.com/rmsportal/HCGowLrLFMFglxRAKjWd.png" alt="icon" />, title: '生活圈' },
{ icon: <img src="https://zos.alipayobjects.com/rmsportal/LeZNKxCTkLHDWsjFfqqn.png" alt="icon" />, title: '微信好友' },
{ icon: <img src="https://zos.alipayobjects.com/rmsportal/YHHFcpGxlvQIqCAvZdbw.png" alt="icon" />, title: 'QQ' },
{ icon: <Icon type={require('./refresh.svg')} />, title: '刷新' },
{ icon: <Icon type={require('./link.svg')} />, title: '链接' },
{ icon: <Icon type={require('./complaints.svg')} />, title: '投诉' },
];
class Test extends React.Component {
constructor() {
super();
this.state = {
clicked: 'none',
clicked1: 'none',
clicked2: 'none',
};
}
showActionSheet = () => {
const BUTTONS = ['操作一', '操作二', '操作三', '删除', '取消'];
ActionSheet.showActionSheetWithOptions({
options: BUTTONS,
cancelButtonIndex: BUTTONS.length - 1,
destructiveButtonIndex: BUTTONS.length - 2,
// title: '标题',
message: '我是描述我是描述',
maskClosable: true,
'data-seed': 'logId',
wrapProps,
},
(buttonIndex) => {
this.setState({ clicked: BUTTONS[buttonIndex] });
});
}
showShareActionSheet = () => {
const icons = [...iconList];
icons.length = 4;
ActionSheet.showShareActionSheetWithOptions({
options: icons,
// title: '标题',
message: '我是描述我是描述',
className: 'my-action-sheet',
},
(buttonIndex) => {
this.setState({ clicked1: buttonIndex > -1 ? icons[buttonIndex].title : 'cancel' });
// also support Promise
return new Promise((resolve) => {
Toast.info('1000ms 后关闭');
setTimeout(resolve, 1000);
});
});
}
showShareActionSheetMulpitleLine = () => {
const icons = [[...iconList], [iconList[5], iconList[6], iconList[7]]];
ActionSheet.showShareActionSheetWithOptions({
options: icons,
// title: '标题',
message: '我是描述我是描述',
className: 'my-action-sheet',
},
(buttonIndex, rowIndex) => {
this.setState({ clicked2: buttonIndex > -1 ? icons[rowIndex][buttonIndex].title : 'cancel' });
});
}
render() {
return (<div className="actionSheetContainer">
<div style={{ margin: '0.15rem 0' }}>
<Button onClick={this.showActionSheet}>默认状态</Button>
</div>
<div style={{ margin: '0.15rem 0' }}>
<Button onClick={this.showShareActionSheet}>分享功能</Button>
</div>
<div style={{ margin: '0.15rem 0' }}>
<Button onClick={this.showShareActionSheetMulpitleLine}>带多行按钮的分享功能</Button>
</div>
</div>);
}
}
ReactDOM.render(<Test />, mountNode);
.actionSheetContainer {
margin: 0 0.3rem;
}
.my-action-sheet .am-action-sheet-share-list-item-icon img {
width: 0.72rem;
}
API
适用平台:WEB、React-Nativestatic showActionSheetWithOptions(options: Object, callback: Function)
显示 action sheet,options
对象必须包含以下的一个或者多个:
options (array of strings) - 按钮标题列表 (required)
cancelButtonIndex (int) - 按钮列表中取消按钮的索引位置
destructiveButtonIndex (int) - 按钮列表中破坏性按钮(一般为删除)的索引位置
title (string) - 顶部标题
message (string/React.element) - 顶部标题下的简要消息
maskClosable (bool)(
web only
) - 点击蒙层是否允许关闭,默认允许
callback
函数支持返回 Promise (web only
)
static showShareActionSheetWithOptions(options: Object, callback: Function)
显示分享 action sheet,options
对象必须包含以下的一个或者多个:
options (array of
{icon:React.node, iconName:string, title:string}
) - 分享按钮列表 (required)注意:
iconName
为 Icon 组件里的某个 type 值,它会覆盖icon
属性设置 (icon
属性用于设置自定义内容)可以是二维数组,能显示多行按钮,例如
[[{icon,title},{icon,title}], [{icon,title},{icon,title}]]
表示两行两列。当为二维数组时callback
有两个参数,第一个为列
序列、第二个为行
序列。
cancelButtonText (string)(
web only
) - 取消按钮文案,默认为取消
title (string) - 顶部标题
message (string/React.element) - 顶部标题下的简要消息
maskClosable (bool)(
web only
) - 点击蒙层是否允许关闭,默认允许
callback
函数支持返回 Promise (web only
)