Browse Source

Adds closePicker function (#602)

Co-authored-by: Henning Hall <henning.hall@hotmail.com>
master
aureosouza 2 years ago
committed by GitHub
parent
commit
c0d6fe9345
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 9 deletions
  1. +8
    -1
      android/src/main/java/com/henninghall/date_picker/DatePickerModule.java
  2. +2
    -0
      ios/RNDatePicker/RNDatePickerManager.h
  3. +11
    -5
      ios/RNDatePicker/RNDatePickerManager.m
  4. +3
    -1
      src/DatePickerAndroid.js
  5. +4
    -2
      src/DatePickerIOS.js

+ 8
- 1
android/src/main/java/com/henninghall/date_picker/DatePickerModule.java View File

@ -19,6 +19,8 @@ import net.time4j.android.ApplicationStarter;
public class DatePickerModule extends ReactContextBaseJavaModule {
private AlertDialog dialog;
DatePickerModule(ReactApplicationContext context) {
super(context);
ApplicationStarter.initialize(context, false); // false = no need to prefetch on time data background tread
@ -37,10 +39,15 @@ public class DatePickerModule extends ReactContextBaseJavaModule {
@ReactMethod
public void openPicker(ReadableMap props, Callback onConfirm, Callback onCancel){
PickerView picker = createPicker(props);
AlertDialog dialog = createDialog(props, picker, onConfirm, onCancel);
dialog = createDialog(props, picker, onConfirm, onCancel);
dialog.show();
}
@ReactMethod
public void closePicker(){
dialog.dismiss();
}
private AlertDialog createDialog(
ReadableMap props, final PickerView picker, final Callback onConfirm, final Callback onCancel) {
String title = props.getString("title");

+ 2
- 0
ios/RNDatePicker/RNDatePickerManager.h View File

@ -10,4 +10,6 @@
@interface RNDatePickerManager : RCTViewManager
@property (strong, nonatomic) UIViewController *topViewController;
@end

+ 11
- 5
ios/RNDatePicker/RNDatePickerManager.m View File

@ -156,16 +156,22 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *) props
}
// Finding the top view controller which is neccessary to be able to show the picker from within modal
UIViewController *topViewController = rootViewController;
while (topViewController.presentedViewController){
topViewController = topViewController.presentedViewController;
_topViewController = rootViewController;
while (_topViewController.presentedViewController){
_topViewController = _topViewController.presentedViewController;
}
[topViewController presentViewController:alertController animated:YES completion:nil];
[_topViewController presentViewController:alertController animated:YES completion:nil];
});
}
RCT_EXPORT_METHOD(closePicker)
{
dispatch_async(dispatch_get_main_queue(), ^{
[_topViewController dismissViewControllerAnimated:YES completion:nil];
});
}
- (double) getPickerWidth :(UIView *) alertView
{
bool iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;

+ 3
- 1
src/DatePickerAndroid.js View File

@ -20,7 +20,7 @@ class DatePickerAndroid extends React.PureComponent {
const props = this.getProps()
const isClosed = this._isCurrentlyClosed();
this.previousProps = props;
this.previousProps = props;
if (props.modal) {
if (props.open && isClosed) {
NativeModules.RNDatePicker.openPicker(
@ -28,6 +28,8 @@ class DatePickerAndroid extends React.PureComponent {
this._onConfirm,
this.props.onCancel
)
} else if (!props.open && !isClosed) {
NativeModules.RNDatePicker.closePicker()
}
return null
}

+ 4
- 2
src/DatePickerIOS.js View File

@ -47,7 +47,7 @@ export default class DatePickerIOS extends React.Component {
const props = this._toIosProps(this.props)
const isClosed = this._isCurrentlyClosed();
this.previousProps = props;
this.previousProps = props;
if (props.modal) {
if (props.open && isClosed) {
NativeModules.RNDatePickerManager.openPicker(
@ -55,6 +55,8 @@ export default class DatePickerIOS extends React.Component {
this._onConfirm,
props.onCancel
)
} else if (!props.open && !isClosed) {
NativeModules.RNDatePickerManager.closePicker()
}
return null
}
@ -72,7 +74,7 @@ export default class DatePickerIOS extends React.Component {
/>
)
}
_isCurrentlyClosed = () => !this.previousProps || !this.previousProps.open
}

Loading…
Cancel
Save