Skip to content

Commit 90bc29c

Browse files
committed
Added option to manually set DatePickerIOS locale.
1 parent f0ac5b3 commit 90bc29c

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

Libraries/Components/DatePicker/DatePickerIOS.ios.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ const DatePickerIOS = createReactClass({
9292
* instance, to show times in Pacific Standard Time, pass -7 * 60.
9393
*/
9494
timeZoneOffsetInMinutes: PropTypes.number,
95+
96+
/**
97+
* DatePicker locale.
98+
*
99+
* By default, date picker locale is based on users locale. Using this parameter
100+
* you can manually change it.
101+
*/
102+
locale: PropTypes.string,
95103
},
96104

97105
getDefaultProps: function(): DefaultProps {
@@ -137,6 +145,7 @@ const DatePickerIOS = createReactClass({
137145
mode={props.mode}
138146
minuteInterval={props.minuteInterval}
139147
timeZoneOffsetInMinutes={props.timeZoneOffsetInMinutes}
148+
locale={props.locale || undefined}
140149
onChange={this._onChange}
141150
onStartShouldSetResponder={() => true}
142151
onResponderTerminationRequest={() => false}

RNTester/js/DatePickerIOSExample.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ class DatePickerExample extends React.Component<$FlowFixMeProps, $FlowFixMeState
2525
static defaultProps = {
2626
date: new Date(),
2727
timeZoneOffsetInHours: (-1) * (new Date()).getTimezoneOffset() / 60,
28+
locale: false,
2829
};
2930

3031
state = {
3132
date: this.props.date,
3233
timeZoneOffsetInHours: this.props.timeZoneOffsetInHours,
34+
locale: this.props.locale,
3335
};
3436

3537
onDateChange = (date) => {
@@ -44,6 +46,10 @@ class DatePickerExample extends React.Component<$FlowFixMeProps, $FlowFixMeState
4446
this.setState({timeZoneOffsetInHours: offset});
4547
};
4648

49+
onLocaleChange = (event) => {
50+
this.setState({locale: event.nativeEvent.text});
51+
};
52+
4753
render() {
4854
// Ideally, the timezone input would be a picker rather than a
4955
// text input, but we don't have any pickers yet :(
@@ -64,25 +70,35 @@ class DatePickerExample extends React.Component<$FlowFixMeProps, $FlowFixMeState
6470
/>
6571
<Text> hours from UTC</Text>
6672
</WithLabel>
73+
<WithLabel label="Locale:">
74+
<TextInput
75+
onChange={this.onLocaleChange}
76+
style={styles.textinput}
77+
value={this.state.locale}
78+
/>
79+
</WithLabel>
6780
<Heading label="Date + time picker" />
6881
<DatePickerIOS
6982
date={this.state.date}
7083
mode="datetime"
7184
timeZoneOffsetInMinutes={this.state.timeZoneOffsetInHours * 60}
7285
onDateChange={this.onDateChange}
86+
locale={this.state.locale}
7387
/>
7488
<Heading label="Date picker" />
7589
<DatePickerIOS
7690
date={this.state.date}
7791
mode="date"
7892
timeZoneOffsetInMinutes={this.state.timeZoneOffsetInHours * 60}
7993
onDateChange={this.onDateChange}
94+
locale={this.state.locale}
8095
/>
8196
<Heading label="Time picker, 10-minute interval" />
8297
<DatePickerIOS
8398
date={this.state.date}
8499
mode="time"
85100
timeZoneOffsetInMinutes={this.state.timeZoneOffsetInHours * 60}
101+
locale={this.state.locale}
86102
onDateChange={this.onDateChange}
87103
minuteInterval={10}
88104
/>

React/Views/RCTDatePickerManager.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ - (UIView *)view
3434
return [RCTDatePicker new];
3535
}
3636

37+
RCT_CUSTOM_VIEW_PROPERTY(locale, NSString, UIDatePicker)
38+
{
39+
[view setLocale:[NSLocale localeWithLocaleIdentifier:json ? [RCTConvert NSString:json] : defaultView.locale.localeIdentifier]];
40+
}
41+
3742
RCT_EXPORT_VIEW_PROPERTY(date, NSDate)
3843
RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSDate)
3944
RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSDate)

0 commit comments

Comments
 (0)