Browse Source

feat: allow overriding the iOS modal theme (light/dark) (#465)

master
simon-abbott 3 years ago
committed by GitHub
parent
commit
b0280c82b0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 1 deletions
  1. +1
    -0
      README.md
  2. +3
    -0
      index.d.ts
  3. +11
    -0
      ios/RNDatePicker/RNDatePickerManager.m
  4. +1
    -0
      src/DatePickerIOS.js
  5. +7
    -0
      src/propChecker.js
  6. +5
    -1
      src/propTypes.js

+ 1
- 0
README.md View File

@ -175,6 +175,7 @@ export default () => {
| `title` | Modal only: Title text. Can be set to null to remove text. | | `title` | Modal only: Title text. Can be set to null to remove text. |
| `confirmText` | Modal only: Confirm button text. | | `confirmText` | Modal only: Confirm button text. |
| `cancelText` | Modal only: Cancel 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 ## Linking

+ 3
- 0
index.d.ts View File

@ -100,6 +100,9 @@ export interface DatePickerProps extends ViewProps {
/** Modal title. Set to null to remove */ /** Modal title. Set to null to remove */
title?: string | null title?: string | null
/** Modal color theme on iOS. Defaults to 'auto' */
theme?: 'light' | 'dark' | 'auto'
} }
export default class DatePicker extends Component<DatePickerProps> {} export default class DatePicker extends Component<DatePickerProps> {}

+ 11
- 0
ios/RNDatePicker/RNDatePickerManager.m View File

@ -114,6 +114,17 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *) props
[picker setTimeZone:[RCTConvert NSTimeZone:timeZoneProp]]; [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]; [alertView addSubview:picker];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

+ 1
- 0
src/DatePickerIOS.js View File

@ -36,6 +36,7 @@ export default class DatePickerIOS extends React.Component {
locale: props.locale ? props.locale : undefined, locale: props.locale ? props.locale : undefined,
maximumDate: props.maximumDate ? props.maximumDate.getTime() : undefined, maximumDate: props.maximumDate ? props.maximumDate.getTime() : undefined,
minimumDate: props.minimumDate ? props.minimumDate.getTime() : undefined, minimumDate: props.minimumDate ? props.minimumDate.getTime() : undefined,
theme: props.theme ? props.theme : 'auto',
} }
} }

+ 7
- 0
src/propChecker.js View File

@ -53,10 +53,17 @@ const androidVariantCheck = new PropCheck(
"Invalid android variant. Valid modes: 'nativeAndroid', 'iosClone'" "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 = [ const checks = [
dateCheck, dateCheck,
widthCheck, widthCheck,
heightCheck, heightCheck,
modeCheck, modeCheck,
androidVariantCheck, androidVariantCheck,
themeCheck,
] ]

+ 5
- 1
src/propTypes.js View File

@ -8,6 +8,10 @@ const androidPropTypes = {
is24hourSource: PropTypes.oneOf(['locale', 'device']), is24hourSource: PropTypes.oneOf(['locale', 'device']),
} }
const iOSPropTypes = {
theme: PropTypes.oneOf(['light', 'dark', 'auto']),
}
const modalPropTypes = { const modalPropTypes = {
modal: PropTypes.bool, modal: PropTypes.bool,
open: PropTypes.bool, open: PropTypes.bool,
@ -21,7 +25,7 @@ const modalPropTypes = {
const DateType = PropTypes.instanceOf(Date) const DateType = PropTypes.instanceOf(Date)
export default { export default {
...(Platform === 'android' ? androidPropTypes : {}),
...(Platform === 'android' ? androidPropTypes : iOSPropTypes),
date: DateType.isRequired, date: DateType.isRequired,
onChange: PropTypes.func, onChange: PropTypes.func,
minimumDate: DateType, minimumDate: DateType,

Loading…
Cancel
Save