Skip to content

Commit 05f39fa

Browse files
authored
feat: add back locale prop (#544)
* Revert "feat: remove locale prop (#540)" This reverts commit 6637c03. * chore: update docs and minor changes * fix screenshot? * improve docs and screenshot
1 parent ea20f45 commit 05f39fa

File tree

10 files changed

+40
-11
lines changed

10 files changed

+40
-11
lines changed

.eslintrc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ module.exports = {
1212
jest: true,
1313
jasmine: true,
1414
waitFor: true,
15-
detoxCircus: true
16-
}
15+
detoxCircus: true,
16+
},
17+
rules: {
18+
'no-var': 2,
19+
},
1720
};

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ React Native date & time picker component for iOS, Android and Windows.
2525
<table>
2626
<tr><td colspan=2><strong>iOS</strong></td></tr>
2727
<tr>
28-
<td><p align="center"><img src="./docs/images/ios_date.png" width="260" height="420"/></p></td>
28+
<td><p align="center"><img src="./docs/images/ios_date_new.png" height="420"/></p></td>
2929
<td><p align="center"><img src="./docs/images/ios_time.png" width="260" height="420"/></p></td>
3030
</tr>
3131
<tr><td colspan=2><strong>Android</strong></td></tr>
@@ -67,6 +67,7 @@ React Native date & time picker component for iOS, Android and Windows.
6767
- [`firstDayOfWeek` (`optional`, `Windows only`)](#firstDayOfWeek-optional-windows-only)
6868
- [`textColor` (`optional`, `iOS only`)](#textColor-optional-ios-only)
6969
- [`themeVariant` (`optional`, `iOS only`)](#themeVariant-optional-ios-only)
70+
- [`locale` (`optional`, `iOS only`)](#locale-optional-ios-only)
7071
- [`is24Hour` (`optional`, `Windows and Android only`)](#is24hour-optional-windows-and-android-only)
7172
- [`neutralButtonLabel` (`optional`, `Android only`)](#neutralbuttonlabel-optional-android-only)
7273
- [`minuteInterval` (`optional`)](#minuteinterval-optional)
@@ -176,12 +177,14 @@ export const App = () => {
176177
};
177178
```
178179

179-
## Locale note
180+
## Localization note
180181

181182
On Android, the picker will be controlled by the system locale. If you wish to change it, [see instructions here](https://stackoverflow.com/a/2900144/2070942).
182183

183184
On iOS, the locale can be controlled from xCode, as [documented here](https://developer.apple.com/documentation/xcode/adding-support-for-languages-and-regions).
184185

186+
There is also the iOS-only locale prop that can be used to force locale in some cases but its usage is discouraged due to [not working robustly in all picker modes](./docs/images/ios_date_new.png) (note the mixed month and day names).
187+
185188
For Expo, follow the [localization docs](https://docs.expo.dev/distribution/app-stores/#localizing-your-ios-app).
186189

187190
## Props
@@ -317,6 +320,15 @@ Allows changing of the textColor of the date picker. Has effect only when `displ
317320
<RNDateTimePicker textColor="red" />
318321
```
319322

323+
#### `locale` (`optional`, `iOS only`)
324+
325+
Allows changing the locale of the component. By default, the device's locale is used. Please note using this prop is discouraged due to not working reliably in all picker modes.
326+
Prefer localization as documented in [Localization note](#localization-note).
327+
328+
```js
329+
<RNDateTimePicker locale="es-ES" />
330+
```
331+
320332
#### `is24Hour` (`optional`, `Windows and Android only`)
321333

322334
Allows changing of the time picker to a 24 hour format. By default, this value is decided automatcially based on the user's chosen locale and other preferences.

docs/images/ios_date_new.png

642 KB
Loading
File renamed without changes.

example/App.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ export const App = () => {
114114
backgroundColor: isDarkMode ? Colors.dark : Colors.lighter,
115115
};
116116

117-
const toggleMinMaxDate = () => {
117+
const toggleMinMaxDateInUTC = () => {
118+
setTzOffsetInMinutes(0);
119+
118120
const startOfTodayUTC = sourceMoment.utc().startOf('day').toDate();
119121
setMinimumDate(maximumDate ? undefined : startOfTodayUTC);
120122
const endOfTomorrowUTC = sourceMoment
@@ -285,7 +287,7 @@ export const App = () => {
285287
<Button
286288
testID="setMinMax"
287289
onPress={() => {
288-
toggleMinMaxDate();
290+
toggleMinMaxDateInUTC();
289291
setShow(true);
290292
}}
291293
title="toggleMinMaxDate"

example/e2e/detoxTest.spec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ describe('Example', () => {
189189

190190
it('should let you pick tomorrow but not yesterday when setting min/max', async () => {
191191
await elementById('DateTimePickerScrollView').scrollTo('bottom');
192-
await elementById('setTzOffsetToZero').tap();
193192
await elementById('setMinMax').tap();
194193

195194
if (isIOS()) {
@@ -207,10 +206,10 @@ describe('Example', () => {
207206

208207
// Ensure you can't select yesterday (Android)
209208
const focusTwelethOfNovemberInCalendar = async () => {
210-
for (var i = 0; i < 4; i++) {
209+
for (let i = 0; i < 4; i++) {
211210
await uiDevice.pressDPadDown();
212211
}
213-
for (var i = 0; i < 3; i++) {
212+
for (let i = 0; i < 3; i++) {
214213
await uiDevice.pressDPadLeft();
215214
}
216215
};
@@ -222,10 +221,10 @@ describe('Example', () => {
222221
// Ensure you can select tomorrow (Android)
223222
await userOpensPicker({mode: 'date', display: getPickerDisplay()});
224223
const focusFourteenthOfNovemberInCalendar = async () => {
225-
for (var i = 0; i < 5; i++) {
224+
for (let i = 0; i < 5; i++) {
226225
await uiDevice.pressDPadDown();
227226
}
228-
for (var i = 0; i < 2; i++) {
227+
for (let i = 0; i < 2; i++) {
229228
await uiDevice.pressDPadLeft();
230229
}
231230
};

ios/RNDateTimePickerManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ + (NSString*) datepickerStyleToString: (UIDatePickerStyle) style API_AVAILABLE(
9797
}
9898

9999
RCT_EXPORT_VIEW_PROPERTY(date, NSDate)
100+
RCT_EXPORT_VIEW_PROPERTY(locale, NSLocale)
100101
RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSDate)
101102
RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSDate)
102103
RCT_EXPORT_VIEW_PROPERTY(minuteInterval, NSInteger)

src/datetimepicker.ios.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const getDisplaySafe = (display: IOSDisplay): IOSDisplay => {
4343

4444
export default function Picker({
4545
value,
46+
locale,
4647
maximumDate,
4748
minimumDate,
4849
style,
@@ -115,6 +116,7 @@ export default function Picker({
115116
ref={_picker}
116117
style={StyleSheet.compose(heightStyle, style)}
117118
date={dates.value}
119+
locale={locale !== null && locale !== '' ? locale : undefined}
118120
maximumDate={dates.maximumDate}
119121
minimumDate={dates.minimumDate}
120122
mode={mode}

src/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export type IOSNativeProps = Readonly<
6767
BaseProps & {
6868
date?: Date;
6969

70+
/**
71+
* The date picker locale.
72+
*/
73+
locale?: string;
74+
7075
/**
7176
* The interval at which minutes can be selected.
7277
*/

src/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ export type IOSNativeProps = $ReadOnly<{|
8888
...BaseProps,
8989
date?: ?Date,
9090

91+
/**
92+
* The date picker locale.
93+
*/
94+
locale?: ?string,
95+
9196
/**
9297
* The interval at which minutes can be selected.
9398
*/

0 commit comments

Comments
 (0)