Skip to content

Commit 1fb6a2e

Browse files
AmauryLietvonovak
andauthored
feat: Allow overriding theme variant used by native ios picker (#457)
* feat: Allow overriding theme variant used by native ios picker * docs: Give workaround for ios 13 and older version * fix prettier Co-authored-by: Vojtech Novak <[email protected]>
1 parent 1e52db5 commit 1fb6a2e

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ React Native date & time picker component for iOS, Android and Windows.
5858
- [`dateFormat` (`optional`, `Windows only`)](#dateFormat-optional-windows-only)
5959
- [`firstDayOfWeek` (`optional`, `Windows only`)](#firstDayOfWeek-optional-windows-only)
6060
- [`textColor` (`optional`, `iOS only`)](#textColor-optional-ios-only)
61+
- [`themeVariant` (`optional`, `iOS only`)](#themeVariant-optional-ios-only)
6162
- [`locale` (`optional`, `iOS only`)](#locale-optional-ios-only)
6263
- [`is24Hour` (`optional`, `Windows and Android only`)](#is24hour-optional-windows-and-android-only)
6364
- [`neutralButtonLabel` (`optional`, `Android only`)](#neutralbuttonlabel-optional-android-only)
@@ -311,6 +312,21 @@ Allows changing of the textColor of the date picker. Has effect only when `displ
311312
<RNDateTimePicker textColor="red" />
312313
```
313314

315+
#### `themeVariant` (`optional`, `iOS only`)
316+
317+
Allows overriding system theme variant (dark or light mode) used by the date picker.
318+
319+
:warning: Has effect only on iOS 14 and later. On iOS 13 & less, use `textColor` to make the picker dark-theme compatible
320+
321+
List of possible values:
322+
323+
- `"light"`
324+
- `"dark"`
325+
326+
```js
327+
<RNDateTimePicker themeVariant="light" />
328+
```
329+
314330
#### `locale` (`optional`, `iOS only`)
315331

316332
Allows changing of the locale of the component. By default it uses the device's locale.

ios/RNDateTimePickerManager.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ @implementation RCTConvert(UIDatePicker)
2929
#endif
3030
}), UIDatePickerStyleAutomatic, integerValue)
3131

32+
RCT_ENUM_CONVERTER(UIUserInterfaceStyle, (@{
33+
@"dark": @(UIUserInterfaceStyleDark),
34+
@"light": @(UIUserInterfaceStyleLight),
35+
}), UIUserInterfaceStyleUnspecified, integerValue)
36+
3237
@end
3338

3439
@implementation RNDateTimePickerManager
@@ -84,6 +89,17 @@ + (NSString*) datepickerStyleToString: (UIDatePickerStyle) style {
8489
RCT_REMAP_VIEW_PROPERTY(mode, datePickerMode, UIDatePickerMode)
8590
RCT_REMAP_VIEW_PROPERTY(timeZoneOffsetInMinutes, timeZone, NSTimeZone)
8691

92+
RCT_CUSTOM_VIEW_PROPERTY(themeVariant, UIUserInterfaceStyle, RNDateTimePicker) {
93+
if (@available(iOS 13.0, *)) {
94+
if (json) {
95+
UIUserInterfaceStyle propValue = [RCTConvert UIUserInterfaceStyle:json];
96+
view.overrideUserInterfaceStyle = propValue;
97+
} else {
98+
view.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
99+
}
100+
}
101+
}
102+
87103
RCT_CUSTOM_VIEW_PROPERTY(textColor, UIColor, RNDateTimePicker)
88104
{
89105
if (@available(iOS 14.0, *) && view.datePickerStyle != UIDatePickerStyleWheels) {

src/datetimepicker.ios.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default function Picker({
5252
minuteInterval,
5353
timeZoneOffsetInMinutes,
5454
textColor,
55+
themeVariant,
5556
onChange,
5657
disabled = false,
5758
...otherProps
@@ -121,6 +122,7 @@ export default function Picker({
121122
timeZoneOffsetInMinutes={timeZoneOffsetInMinutes}
122123
onChange={_onChange}
123124
textColor={textColor}
125+
themeVariant={themeVariant}
124126
onStartShouldSetResponder={() => true}
125127
onResponderTerminationRequest={() => false}
126128
displayIOS={display}

src/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ export type IOSNativeProps = Readonly<
9696
*/
9797
textColor?: string;
9898

99+
/**
100+
* Override theme variant used by iOS native picker
101+
*/
102+
themeVariant?: 'dark' | 'light';
103+
99104
/**
100105
* Sets the preferredDatePickerStyle for picker
101106
*/

src/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ export type IOSNativeProps = $ReadOnly<{|
115115
*/
116116
textColor?: ?ColorValue,
117117

118+
/**
119+
* Override theme variant used by iOS native picker
120+
*/
121+
themeVariant?: 'dark' | 'light',
122+
118123
/**
119124
* Sets the preferredDatePickerStyle for picker
120125
*/

0 commit comments

Comments
 (0)