ActionSheet 动作面板
从底部弹出的模态框,提供和当前场景相关的 2 个以上的操作动作,也支持提供标题和描述。内置固定的展示样式、不支持特别灵活的修改。规则
提供清晰的退出按钮。
可高亮破坏性操作,e.g. 将『删除』处理成红色文本。
不要放置过多内容,避免面板纵向滚动。
代码演示
import { ActionSheet, WingBlank, WhiteSpace, Button, Toast } 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(),
};
}
class Test extends React.Component {
constructor() {
super();
this.state = {
clicked: 'none',
clicked1: 'none',
clicked2: 'none',
};
}
dataList = [
{ url: 'OpHiXAcYzmPQHcdlLFrc', title: '发送给朋友' },
{ url: 'wvEzCMiDZjthhAOcwTOu', title: '新浪微博' },
{ url: 'cTTayShKtEIdQVEMuiWt', title: '生活圈' },
{ url: 'umnHwvEgSyQtXlZjNJTt', title: '微信好友' },
{ url: 'SxpunpETIwdxNjcJamwB', title: 'QQ' },
].map(obj => ({
icon: <img src={`https://gw.alipayobjects.com/zos/rmsportal/${obj.url}.png`} alt={obj.title} style={{ width: 36 }} />,
title: obj.title,
}));
showActionSheet = () => {
const BUTTONS = ['Operation1', 'Operation2', 'Operation2', 'Delete', 'Cancel'];
ActionSheet.showActionSheetWithOptions({
options: BUTTONS,
cancelButtonIndex: BUTTONS.length - 1,
destructiveButtonIndex: BUTTONS.length - 2,
// title: 'title',
message: 'I am description, description, description',
maskClosable: true,
'data-seed': 'logId',
wrapProps,
},
(buttonIndex) => {
this.setState({ clicked: BUTTONS[buttonIndex] });
});
}
showShareActionSheet = () => {
ActionSheet.showShareActionSheetWithOptions({
options: this.dataList,
// title: 'title',
message: 'I am description, description, description',
},
(buttonIndex) => {
this.setState({ clicked1: buttonIndex > -1 ? this.dataList[buttonIndex].title : 'cancel' });
// also support Promise
return new Promise((resolve) => {
Toast.info('closed after 1000ms');
setTimeout(resolve, 1000);
});
});
}
showShareActionSheetMulpitleLine = () => {
const data = [[...this.dataList, this.dataList[2]], [this.dataList[3], this.dataList[4]]];
ActionSheet.showShareActionSheetWithOptions({
options: data,
message: 'I am description, description, description',
},
(buttonIndex, rowIndex) => {
this.setState({ clicked2: buttonIndex > -1 ? data[rowIndex][buttonIndex].title : 'cancel' });
});
}
showActionSheetBadge = () => {
const BUTTONS = ['Operation1', 'Operation2', 'Operation3', 'Operation4', 'Operation5', 'Delete', 'Cancel'];
const BADGES = [{
index: 1,
dot: true,
}, {
index: 2,
text: 3100,
}, {
index: 3,
text: '推荐',
}, {
index: 4,
text: '···',
}];
ActionSheet.showActionSheetWithOptions({
options: BUTTONS,
cancelButtonIndex: BUTTONS.length - 1,
destructiveButtonIndex: BUTTONS.length - 2,
badges: BADGES,
// title: 'title',
message: 'I am description, description, description',
maskClosable: true,
'data-seed': 'logId',
wrapProps,
},
(buttonIndex) => {
this.setState({ clicked: BUTTONS[buttonIndex] });
});
}
render() {
return (<WingBlank>
<Button onClick={this.showActionSheet}>showActionSheet</Button>
<WhiteSpace />
<Button onClick={this.showActionSheetBadge}>showActionSheet&Badge</Button>
<WhiteSpace />
<Button onClick={this.showShareActionSheet}>showShareActionSheet</Button>
<WhiteSpace />
<Button onClick={this.showShareActionSheetMulpitleLine}>showShareActionSheetMulpitleLine</Button>
</WingBlank>);
}
}
ReactDOM.render(<Test />, mountNode);
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