mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 09:35:48 +00:00
a5dea8d612
Summary: Thanks for submitting a PR! Please read these instructions carefully: - [ ] Explain the **motivation** for making this change. - [ ] Provide a **test plan** demonstrating that the code is solid. - [ ] Match the **code formatting** of the rest of the codebase. - [ ] Target the `master` branch, NOT a "stable" branch. What existing problem does the pull request solve? A good test plan has the exact commands you ran and their output, provides screenshots or videos if the pull request changes UI or updates the website. See [What is a Test Plan?][1] to learn more. If you have added code that should be tested, add tests. Sign the [CLA][2], if you haven't already. Small pull requests are much easier to review and more likely to get merged. Make sure the PR does only one thing, otherwise please split it. Make sure all **tests pass** on both [Travis][3] and [Circle CI][4]. PRs that break tests are unlikely to be merged. Closes https://github.com/facebook/react-native/pull/13556 Differential Revision: D4908506 Pulled By: javache fbshipit-source-id: 17f6a57e31e68dc79ac9b5303bf56461663d7edd
90 lines
3.0 KiB
JavaScript
90 lines
3.0 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule DatePickerAndroid
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
const DatePickerModule = require('NativeModules').DatePickerAndroid;
|
|
|
|
/**
|
|
* Convert a Date to a timestamp.
|
|
*/
|
|
function _toMillis(options: Object, key: string) {
|
|
const dateVal = options[key];
|
|
// Is it a Date object?
|
|
if (typeof dateVal === 'object' && typeof dateVal.getMonth === 'function') {
|
|
options[key] = dateVal.getTime();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Opens the standard Android date picker dialog.
|
|
*
|
|
* ### Example
|
|
*
|
|
* ```
|
|
* try {
|
|
* const {action, year, month, day} = await DatePickerAndroid.open({
|
|
* // Use `new Date()` for current date.
|
|
* // May 25 2020. Month 0 is January.
|
|
* date: new Date(2020, 4, 25)
|
|
* });
|
|
* if (action !== DatePickerAndroid.dismissedAction) {
|
|
* // Selected year, month (0-11), day
|
|
* }
|
|
* } catch ({code, message}) {
|
|
* console.warn('Cannot open date picker', message);
|
|
* }
|
|
* ```
|
|
*/
|
|
class DatePickerAndroid {
|
|
/**
|
|
* Opens the standard Android date picker dialog.
|
|
*
|
|
* The available keys for the `options` object are:
|
|
*
|
|
* - `date` (`Date` object or timestamp in milliseconds) - date to show by default
|
|
* - `minDate` (`Date` or timestamp in milliseconds) - minimum date that can be selected
|
|
* - `maxDate` (`Date` object or timestamp in milliseconds) - maximum date that can be selected
|
|
* - `mode` (`enum('calendar', 'spinner', 'default')`) - To set the date-picker mode to calendar/spinner/default
|
|
* - 'calendar': Show a date picker in calendar mode.
|
|
* - 'spinner': Show a date picker in spinner mode.
|
|
* - 'default': Show a default native date picker(spinner/calendar) based on android versions.
|
|
*
|
|
* Returns a Promise which will be invoked an object containing `action`, `year`, `month` (0-11),
|
|
* `day` if the user picked a date. If the user dismissed the dialog, the Promise will
|
|
* still be resolved with action being `DatePickerAndroid.dismissedAction` and all the other keys
|
|
* being undefined. **Always** check whether the `action` before reading the values.
|
|
*
|
|
* Note the native date picker dialog has some UI glitches on Android 4 and lower
|
|
* when using the `minDate` and `maxDate` options.
|
|
*/
|
|
static async open(options: Object): Promise<Object> {
|
|
const optionsMs = options;
|
|
if (optionsMs) {
|
|
_toMillis(options, 'date');
|
|
_toMillis(options, 'minDate');
|
|
_toMillis(options, 'maxDate');
|
|
}
|
|
return DatePickerModule.open(options);
|
|
}
|
|
|
|
/**
|
|
* A date has been selected.
|
|
*/
|
|
static get dateSetAction() { return 'dateSetAction'; }
|
|
/**
|
|
* The dialog has been dismissed.
|
|
*/
|
|
static get dismissedAction() { return 'dismissedAction'; }
|
|
}
|
|
|
|
module.exports = DatePickerAndroid;
|