ActionSheet 动作面板

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

规则

  • 提供清晰的退出按钮。

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

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

代码演示

基本

  1. import { ActionSheet, WingBlank, WhiteSpace, Button, Toast } from 'antd-mobile';
  2. // fix touch to scroll background page on iOS
  3. // https://github.com/ant-design/ant-design-mobile/issues/307
  4. // https://github.com/ant-design/ant-design-mobile/issues/163
  5. const isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent);
  6. let wrapProps;
  7. if (isIPhone) {
  8. wrapProps = {
  9. onTouchStart: e => e.preventDefault(),
  10. };
  11. }
  12. class Test extends React.Component {
  13. constructor() {
  14. super();
  15. this.state = {
  16. clicked: 'none',
  17. clicked1: 'none',
  18. clicked2: 'none',
  19. };
  20. }
  21. dataList = [
  22. { url: 'OpHiXAcYzmPQHcdlLFrc', title: '发送给朋友' },
  23. { url: 'wvEzCMiDZjthhAOcwTOu', title: '新浪微博' },
  24. { url: 'cTTayShKtEIdQVEMuiWt', title: '生活圈' },
  25. { url: 'umnHwvEgSyQtXlZjNJTt', title: '微信好友' },
  26. { url: 'SxpunpETIwdxNjcJamwB', title: 'QQ' },
  27. ].map(obj => ({
  28. icon: <img src={`https://gw.alipayobjects.com/zos/rmsportal/${obj.url}.png`} alt={obj.title} style={{ width: 36 }} />,
  29. title: obj.title,
  30. }));
  31. showActionSheet = () => {
  32. const BUTTONS = ['Operation1', 'Operation2', 'Operation2', 'Delete', 'Cancel'];
  33. ActionSheet.showActionSheetWithOptions({
  34. options: BUTTONS,
  35. cancelButtonIndex: BUTTONS.length - 1,
  36. destructiveButtonIndex: BUTTONS.length - 2,
  37. // title: 'title',
  38. message: 'I am description, description, description',
  39. maskClosable: true,
  40. 'data-seed': 'logId',
  41. wrapProps,
  42. },
  43. (buttonIndex) => {
  44. this.setState({ clicked: BUTTONS[buttonIndex] });
  45. });
  46. }
  47. showShareActionSheet = () => {
  48. ActionSheet.showShareActionSheetWithOptions({
  49. options: this.dataList,
  50. // title: 'title',
  51. message: 'I am description, description, description',
  52. },
  53. (buttonIndex) => {
  54. this.setState({ clicked1: buttonIndex > -1 ? this.dataList[buttonIndex].title : 'cancel' });
  55. // also support Promise
  56. return new Promise((resolve) => {
  57. Toast.info('closed after 1000ms');
  58. setTimeout(resolve, 1000);
  59. });
  60. });
  61. }
  62. showShareActionSheetMulpitleLine = () => {
  63. const data = [[...this.dataList, this.dataList[2]], [this.dataList[3], this.dataList[4]]];
  64. ActionSheet.showShareActionSheetWithOptions({
  65. options: data,
  66. message: 'I am description, description, description',
  67. },
  68. (buttonIndex, rowIndex) => {
  69. this.setState({ clicked2: buttonIndex > -1 ? data[rowIndex][buttonIndex].title : 'cancel' });
  70. });
  71. }
  72. showActionSheetBadge = () => {
  73. const BUTTONS = ['Operation1', 'Operation2', 'Operation3', 'Operation4', 'Operation5', 'Delete', 'Cancel'];
  74. const BADGES = [{
  75. index: 1,
  76. dot: true,
  77. }, {
  78. index: 2,
  79. text: 3100,
  80. }, {
  81. index: 3,
  82. text: '推荐',
  83. }, {
  84. index: 4,
  85. text: '···',
  86. }];
  87. ActionSheet.showActionSheetWithOptions({
  88. options: BUTTONS,
  89. cancelButtonIndex: BUTTONS.length - 1,
  90. destructiveButtonIndex: BUTTONS.length - 2,
  91. badges: BADGES,
  92. // title: 'title',
  93. message: 'I am description, description, description',
  94. maskClosable: true,
  95. 'data-seed': 'logId',
  96. wrapProps,
  97. },
  98. (buttonIndex) => {
  99. this.setState({ clicked: BUTTONS[buttonIndex] });
  100. });
  101. }
  102. render() {
  103. return (<WingBlank>
  104. <Button onClick={this.showActionSheet}>showActionSheet</Button>
  105. <WhiteSpace />
  106. <Button onClick={this.showActionSheetBadge}>showActionSheet&Badge</Button>
  107. <WhiteSpace />
  108. <Button onClick={this.showShareActionSheet}>showShareActionSheet</Button>
  109. <WhiteSpace />
  110. <Button onClick={this.showShareActionSheetMulpitleLine}>showShareActionSheetMulpitleLine</Button>
  111. </WingBlank>);
  112. }
  113. }
  114. ReactDOM.render(<Test />, mountNode);

ActionSheet动作面板 - 图1

API

static showActionSheetWithOptions(options: Object, callback: Function)

显示 action sheet,options对象必须包含以下的一个或者多个:
  • options (array of strings) - 按钮标题列表 (required)

  • badges (array of {…Badges, index: number}) - 徽标数列表

  • cancelButtonIndex (int) - 按钮列表中取消按钮的索引位置

  • destructiveButtonIndex (int) - 按钮列表中破坏性按钮(一般为删除)的索引位置

  • title (string) - 顶部标题

  • message (string/React.element) - 顶部标题下的简要消息

  • maskClosable (bool) - 点击蒙层是否允许关闭,默认允许

callback函数支持返回 Promise

static showShareActionSheetWithOptions(options: Object, callback: Function)

显示分享 action sheet,options对象必须包含以下的一个或者多个:
  • options (array of {icon: ReactNode, title: string}) - 分享按钮列表 (required)

    • 可以是二维数组,能显示多行按钮,例如[[{icon,title},…],…]表示两行两列。当为二维数组时callback有两个参数,第一个为序列、第二个为序列。
  • cancelButtonText (string) - 取消按钮文案,默认为取消

  • title (string) - 顶部标题

  • message (string/React.element) - 顶部标题下的简要消息

  • maskClosable (bool) - 点击蒙层是否允许关闭,默认允许

callback函数支持返回 Promise

static close() - programmatically close.