Skip to content

Commit fd9c361

Browse files
robertpaul01facebook-github-bot
authored andcommitted
- Adding locale prop to DatePickerIOS
Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> 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: ``` <DatePickerIOS date={this.state.date} mode="date" locale="fr_FR" onDateChange={date => this.setState({ date: date })} /> ``` <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAl ] [ BUGFIX ] [-{Component}-] [ INTERNAL ] [ ENHANCEMENT ] [ {File} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| [CATEGORY] [TYPE] [LOCATION] - MESSAGE EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> [IOS][ENHANCEMENT][DatePickerIOS] - Adding a locale prop. Closes #16639 Differential Revision: D6241981 Pulled By: hramos fbshipit-source-id: 77b1b85c09f3e12d6b3e103b3d1ffd1f12e2cea9
1 parent 38b96cd commit fd9c361

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Libraries/Components/DatePicker/DatePickerIOS.ios.js

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ const DatePickerIOS = createReactClass({
7878
*/
7979
mode: PropTypes.oneOf(['date', 'time', 'datetime']),
8080

81+
/**
82+
* The date picker locale.
83+
*/
84+
locale: PropTypes.string,
85+
8186
/**
8287
* The interval at which minutes can be selected.
8388
*/
@@ -127,6 +132,7 @@ const DatePickerIOS = createReactClass({
127132
ref={ picker => { this._picker = picker; } }
128133
style={styles.datePickerIOS}
129134
date={props.date.getTime()}
135+
locale={props.locale ? props.locale : undefined}
130136
maximumDate={
131137
props.maximumDate ? props.maximumDate.getTime() : undefined
132138
}
@@ -155,6 +161,7 @@ const RCTDatePickerIOS = requireNativeComponent('RCTDatePicker', {
155161
propTypes: {
156162
...DatePickerIOS.propTypes,
157163
date: PropTypes.number,
164+
locale: PropTypes.string,
158165
minimumDate: PropTypes.number,
159166
maximumDate: PropTypes.number,
160167
onDateChange: () => null,

React/Base/RCTConvert.h

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef NSURL RCTFileURL;
5555
+ (RCTFileURL *)RCTFileURL:(id)json;
5656

5757
+ (NSDate *)NSDate:(id)json;
58+
+ (NSLocale *)NSLocale:(id)json;
5859
+ (NSTimeZone *)NSTimeZone:(id)json;
5960
+ (NSTimeInterval)NSTimeInterval:(id)json;
6061

React/Base/RCTConvert.m

+14
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ + (NSDate *)NSDate:(id)json
222222
return nil;
223223
}
224224

225+
+ (NSLocale *)NSLocale:(id)json
226+
{
227+
if ([json isKindOfClass:[NSString class]]) {
228+
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:json];
229+
if (!locale) {
230+
RCTLogError(@"JSON String '%@' could not be interpreted as a valid locale. ", json);
231+
}
232+
return locale;
233+
} else if (json) {
234+
RCTLogConvertError(json, @"a locale");
235+
}
236+
return nil;
237+
}
238+
225239
// JS Standard for time is milliseconds
226240
RCT_CUSTOM_CONVERTER(NSTimeInterval, NSTimeInterval, [self double:json] / 1000.0)
227241

React/Views/RCTDatePickerManager.m

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ - (UIView *)view
3535
}
3636

3737
RCT_EXPORT_VIEW_PROPERTY(date, NSDate)
38+
RCT_EXPORT_VIEW_PROPERTY(locale, NSLocale)
3839
RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSDate)
3940
RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSDate)
4041
RCT_EXPORT_VIEW_PROPERTY(minuteInterval, NSInteger)

0 commit comments

Comments
 (0)