ActionSheetIOS.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow
  8. * @format
  9. */
  10. 'use strict';
  11. import RCTActionSheetManager from './NativeActionSheetManager';
  12. const invariant = require('invariant');
  13. const processColor = require('../StyleSheet/processColor');
  14. import type {ColorValue} from '../StyleSheet/StyleSheetTypes';
  15. import type {ProcessedColorValue} from '../StyleSheet/processColor';
  16. /**
  17. * Display action sheets and share sheets on iOS.
  18. *
  19. * See https://reactnative.dev/docs/actionsheetios.html
  20. */
  21. const ActionSheetIOS = {
  22. /**
  23. * Display an iOS action sheet.
  24. *
  25. * The `options` object must contain one or more of:
  26. *
  27. * - `options` (array of strings) - a list of button titles (required)
  28. * - `cancelButtonIndex` (int) - index of cancel button in `options`
  29. * - `destructiveButtonIndex` (int or array of ints) - index or indices of destructive buttons in `options`
  30. * - `title` (string) - a title to show above the action sheet
  31. * - `message` (string) - a message to show below the title
  32. *
  33. * The 'callback' function takes one parameter, the zero-based index
  34. * of the selected item.
  35. *
  36. * See https://reactnative.dev/docs/actionsheetios.html#showactionsheetwithoptions
  37. */
  38. showActionSheetWithOptions(
  39. options: {|
  40. +title?: ?string,
  41. +message?: ?string,
  42. +options: Array<string>,
  43. +destructiveButtonIndex?: ?number | ?Array<number>,
  44. +cancelButtonIndex?: ?number,
  45. +anchor?: ?number,
  46. +tintColor?: ColorValue | ProcessedColorValue,
  47. +userInterfaceStyle?: string,
  48. |},
  49. callback: (buttonIndex: number) => void,
  50. ) {
  51. invariant(
  52. typeof options === 'object' && options !== null,
  53. 'Options must be a valid object',
  54. );
  55. invariant(typeof callback === 'function', 'Must provide a valid callback');
  56. invariant(RCTActionSheetManager, "ActionSheetManager does't exist");
  57. const {tintColor, destructiveButtonIndex, ...remainingOptions} = options;
  58. let destructiveButtonIndices = null;
  59. if (Array.isArray(destructiveButtonIndex)) {
  60. destructiveButtonIndices = destructiveButtonIndex;
  61. } else if (typeof destructiveButtonIndex === 'number') {
  62. destructiveButtonIndices = [destructiveButtonIndex];
  63. }
  64. const processedTintColor = processColor(tintColor);
  65. invariant(
  66. processedTintColor == null || typeof processedTintColor === 'number',
  67. 'Unexpected color given for ActionSheetIOS.showActionSheetWithOptions tintColor',
  68. );
  69. RCTActionSheetManager.showActionSheetWithOptions(
  70. {
  71. ...remainingOptions,
  72. tintColor: processedTintColor,
  73. destructiveButtonIndices,
  74. },
  75. callback,
  76. );
  77. },
  78. /**
  79. * Display the iOS share sheet. The `options` object should contain
  80. * one or both of `message` and `url` and can additionally have
  81. * a `subject` or `excludedActivityTypes`:
  82. *
  83. * - `url` (string) - a URL to share
  84. * - `message` (string) - a message to share
  85. * - `subject` (string) - a subject for the message
  86. * - `excludedActivityTypes` (array) - the activities to exclude from
  87. * the ActionSheet
  88. * - `tintColor` (color) - tint color of the buttons
  89. *
  90. * The 'failureCallback' function takes one parameter, an error object.
  91. * The only property defined on this object is an optional `stack` property
  92. * of type `string`.
  93. *
  94. * The 'successCallback' function takes two parameters:
  95. *
  96. * - a boolean value signifying success or failure
  97. * - a string that, in the case of success, indicates the method of sharing
  98. *
  99. * See https://reactnative.dev/docs/actionsheetios.html#showshareactionsheetwithoptions
  100. */
  101. showShareActionSheetWithOptions(
  102. options: Object,
  103. failureCallback: Function,
  104. successCallback: Function,
  105. ) {
  106. invariant(
  107. typeof options === 'object' && options !== null,
  108. 'Options must be a valid object',
  109. );
  110. invariant(
  111. typeof failureCallback === 'function',
  112. 'Must provide a valid failureCallback',
  113. );
  114. invariant(
  115. typeof successCallback === 'function',
  116. 'Must provide a valid successCallback',
  117. );
  118. invariant(RCTActionSheetManager, "ActionSheetManager does't exist");
  119. RCTActionSheetManager.showShareActionSheetWithOptions(
  120. {...options, tintColor: processColor(options.tintColor)},
  121. failureCallback,
  122. successCallback,
  123. );
  124. },
  125. };
  126. module.exports = ActionSheetIOS;