Skip to content

Commit

Permalink
Merge pull request #1 from react-native-datetimepicker/fix/style
Browse files Browse the repository at this point in the history
fix: use UIDatePickerStyle value for dimension measurements
  • Loading branch information
fortmarek authored Sep 16, 2022
2 parents 43ee84b + d0c4f4c commit d312edb
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 118 deletions.
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)
{
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() {
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.

0 comments on commit d312edb

Please sign in to comment.