Skip to content

Commit

Permalink
feat: allow overriding the iOS modal theme (light/dark) (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-abbott committed Feb 23, 2022
1 parent fbb6e59 commit b0280c8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export default () => {
| `title` | Modal only: Title text. Can be set to null to remove text. |
| `confirmText` | Modal only: Confirm button text. |
| `cancelText` | Modal only: Cancel button text. |
| `theme` | Modal only, iOS 13+: The theme of the modal. `"light"`, `"dark"`, `"auto"`. Defaults to `"auto"`. |

## Linking

Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export interface DatePickerProps extends ViewProps {

/** Modal title. Set to null to remove */
title?: string | null

/** Modal color theme on iOS. Defaults to 'auto' */
theme?: 'light' | 'dark' | 'auto'
}

export default class DatePicker extends Component<DatePickerProps> {}
11 changes: 11 additions & 0 deletions ios/RNDatePicker/RNDatePickerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ - (UIView *)view
[picker setTimeZone:[RCTConvert NSTimeZone:timeZoneProp]];
}

if(@available(iOS 13, *)) {
NSString * _Nonnull theme = [RCTConvert NSString:[props objectForKey:@"theme"]];
if ([theme isEqualToString:@"light"]) {
alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
} else if ([theme isEqualToString:@"dark"]) {
alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
} else {
alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
}
}

[alertView addSubview:picker];

UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
Expand Down
1 change: 1 addition & 0 deletions src/DatePickerIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class DatePickerIOS extends React.Component {
locale: props.locale ? props.locale : undefined,
maximumDate: props.maximumDate ? props.maximumDate.getTime() : undefined,
minimumDate: props.minimumDate ? props.minimumDate.getTime() : undefined,
theme: props.theme ? props.theme : 'auto',
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/propChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ const androidVariantCheck = new PropCheck(
"Invalid android variant. Valid modes: 'nativeAndroid', 'iosClone'"
)

const themeCheck = new PropCheck(
(props) =>
props && props.theme && !['light', 'dark', 'auto'].includes(props.theme),
"Invalid theme. Valid options: 'light', 'dark', 'auto'"
)

const checks = [
dateCheck,
widthCheck,
heightCheck,
modeCheck,
androidVariantCheck,
themeCheck,
]
6 changes: 5 additions & 1 deletion src/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const androidPropTypes = {
is24hourSource: PropTypes.oneOf(['locale', 'device']),
}

const iOSPropTypes = {
theme: PropTypes.oneOf(['light', 'dark', 'auto']),
}

const modalPropTypes = {
modal: PropTypes.bool,
open: PropTypes.bool,
Expand All @@ -21,7 +25,7 @@ const modalPropTypes = {
const DateType = PropTypes.instanceOf(Date)

export default {
...(Platform === 'android' ? androidPropTypes : {}),
...(Platform === 'android' ? androidPropTypes : iOSPropTypes),
date: DateType.isRequired,
onChange: PropTypes.func,
minimumDate: DateType,
Expand Down

0 comments on commit b0280c8

Please sign in to comment.