-
-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
import { | ||
DATE_SET_ACTION, | ||
TIME_SET_ACTION, | ||
DISMISS_ACTION, | ||
NEUTRAL_BUTTON_ACTION, | ||
ANDROID_DISPLAY, | ||
ANDROID_MODE, | ||
ANDROID_EVT_TYPE, | ||
EVENT_TYPE_SET, | ||
} from './constants'; | ||
import invariant from 'invariant'; | ||
|
||
import type {DateTimePickerEvent, AndroidNativeProps} from './types'; | ||
import { | ||
getOpenPicker, | ||
timeZoneOffsetDateSetter, | ||
validateAndroidProps, | ||
} from './androidUtils'; | ||
import pickers from './picker'; | ||
|
||
function open(props: AndroidNativeProps) { | ||
const { | ||
mode = ANDROID_MODE.date, | ||
display = ANDROID_DISPLAY.default, | ||
value: originalValue, | ||
is24Hour, | ||
minimumDate, | ||
maximumDate, | ||
neutralButtonLabel, | ||
minuteInterval, | ||
timeZoneOffsetInMinutes, | ||
onChange, | ||
onError, | ||
} = props; | ||
validateAndroidProps(props); | ||
invariant(originalValue, 'A date or time must be specified as `value` prop.'); | ||
|
||
const valueTimestamp = originalValue.getTime(); | ||
const openPicker = getOpenPicker({ | ||
mode, | ||
value: valueTimestamp, | ||
display, | ||
is24Hour, | ||
minimumDate, | ||
maximumDate, | ||
neutralButtonLabel, | ||
minuteInterval, | ||
timeZoneOffsetInMinutes, | ||
}); | ||
|
||
const presentPicker = async () => { | ||
try { | ||
const {action, day, month, year, minute, hour} = await openPicker(); | ||
let date = new Date(valueTimestamp); | ||
let event: DateTimePickerEvent = { | ||
type: EVENT_TYPE_SET, | ||
nativeEvent: {}, | ||
}; | ||
|
||
switch (action) { | ||
case DATE_SET_ACTION: | ||
date.setFullYear(year, month, day); | ||
date = timeZoneOffsetDateSetter(date, timeZoneOffsetInMinutes); | ||
event.nativeEvent.timestamp = date.getTime(); | ||
onChange?.(event, date); | ||
break; | ||
|
||
case TIME_SET_ACTION: | ||
date.setHours(hour, minute); | ||
date = timeZoneOffsetDateSetter(date, timeZoneOffsetInMinutes); | ||
event.nativeEvent.timestamp = date.getTime(); | ||
onChange?.(event, date); | ||
break; | ||
|
||
case NEUTRAL_BUTTON_ACTION: | ||
event.type = ANDROID_EVT_TYPE.neutralButtonPressed; | ||
onChange?.(event, originalValue); | ||
break; | ||
|
||
case DISMISS_ACTION: | ||
default: | ||
event.type = ANDROID_EVT_TYPE.dismissed; | ||
onChange?.(event, originalValue); | ||
break; | ||
} | ||
} catch (error) { | ||
onError && onError(error); | ||
} | ||
}; | ||
presentPicker(); | ||
} | ||
|
||
function dismiss(mode: AndroidNativeProps['mode']) { | ||
// $FlowFixMe - `AbstractComponent` [1] is not an instance type. | ||
pickers[mode].dismiss(); | ||
} | ||
|
||
export const DateTimePickerAndroid = {open, dismiss}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,7 @@ | ||
/** | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
import { | ||
DATE_SET_ACTION, | ||
TIME_SET_ACTION, | ||
DISMISS_ACTION, | ||
NEUTRAL_BUTTON_ACTION, | ||
ANDROID_DISPLAY, | ||
ANDROID_MODE, | ||
ANDROID_EVT_TYPE, | ||
EVENT_TYPE_SET, | ||
} from './constants'; | ||
import invariant from 'invariant'; | ||
import {Platform} from 'react-native'; | ||
|
||
import type {DateTimePickerEvent, AndroidNativeProps} from './types'; | ||
import { | ||
getOpenPicker, | ||
timeZoneOffsetDateSetter, | ||
validateAndroidProps, | ||
} from './androidUtils'; | ||
import pickers from './picker'; | ||
const warn = () => { | ||
console.warn(`DateTimePickerAndroid is not supported on: ${Platform.OS}`); | ||
}; | ||
|
||
function open(props: AndroidNativeProps) { | ||
const { | ||
mode = ANDROID_MODE.date, | ||
display = ANDROID_DISPLAY.default, | ||
value: originalValue, | ||
is24Hour, | ||
minimumDate, | ||
maximumDate, | ||
neutralButtonLabel, | ||
minuteInterval, | ||
timeZoneOffsetInMinutes, | ||
onChange, | ||
onError, | ||
} = props; | ||
validateAndroidProps(props); | ||
invariant(originalValue, 'A date or time must be specified as `value` prop.'); | ||
|
||
const valueTimestamp = originalValue.getTime(); | ||
const openPicker = getOpenPicker({ | ||
mode, | ||
value: valueTimestamp, | ||
display, | ||
is24Hour, | ||
minimumDate, | ||
maximumDate, | ||
neutralButtonLabel, | ||
minuteInterval, | ||
timeZoneOffsetInMinutes, | ||
}); | ||
|
||
const presentPicker = async () => { | ||
try { | ||
const {action, day, month, year, minute, hour} = await openPicker(); | ||
let date = new Date(valueTimestamp); | ||
let event: DateTimePickerEvent = { | ||
type: EVENT_TYPE_SET, | ||
nativeEvent: {}, | ||
}; | ||
|
||
switch (action) { | ||
case DATE_SET_ACTION: | ||
date.setFullYear(year, month, day); | ||
date = timeZoneOffsetDateSetter(date, timeZoneOffsetInMinutes); | ||
event.nativeEvent.timestamp = date.getTime(); | ||
onChange?.(event, date); | ||
break; | ||
|
||
case TIME_SET_ACTION: | ||
date.setHours(hour, minute); | ||
date = timeZoneOffsetDateSetter(date, timeZoneOffsetInMinutes); | ||
event.nativeEvent.timestamp = date.getTime(); | ||
onChange?.(event, date); | ||
break; | ||
|
||
case NEUTRAL_BUTTON_ACTION: | ||
event.type = ANDROID_EVT_TYPE.neutralButtonPressed; | ||
onChange?.(event, originalValue); | ||
break; | ||
|
||
case DISMISS_ACTION: | ||
default: | ||
event.type = ANDROID_EVT_TYPE.dismissed; | ||
onChange?.(event, originalValue); | ||
break; | ||
} | ||
} catch (error) { | ||
onError && onError(error); | ||
} | ||
}; | ||
presentPicker(); | ||
} | ||
|
||
function dismiss(mode: AndroidNativeProps['mode']) { | ||
// $FlowFixMe - `AbstractComponent` [1] is not an instance type. | ||
pickers[mode].dismiss(); | ||
} | ||
|
||
export const DateTimePickerAndroid = {open, dismiss}; | ||
export const DateTimePickerAndroid = {open: warn, dismiss: warn}; |