DatePickerAndroid.android.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * @format
  8. * @flow strict-local
  9. */
  10. 'use strict';
  11. import type {Options, DatePickerOpenAction} from './DatePickerAndroidTypes';
  12. import NativeDatePickerAndroid from './NativeDatePickerAndroid';
  13. /**
  14. * Convert a Date to a timestamp.
  15. */
  16. function _toMillis(options: Options, key: string) {
  17. const dateVal = options[key];
  18. // Is it a Date object?
  19. if (typeof dateVal === 'object' && typeof dateVal.getMonth === 'function') {
  20. options[key] = dateVal.getTime();
  21. }
  22. }
  23. /**
  24. * Opens the standard Android date picker dialog.
  25. *
  26. * ### Example
  27. *
  28. * ```
  29. * try {
  30. * const {action, year, month, day} = await DatePickerAndroid.open({
  31. * // Use `new Date()` for current date.
  32. * // May 25 2020. Month 0 is January.
  33. * date: new Date(2020, 4, 25)
  34. * });
  35. * if (action !== DatePickerAndroid.dismissedAction) {
  36. * // Selected year, month (0-11), day
  37. * }
  38. * } catch ({code, message}) {
  39. * console.warn('Cannot open date picker', message);
  40. * }
  41. * ```
  42. */
  43. class DatePickerAndroid {
  44. /**
  45. * Opens the standard Android date picker dialog.
  46. *
  47. * The available keys for the `options` object are:
  48. *
  49. * - `date` (`Date` object or timestamp in milliseconds) - date to show by default
  50. * - `minDate` (`Date` or timestamp in milliseconds) - minimum date that can be selected
  51. * - `maxDate` (`Date` object or timestamp in milliseconds) - maximum date that can be selected
  52. * - `mode` (`enum('calendar', 'spinner', 'default')`) - To set the date-picker mode to calendar/spinner/default
  53. * - 'calendar': Show a date picker in calendar mode.
  54. * - 'spinner': Show a date picker in spinner mode.
  55. * - 'default': Show a default native date picker(spinner/calendar) based on android versions.
  56. *
  57. * Returns a Promise which will be invoked an object containing `action`, `year`, `month` (0-11),
  58. * `day` if the user picked a date. If the user dismissed the dialog, the Promise will
  59. * still be resolved with action being `DatePickerAndroid.dismissedAction` and all the other keys
  60. * being undefined. **Always** check whether the `action` before reading the values.
  61. *
  62. * Note the native date picker dialog has some UI glitches on Android 4 and lower
  63. * when using the `minDate` and `maxDate` options.
  64. */
  65. static async open(options: ?Options): Promise<DatePickerOpenAction> {
  66. const optionsMs = options;
  67. if (optionsMs != null) {
  68. _toMillis(optionsMs, 'date');
  69. _toMillis(optionsMs, 'minDate');
  70. _toMillis(optionsMs, 'maxDate');
  71. }
  72. return NativeDatePickerAndroid.open(options);
  73. }
  74. /**
  75. * A date has been selected.
  76. */
  77. static +dateSetAction: 'dateSetAction' = 'dateSetAction';
  78. /**
  79. * The dialog has been dismissed.
  80. */
  81. static +dismissedAction: 'dismissedAction' = 'dismissedAction';
  82. }
  83. module.exports = DatePickerAndroid;