ActionSheet.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Taro from "@tarojs/taro";
  2. export default function showActionSheet(props: any) {
  3. if (process.env.TARO_ENV == 'weapp') {
  4. Taro.showActionSheet({
  5. alertText:props.title??'',
  6. itemList: props.itemList,
  7. success: function (res) {
  8. props.success(res.tapIndex)
  9. },
  10. fail: function (res) {
  11. if (props.fail)
  12. props.fail(res)
  13. }
  14. })
  15. }
  16. else {
  17. const { showActionSheetWithOptions } = props.showActionSheetWithOptions;//useActionSheet();
  18. var list = JSON.parse(JSON.stringify(props.itemList));
  19. list.push('Cancel');
  20. let options = {
  21. options: list,
  22. cancelButtonIndex: list.length - 1,
  23. // destructiveButtonIndex: 0,
  24. title: props.title,
  25. message: props.message,
  26. };
  27. var destructiveButtonIndex = -1;
  28. list.map((item, index) => {
  29. if (item == 'Delete' || item == '删除'|| item == 'Delete Account'|| item == '注销账号')
  30. destructiveButtonIndex = index
  31. });
  32. (options as any).destructiveButtonIndex = destructiveButtonIndex
  33. showActionSheetWithOptions(options, (selectedIndex: number) => {
  34. if (selectedIndex < list.length - 1)
  35. props.success(selectedIndex)
  36. });
  37. }
  38. }