Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use UIDatePickerStyle value for dimension measurements #1

Merged
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
25 changes: 1 addition & 24 deletions ios/RNDateTimePickerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,10 @@ + (NSString*) datepickerStyleToString: (UIDatePickerStyle) style API_AVAILABLE(
}
}

RCT_EXPORT_METHOD(getDefaultDisplayValue:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect 👌 I have not realized we can remove this logic.

{
dispatch_async(dispatch_get_main_queue(), ^{
if (@available(iOS 13.4, *)) {
UIDatePicker* view = [RNDateTimePicker new];

view.preferredDatePickerStyle = UIDatePickerStyleAutomatic;
UIDatePickerMode renderedMode = [RCTConvert UIDatePickerMode:options[@"mode"]];
view.datePickerMode = renderedMode;
// NOTE afaict we do not need to measure the actual dimensions here, but if we do, just look at the original PR

UIDatePickerStyle determinedDisplayValue = view.datePickerStyle;

resolve(@{
@"determinedDisplayValue": [RNDateTimePickerManager datepickerStyleToString:determinedDisplayValue],
});
} else {
// never happens; the condition is just to avoid compiler warnings
reject(@"UNEXPECTED_CALL", @"unexpected getDefaultDisplayValue() call", nil);
}
});
}

RCT_EXPORT_SHADOW_PROPERTY(date, NSDate)
RCT_EXPORT_SHADOW_PROPERTY(mode, UIDatePickerMode)
RCT_EXPORT_SHADOW_PROPERTY(locale, NSLocale)
RCT_EXPORT_SHADOW_PROPERTY(datePickerStyle, UIDatePickerStyle)
RCT_EXPORT_SHADOW_PROPERTY(displayIOS, RNCUIDatePickerStyle)

RCT_EXPORT_VIEW_PROPERTY(date, NSDate)
RCT_EXPORT_VIEW_PROPERTY(locale, NSLocale)
Expand Down
2 changes: 1 addition & 1 deletion ios/RNDateTimePickerShadowView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
@property (nonatomic) UIDatePickerMode mode;
@property (nullable, nonatomic, strong) NSDate *date;
@property (nullable, nonatomic, strong) NSLocale *locale;
@property (nonatomic, readonly, assign) UIDatePickerStyle datePickerStyle API_AVAILABLE(ios(13.4));
@property (nonatomic, assign) UIDatePickerStyle displayIOS API_AVAILABLE(ios(13.4));

@end
8 changes: 7 additions & 1 deletion ios/RNDateTimePickerShadowView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ - (void)setMode:(UIDatePickerMode)mode {
YGNodeMarkDirty(self.yogaNode);
}


- (void)setDisplayIOS:(UIDatePickerStyle)displayIOS {
_displayIOS = displayIOS;
YGNodeMarkDirty(self.yogaNode);
}

static YGSize RNDateTimePickerShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
{
RNDateTimePickerShadowView *shadowPickerView = (__bridge RNDateTimePickerShadowView *)YGNodeGetContext(node);
Expand All @@ -35,7 +41,7 @@ static YGSize RNDateTimePickerShadowViewMeasure(YGNodeRef node, float width, YGM
[shadowPickerView.picker setDatePickerMode:shadowPickerView.mode];
[shadowPickerView.picker setLocale:shadowPickerView.locale];
if (@available(iOS 14.0, *)) {
[shadowPickerView.picker setPreferredDatePickerStyle:shadowPickerView.datePickerStyle];
[shadowPickerView.picker setPreferredDatePickerStyle:shadowPickerView.displayIOS];
}
size = [shadowPickerView.picker sizeThatFits:UILayoutFittingCompressedSize];
});
Expand Down
23 changes: 2 additions & 21 deletions src/datetimepicker.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {sharedPropsValidation, toMilliseconds} from './utils';
import {IOS_DISPLAY, ANDROID_MODE, EVENT_TYPE_SET} from './constants';
import invariant from 'invariant';
import * as React from 'react';
import {getPickerHeightStyle} from './layoutUtilsIOS';
import {Platform, StyleSheet} from 'react-native';
import {Platform} from 'react-native';

import type {
NativeEventIOS,
Expand Down Expand Up @@ -61,7 +60,6 @@ export default function Picker({
}: IOSNativeProps): React.Node {
sharedPropsValidation({value});

const [heightStyle, setHeightStyle] = React.useState(undefined);
const _picker: NativeRef = React.useRef(null);
const display = getDisplaySafe(providedDisplay);

Expand All @@ -80,18 +78,6 @@ export default function Picker({
[onChange, value],
);

React.useEffect(
function ensureCorrectHeight() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the old architecture, we definitely don't need this, good catch 👍

However, I have not tested my PR with the New Architecture PR. I think the shadow view implementation I've done is only for the old architecture given the rendering on the New Architecture is done in C++. But I am not 100 % sure. @alfonsocj, would you be able to check if this library still works on the New Architecture if you'd merge the changes from here?

const height = getPickerHeightStyle(display, mode);
if (height instanceof Promise) {
height.then(setHeightStyle);
} else {
setHeightStyle(height);
}
},
[display, mode],
);

const _onChange = (event: NativeEventIOS) => {
const timestamp = event.nativeEvent.timestamp;
// $FlowFixMe Cannot assign object literal to `unifiedEvent` because number [1] is incompatible with undefined [2] in property `nativeEvent.timestamp`.
Expand All @@ -104,11 +90,6 @@ export default function Picker({

invariant(value, 'A date or time should be specified as `value`.');

if (!heightStyle) {
// wait for height to be available in state
return null;
}

const dates: DatePickerOptions = {value, maximumDate, minimumDate};
toMilliseconds(dates, 'value', 'minimumDate', 'maximumDate');

Expand All @@ -117,7 +98,7 @@ export default function Picker({
<RNDateTimePicker
testID={testID}
ref={_picker}
style={StyleSheet.compose(heightStyle, style)}
style={style}
date={dates.value}
locale={locale !== null && locale !== '' ? locale : undefined}
maximumDate={dates.maximumDate}
Expand Down
71 changes: 0 additions & 71 deletions src/layoutUtilsIOS.js

This file was deleted.