Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ public void onClick(DialogInterface dialog, int which) {
}
}

@ReactMethod
public void close(Promise promise) {
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
if (activity == null) {
promise.reject(
RNConstants.ERROR_NO_ACTIVITY,
"Tried to close a DatePicker dialog while not attached to an Activity");
return;
}

FragmentManager fragmentManager = activity.getSupportFragmentManager();
final RNDatePickerDialogFragment oldFragment = (RNDatePickerDialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);

if (oldFragment != null) {
oldFragment.dismiss();
}

promise.resolve(null);
}
/**
* Show a date picker dialog.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ public void onClick(DialogInterface dialog, int which) {
}
}

@ReactMethod
public void close(Promise promise) {
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
if (activity == null) {
promise.reject(
RNConstants.ERROR_NO_ACTIVITY,
"Tried to close a TimePicker dialog while not attached to an Activity");
return;
}

FragmentManager fragmentManager = activity.getSupportFragmentManager();
final RNTimePickerDialogFragment oldFragment = (RNTimePickerDialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);

if (oldFragment != null) {
oldFragment.dismiss();
}

promise.resolve(null);
}

@ReactMethod
public void open(@Nullable final ReadableMap options, Promise promise) {

Expand Down
4 changes: 4 additions & 0 deletions src/datepicker.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export default class DatePickerAndroid {
return NativeModules.RNDatePickerAndroid.open(options);
}

static async close(): Promise<null> {
return NativeModules.RNDatePickerAndroid.close();
}

/**
* A date has been selected.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/datetimepicker.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './constants';
import pickers from './picker';
import invariant from 'invariant';
import {useEffect} from 'react';

import type {AndroidEvent, AndroidNativeProps} from './types';

Expand Down Expand Up @@ -66,6 +67,12 @@ export default function RNDateTimePicker(props: AndroidNativeProps) {
break;
}

useEffect(() => {
// This effect runs on unmount, and will ensure the picker is closed.
// This allows for controlling the opening state of the picker through declarative logic in jsx.
return () => (pickers[mode] ?? pickers[MODE_DATE]).close();
}, [mode]);

picker.then(
function resolve({action, day, month, year, minute, hour}) {
const date = new Date(value);
Expand Down
4 changes: 4 additions & 0 deletions src/timepicker.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export default class TimePickerAndroid {
return NativeModules.RNTimePickerAndroid.open(options);
}

static async close(): Promise<null> {
return NativeModules.RNDatePickerAndroid.close();
}

/**
* A time has been selected.
*/
Expand Down