ActionSheet 动作面板

从底部弹出的模态框,提供和当前场景相关的 2 个以上的操作动作,也支持提供标题和描述。内置固定的展示样式、不支持特别灵活的修改。

规则

  • 提供清晰的退出按钮。

  • 可高亮破坏性操作,e.g. 将『删除』处理成红色文本。

  • 不要放置过多内容,避免面板纵向滚动。

代码演示

基本

  1. /* eslint global-require: 0 */
  2. import { ActionSheet, Button, Toast, Icon } from 'antd-mobile';
  3. // fix touch to scroll background page on iOS
  4. // https://github.com/ant-design/ant-design-mobile/issues/307
  5. // https://github.com/ant-design/ant-design-mobile/issues/163
  6. const isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent);
  7. let wrapProps;
  8. if (isIPhone) {
  9. wrapProps = {
  10. onTouchStart: e => e.preventDefault(),
  11. };
  12. }
  13. const iconList = [
  14. { icon: <img src="https://zos.alipayobjects.com/rmsportal/WmEzpOsElbbvgmrexFSH.png" alt="icon" />, title: '发送给朋友' },
  15. { icon: <img src="https://zos.alipayobjects.com/rmsportal/HssPJKvrjEByyVWJIFwl.png" alt="icon" />, title: '新浪微博' },
  16. { icon: <img src="https://zos.alipayobjects.com/rmsportal/HCGowLrLFMFglxRAKjWd.png" alt="icon" />, title: '生活圈' },
  17. { icon: <img src="https://zos.alipayobjects.com/rmsportal/LeZNKxCTkLHDWsjFfqqn.png" alt="icon" />, title: '微信好友' },
  18. { icon: <img src="https://zos.alipayobjects.com/rmsportal/YHHFcpGxlvQIqCAvZdbw.png" alt="icon" />, title: 'QQ' },
  19. { icon: <Icon type={require('./refresh.svg')} />, title: '刷新' },
  20. { icon: <Icon type={require('./link.svg')} />, title: '链接' },
  21. { icon: <Icon type={require('./complaints.svg')} />, title: '投诉' },
  22. ];
  23. class Test extends React.Component {
  24. constructor() {
  25. super();
  26. this.state = {
  27. clicked: 'none',
  28. clicked1: 'none',
  29. clicked2: 'none',
  30. };
  31. }
  32. showActionSheet = () => {
  33. const BUTTONS = ['操作一', '操作二', '操作三', '删除', '取消'];
  34. ActionSheet.showActionSheetWithOptions({
  35. options: BUTTONS,
  36. cancelButtonIndex: BUTTONS.length - 1,
  37. destructiveButtonIndex: BUTTONS.length - 2,
  38. // title: '标题',
  39. message: '我是描述我是描述',
  40. maskClosable: true,
  41. 'data-seed': 'logId',
  42. wrapProps,
  43. },
  44. (buttonIndex) => {
  45. this.setState({ clicked: BUTTONS[buttonIndex] });
  46. });
  47. }
  48. showShareActionSheet = () => {
  49. const icons = [...iconList];
  50. icons.length = 4;
  51. ActionSheet.showShareActionSheetWithOptions({
  52. options: icons,
  53. // title: '标题',
  54. message: '我是描述我是描述',
  55. className: 'my-action-sheet',
  56. },
  57. (buttonIndex) => {
  58. this.setState({ clicked1: buttonIndex > -1 ? icons[buttonIndex].title : 'cancel' });
  59. // also support Promise
  60. return new Promise((resolve) => {
  61. Toast.info('1000ms 后关闭');
  62. setTimeout(resolve, 1000);
  63. });
  64. });
  65. }
  66. showShareActionSheetMulpitleLine = () => {
  67. const icons = [[...iconList], [iconList[5], iconList[6], iconList[7]]];
  68. ActionSheet.showShareActionSheetWithOptions({
  69. options: icons,
  70. // title: '标题',
  71. message: '我是描述我是描述',
  72. className: 'my-action-sheet',
  73. },
  74. (buttonIndex, rowIndex) => {
  75. this.setState({ clicked2: buttonIndex > -1 ? icons[rowIndex][buttonIndex].title : 'cancel' });
  76. });
  77. }
  78. render() {
  79. return (<div className="actionSheetContainer">
  80. <div style={{ margin: '0.15rem 0' }}>
  81. <Button onClick={this.showActionSheet}>默认状态</Button>
  82. </div>
  83. <div style={{ margin: '0.15rem 0' }}>
  84. <Button onClick={this.showShareActionSheet}>分享功能</Button>
  85. </div>
  86. <div style={{ margin: '0.15rem 0' }}>
  87. <Button onClick={this.showShareActionSheetMulpitleLine}>带多行按钮的分享功能</Button>
  88. </div>
  89. </div>);
  90. }
  91. }
  92. ReactDOM.render(<Test />, mountNode);
  1. .actionSheetContainer {
  2. margin: 0 0.3rem;
  3. }
  4. .my-action-sheet .am-action-sheet-share-list-item-icon img {
  5. width: 0.72rem;
  6. }

ActionSheet动作面板 - 图1

API

适用平台:WEB、React-Native

static 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)

    • 注意:iconNameIcon 组件里的某个 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)

static close() - (web、android only) programmatically close.