Skip to content

Commit c0d6fe9

Browse files
Adds closePicker function (#602)
Co-authored-by: Henning Hall <[email protected]>
1 parent 2e3235a commit c0d6fe9

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

android/src/main/java/com/henninghall/date_picker/DatePickerModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
public class DatePickerModule extends ReactContextBaseJavaModule {
2121

22+
private AlertDialog dialog;
23+
2224
DatePickerModule(ReactApplicationContext context) {
2325
super(context);
2426
ApplicationStarter.initialize(context, false); // false = no need to prefetch on time data background tread
@@ -37,10 +39,15 @@ public void removeListeners(Integer count) {
3739
@ReactMethod
3840
public void openPicker(ReadableMap props, Callback onConfirm, Callback onCancel){
3941
PickerView picker = createPicker(props);
40-
AlertDialog dialog = createDialog(props, picker, onConfirm, onCancel);
42+
dialog = createDialog(props, picker, onConfirm, onCancel);
4143
dialog.show();
4244
}
4345

46+
@ReactMethod
47+
public void closePicker(){
48+
dialog.dismiss();
49+
}
50+
4451
private AlertDialog createDialog(
4552
ReadableMap props, final PickerView picker, final Callback onConfirm, final Callback onCancel) {
4653
String title = props.getString("title");

ios/RNDatePicker/RNDatePickerManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010

1111
@interface RNDatePickerManager : RCTViewManager
1212

13+
@property (strong, nonatomic) UIViewController *topViewController;
14+
1315
@end

ios/RNDatePicker/RNDatePickerManager.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,22 @@ - (UIView *)view
156156
}
157157

158158
// Finding the top view controller which is neccessary to be able to show the picker from within modal
159-
UIViewController *topViewController = rootViewController;
160-
while (topViewController.presentedViewController){
161-
topViewController = topViewController.presentedViewController;
159+
_topViewController = rootViewController;
160+
while (_topViewController.presentedViewController){
161+
_topViewController = _topViewController.presentedViewController;
162162
}
163-
164-
[topViewController presentViewController:alertController animated:YES completion:nil];
163+
[_topViewController presentViewController:alertController animated:YES completion:nil];
165164
});
166165

167166
}
168167

168+
RCT_EXPORT_METHOD(closePicker)
169+
{
170+
dispatch_async(dispatch_get_main_queue(), ^{
171+
[_topViewController dismissViewControllerAnimated:YES completion:nil];
172+
});
173+
}
174+
169175
- (double) getPickerWidth :(UIView *) alertView
170176
{
171177
bool iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;

src/DatePickerAndroid.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ class DatePickerAndroid extends React.PureComponent {
2020
const props = this.getProps()
2121
const isClosed = this._isCurrentlyClosed();
2222

23-
this.previousProps = props;
23+
this.previousProps = props;
2424
if (props.modal) {
2525
if (props.open && isClosed) {
2626
NativeModules.RNDatePicker.openPicker(
2727
props,
2828
this._onConfirm,
2929
this.props.onCancel
3030
)
31+
} else if (!props.open && !isClosed) {
32+
NativeModules.RNDatePicker.closePicker()
3133
}
3234
return null
3335
}

src/DatePickerIOS.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ export default class DatePickerIOS extends React.Component {
4747
const props = this._toIosProps(this.props)
4848
const isClosed = this._isCurrentlyClosed();
4949

50-
this.previousProps = props;
50+
this.previousProps = props;
5151
if (props.modal) {
5252
if (props.open && isClosed) {
5353
NativeModules.RNDatePickerManager.openPicker(
5454
props,
5555
this._onConfirm,
5656
props.onCancel
5757
)
58+
} else if (!props.open && !isClosed) {
59+
NativeModules.RNDatePickerManager.closePicker()
5860
}
5961
return null
6062
}
@@ -72,7 +74,7 @@ export default class DatePickerIOS extends React.Component {
7274
/>
7375
)
7476
}
75-
77+
7678
_isCurrentlyClosed = () => !this.previousProps || !this.previousProps.open
7779
}
7880

0 commit comments

Comments
 (0)