This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

View File

@ -0,0 +1,89 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
'use strict';
import type {Options, DatePickerOpenAction} from './DatePickerAndroidTypes';
import NativeDatePickerAndroid from './NativeDatePickerAndroid';
/**
* Convert a Date to a timestamp.
*/
function _toMillis(options: Options, 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: ?Options): Promise<DatePickerOpenAction> {
const optionsMs = options;
if (optionsMs != null) {
_toMillis(optionsMs, 'date');
_toMillis(optionsMs, 'minDate');
_toMillis(optionsMs, 'maxDate');
}
return NativeDatePickerAndroid.open(options);
}
/**
* A date has been selected.
*/
static +dateSetAction: 'dateSetAction' = 'dateSetAction';
/**
* The dialog has been dismissed.
*/
static +dismissedAction: 'dismissedAction' = 'dismissedAction';
}
module.exports = DatePickerAndroid;

View File

@ -0,0 +1,30 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
'use strict';
import type {Options, DatePickerOpenAction} from './DatePickerAndroidTypes';
class DatePickerAndroid {
static async open(options: ?Options): Promise<DatePickerOpenAction> {
throw new Error('DatePickerAndroid is not supported on this platform.');
}
/**
* A date has been selected.
*/
static +dateSetAction: 'dateSetAction' = 'dateSetAction';
/**
* The dialog has been dismissed.
*/
static +dismissedAction: 'dismissedAction' = 'dismissedAction';
}
module.exports = DatePickerAndroid;

View File

@ -0,0 +1,30 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
export type Options = $ReadOnly<{|
date?: ?(Date | number),
minDate?: ?(Date | number),
maxDate?: ?(Date | number),
mode?: ?('calendar' | 'spinner' | 'default'),
|}>;
export type DatePickerOpenAction =
| {|
action: 'dateSetAction',
year: number,
month: number,
day: number,
|}
| {|
action: 'dismissedAction',
year: void,
month: void,
day: void,
|};

View File

@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {TurboModule} from '../../TurboModule/RCTExport';
import * as TurboModuleRegistry from '../../TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
+open: (options: Object) => Promise<Object>;
}
export default (TurboModuleRegistry.getEnforcing<Spec>(
'DatePickerAndroid',
): Spec);