From fd9c3618fcd3dc80f9abf3da779627704c7350e4 Mon Sep 17 00:00:00 2001 From: Robert Paul Date: Sat, 4 Nov 2017 14:32:07 -0700 Subject: [PATCH] - Adding locale prop to DatePickerIOS Summary: While building a React Native application, I've come across the use case of wanting to set a specific locale for DatePickers irrespective of the users OS region setting. Since this is a feature available to native DatePicker components, I think it would be helpful to expose this in React Native as well. Testing can be done by passing a `locale` prop to a DatePickerIOS. Example: ``` this.setState({ date: date })} /> ``` [IOS][ENHANCEMENT][DatePickerIOS] - Adding a locale prop. Closes https://github.com/facebook/react-native/pull/16639 Differential Revision: D6241981 Pulled By: hramos fbshipit-source-id: 77b1b85c09f3e12d6b3e103b3d1ffd1f12e2cea9 --- .../Components/DatePicker/DatePickerIOS.ios.js | 7 +++++++ React/Base/RCTConvert.h | 1 + React/Base/RCTConvert.m | 14 ++++++++++++++ React/Views/RCTDatePickerManager.m | 1 + 4 files changed, 23 insertions(+) diff --git a/Libraries/Components/DatePicker/DatePickerIOS.ios.js b/Libraries/Components/DatePicker/DatePickerIOS.ios.js index 83a2aed5bca2cf..88ae7d5c876d0e 100644 --- a/Libraries/Components/DatePicker/DatePickerIOS.ios.js +++ b/Libraries/Components/DatePicker/DatePickerIOS.ios.js @@ -78,6 +78,11 @@ const DatePickerIOS = createReactClass({ */ mode: PropTypes.oneOf(['date', 'time', 'datetime']), + /** + * The date picker locale. + */ + locale: PropTypes.string, + /** * The interval at which minutes can be selected. */ @@ -127,6 +132,7 @@ const DatePickerIOS = createReactClass({ ref={ picker => { this._picker = picker; } } style={styles.datePickerIOS} date={props.date.getTime()} + locale={props.locale ? props.locale : undefined} maximumDate={ props.maximumDate ? props.maximumDate.getTime() : undefined } @@ -155,6 +161,7 @@ const RCTDatePickerIOS = requireNativeComponent('RCTDatePicker', { propTypes: { ...DatePickerIOS.propTypes, date: PropTypes.number, + locale: PropTypes.string, minimumDate: PropTypes.number, maximumDate: PropTypes.number, onDateChange: () => null, diff --git a/React/Base/RCTConvert.h b/React/Base/RCTConvert.h index e27ae4699ce26a..b3ef27cdb4cedf 100644 --- a/React/Base/RCTConvert.h +++ b/React/Base/RCTConvert.h @@ -55,6 +55,7 @@ typedef NSURL RCTFileURL; + (RCTFileURL *)RCTFileURL:(id)json; + (NSDate *)NSDate:(id)json; ++ (NSLocale *)NSLocale:(id)json; + (NSTimeZone *)NSTimeZone:(id)json; + (NSTimeInterval)NSTimeInterval:(id)json; diff --git a/React/Base/RCTConvert.m b/React/Base/RCTConvert.m index 741830f1224409..6be303046efcc1 100644 --- a/React/Base/RCTConvert.m +++ b/React/Base/RCTConvert.m @@ -222,6 +222,20 @@ + (NSDate *)NSDate:(id)json return nil; } ++ (NSLocale *)NSLocale:(id)json +{ + if ([json isKindOfClass:[NSString class]]) { + NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:json]; + if (!locale) { + RCTLogError(@"JSON String '%@' could not be interpreted as a valid locale. ", json); + } + return locale; + } else if (json) { + RCTLogConvertError(json, @"a locale"); + } + return nil; +} + // JS Standard for time is milliseconds RCT_CUSTOM_CONVERTER(NSTimeInterval, NSTimeInterval, [self double:json] / 1000.0) diff --git a/React/Views/RCTDatePickerManager.m b/React/Views/RCTDatePickerManager.m index 9689c879cb2020..6c70c76c1c23cb 100644 --- a/React/Views/RCTDatePickerManager.m +++ b/React/Views/RCTDatePickerManager.m @@ -35,6 +35,7 @@ - (UIView *)view } RCT_EXPORT_VIEW_PROPERTY(date, NSDate) +RCT_EXPORT_VIEW_PROPERTY(locale, NSLocale) RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSDate) RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSDate) RCT_EXPORT_VIEW_PROPERTY(minuteInterval, NSInteger)