Skip to content

Commit

Permalink
fix: avoid crashing on web (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak authored Jun 27, 2022
1 parent e3c05a8 commit 69589a0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 100 deletions.
102 changes: 102 additions & 0 deletions src/DateTimePickerAndroid.android.js
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};
105 changes: 5 additions & 100 deletions src/DateTimePickerAndroid.js
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};

0 comments on commit 69589a0

Please sign in to comment.