1 | # react-native-modal-datetime-picker
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/react-native-modal-datetime-picker.svg)](https://badge.fury.io/js/react-native-modal-datetime-picker)
|
4 | ![Supports Android and iOS](https://img.shields.io/badge/platforms-android%20|%20ios-lightgrey.svg)
|
5 |
|
6 | A declarative cross-platform react-native date and time picker.
|
7 |
|
8 | <p align="center">
|
9 | <img src="./.github/images/datetimepicker-android.gif" height="400" />
|
10 | <img src="./.github/images/datetimepicker-ios.gif" height="400" />
|
11 | </p>
|
12 |
|
13 | This library exposes a cross-platform interface for showing the native date-picker and time-picker inside a modal, providing a unified user and developer experience.
|
14 |
|
15 | Under the hood this library is using [`@react-native-community/datetimepicker`](https://github.com/react-native-community/react-native-datetimepicker).
|
16 |
|
17 | ## Setup (for non-Expo projects)
|
18 |
|
19 | If your project is not using [Expo](https://expo.io/), install the library and the community date/time picker using npm or yarn:
|
20 |
|
21 | ```bash
|
22 | # using npm
|
23 | $ npm i react-native-modal-datetime-picker @react-native-community/datetimepicker
|
24 |
|
25 | # using yarn
|
26 | $ yarn add react-native-modal-datetime-picker @react-native-community/datetimepicker
|
27 | ```
|
28 |
|
29 | Please notice that the `@react-native-community/datetimepicker` package is a native module so [**it might require manual linking**](https://github.com/react-native-community/react-native-datetimepicker#getting-started).
|
30 |
|
31 | ## Setup (for Expo projects)
|
32 |
|
33 | If your project is using [Expo](https://expo.io/), install the library and the community date/time picker using the [Expo CLI](https://docs.expo.io/versions/latest/workflow/expo-cli/):
|
34 |
|
35 | ```bash
|
36 | expo install react-native-modal-datetime-picker @react-native-community/datetimepicker
|
37 | ```
|
38 |
|
39 | ## Usage
|
40 |
|
41 | ```javascript
|
42 | import React, { useState } from "react";
|
43 | import { Button, View } from "react-native";
|
44 | import DateTimePickerModal from "react-native-modal-datetime-picker";
|
45 |
|
46 | const Example = () => {
|
47 | const [isDatePickerVisible, setDatePickerVisibility] = useState(false);
|
48 |
|
49 | const showDatePicker = () => {
|
50 | setDatePickerVisibility(true);
|
51 | };
|
52 |
|
53 | const hideDatePicker = () => {
|
54 | setDatePickerVisibility(false);
|
55 | };
|
56 |
|
57 | const handleConfirm = (date) => {
|
58 | console.warn("A date has been picked: ", date);
|
59 | hideDatePicker();
|
60 | };
|
61 |
|
62 | return (
|
63 | <View>
|
64 | <Button title="Show Date Picker" onPress={showDatePicker} />
|
65 | <DateTimePickerModal
|
66 | isVisible={isDatePickerVisible}
|
67 | mode="date"
|
68 | onConfirm={handleConfirm}
|
69 | onCancel={hideDatePicker}
|
70 | />
|
71 | </View>
|
72 | );
|
73 | };
|
74 |
|
75 | export default Example;
|
76 | ```
|
77 |
|
78 | ## Available props
|
79 |
|
80 | | Name | Type | Default | Description |
|
81 | | ----------------------- | --------- | ------------- | ----------------------------------------------------------------------------------------------- |
|
82 | | cancelTextIOS | string | 'Cancel' | The label of the cancel button (iOS) |
|
83 | | confirmTextIOS | string | 'Confirm' | The label of the confirm button (iOS) |
|
84 | | customCancelButtonIOS | component | | Overrides the default cancel button component (iOS) |
|
85 | | customConfirmButtonIOS | component | | Overrides the default confirm button component (iOS) |
|
86 | | customHeaderIOS | component | | Overrides the default header component (iOS) |
|
87 | | customPickerIOS | component | | Overrides the default native picker component (iOS) |
|
88 | | date | obj | new Date() | Initial selected date/time |
|
89 | | headerTextIOS | string | "Pick a date" | The title text of header (iOS) |
|
90 | | isVisible | bool | false | Show the datetime picker? |
|
91 | | isDarkModeEnabled | bool? | undefined | Forces the dark/light mode if set (otherwise fallbacks to the Appearance color scheme) (iOS) |
|
92 | | modalPropsIOS | object | {} | Additional [modal](https://reactnative.dev/docs/modal) props for iOS |
|
93 | | modalStyleIOS | style | | Style of the modal content (iOS) |
|
94 | | mode | string | "date" | Choose between 'date', 'time', and 'datetime' |
|
95 | | onCancel | func | **REQUIRED** | Function called on dismiss |
|
96 | | onConfirm | func | **REQUIRED** | Function called on date or time picked. It returns the date or time as a JavaScript Date object |
|
97 | | onHide | func | () => null | Called after the hide animation |
|
98 | | pickerContainerStyleIOS | style | | The style of the picker container (iOS) |
|
99 |
|
100 | 👉 Please notice that **all the [`@react-native-community/react-native-datetimepicker`](https://github.com/react-native-community/react-native-datetimepicker) props are also supported**!
|
101 |
|
102 | ## Frequently Asked Questions
|
103 |
|
104 | ### The component is not working as expected
|
105 |
|
106 | Under the hood `react-native-modal-datetime-picker` uses [`@react-native-community/datetimepicker`](https://github.com/react-native-community/react-native-datetimepicker).
|
107 | Before reporting a bug, try swapping `react-native-datetime-picker` with [`@react-native-community/datetimepicker`](https://github.com/react-native-community/react-native-datetimepicker) and, if the issue persists, check if it has already been reported as a an issue there.
|
108 |
|
109 | ### How can I show the timepicker instead of the datepicker?
|
110 |
|
111 | Set the `mode` prop to `time`.
|
112 | You can also display both the datepicker and the timepicker in one step by setting the `mode` prop to `datetime`.
|
113 |
|
114 | ### I can't set the initial date on the picker
|
115 |
|
116 | Please make sure you're using the `date` props (and not the `value` one).
|
117 |
|
118 | ### The picker shows up twice on Android
|
119 |
|
120 | This seems to be a known issue of the [`@react-native-community/datetimepicker`](https://github.com/react-native-community/datetimepicker/issues/54). Please see [this thread](https://github.com/react-native-community/datetimepicker/issues/54) for a couple of workarounds. The solution, as described in [this reply](https://github.com/react-native-datetimepicker/datetimepicker/issues/54#issuecomment-618776550) is hiding the modal, **before doing anything else**.
|
121 |
|
122 | <details><summary><strong>Example of solution using Input + DatePicker</strong></summary>
|
123 | <p>
|
124 | The most common approach for solving this issue when using an <code>Input</code> is:
|
125 | <ul>
|
126 | <li>Wrap your <code>Input</code> with a "<code>Pressable</code>"/<code>Button</code> (<code>TouchableWithoutFeedback</code>/<code>TouchableOpacity</code> + <code>activeOpacity={1}</code> for example)</li>
|
127 | <li>Prevent <code>Input</code> from being focused. You could set <code>editable={false}</code> too for preventing Keyboard opening</li>
|
128 | <li>Triggering your <code>hideModal()</code> callback as a first thing inside <code>onConfirm</code>/<code>onCancel</code> callback props</li>
|
129 | </ul>
|
130 |
|
131 | ```jsx
|
132 | const [isVisible, setVisible] = useState(false);
|
133 | const [date, setDate] = useState('');
|
134 |
|
135 | <TouchableOpacity
|
136 | activeOpaticy={1}
|
137 | onPress={() => setVisible(true)}>
|
138 | <Input
|
139 | value={value}
|
140 | editable={false} // optional
|
141 | />
|
142 | </TouchableOpacity>
|
143 | <DatePicker
|
144 | isVisible={isVisible}
|
145 | onConfirm={(date) => {
|
146 | setVisible(false); // <- first thing
|
147 | setValue(parseDate(date));
|
148 | }}
|
149 | onCancel={() => setVisible(false)}
|
150 | />
|
151 | ```
|
152 | </p>
|
153 | </details>
|
154 |
|
155 | ### How can I set a minimum and/or maximum date?
|
156 |
|
157 | You can use the [`minimumDate`](https://github.com/react-native-datetimepicker/datetimepicker#minimumdate-optional) and [`maximumDate`](https://github.com/react-native-datetimepicker/datetimepicker#maximumdate-optional) props from [`@react-native-community/datetimepicker`](https://github.com/react-native-community/react-native-datetimepicker).
|
158 |
|
159 | ### How do I change the color of the Android date and time pickers?
|
160 |
|
161 | This is more a React-Native specific question than a react-native-modal-datetime-picker one.
|
162 | See issue [#29](https://github.com/mmazzarolo/react-native-modal-datetime-picker/issues/29) and [#106](https://github.com/mmazzarolo/react-native-modal-datetime-picker/issues/106) for some solutions.
|
163 |
|
164 | ### How to set 24 hours in iOS ?
|
165 |
|
166 | The `is24Hour` prop is only available on Android but you can use a small hack for enabling it on iOS by setting the picker timezone to `en_GB`:
|
167 |
|
168 | ```js
|
169 | <DatePicker
|
170 | mode="time"
|
171 | locale="en_GB" // Use "en_GB" here
|
172 | date={new Date()}
|
173 | />
|
174 | ```
|
175 |
|
176 | ### How can I set an automatic locale in iOS
|
177 |
|
178 | The datepicker can adjust by itself the locale (`fr_FR`, `en_GB`...) depending on the user's device locale.
|
179 | To do so, edit your `AppDelegate.m` file and add the following to `didFinishLaunchingWithOptions`.
|
180 |
|
181 | ```objc
|
182 | // Force DatePicker locale to current language (for: 24h or 12h format, full day names etc...)
|
183 | NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
|
184 | [[UIDatePicker appearance] setLocale:[[NSLocale alloc]initWithLocaleIdentifier:currentLanguage]];
|
185 | ```
|
186 |
|
187 | ### The picker is not showing the right layout on iOS >= 14
|
188 |
|
189 | Please make sure you're on the latest version of `react-native-modal-datetime-picker` and of the [`@react-native-community/datetimepicker`](https://github.com/react-native-community/datetimepicker).
|
190 | [We already closed several iOS 14 issues that were all caused by outdated/cached versions of the community datetimepicker](https://github.com/mmazzarolo/react-native-modal-datetime-picker/issues?q=%22ios+14%22).
|
191 |
|
192 | ### I can't show an alert after the picker has been hidden (on iOS)
|
193 |
|
194 | Unfortunately this is a know issue with React-Native on iOS. Even by using the `onHide` callback exposed by `react-native-modal-datetime-picker` you might not be able to show the (native) alert successfully. The only workaround that seems to work consistently for now is to wrap showing the alter in a setTimeout 😔:
|
195 | ```js
|
196 | const handleHide = () => {
|
197 | setTimeout(() => Alert.alert("Hello"), 0);
|
198 | };
|
199 | ```
|
200 |
|
201 | See issue [#512](https://github.com/mmazzarolo/react-native-modal-datetime-picker/issues/512) for more info.
|
202 |
|
203 | ### The date in `onConfirm` doesn't match the picked date (on iOS)
|
204 |
|
205 | On iOS, clicking the "Confirm" button while the spinner is still in motion — even just _slightly_ in motion — will cause the `onConfirm` callback to return the initial date instead of the picked one. This is is a long standing iOS issue (that can happen even on native app like the iOS calendar) and there's no failproof way to fix it on the JavaScript side.
|
206 | See [this GitHub gist](https://gist.github.com/SudoPlz/6959001879fbfcc7e2aa42a428a5265c) for an example of how it might be solved at the native level — but keep in mind it won't work on this component until it has been merged into the official React-Native repo.
|
207 |
|
208 | Related issue in the React-Native repo [here](https://github.com/facebook/react-native/issues/8169).
|
209 |
|
210 |
|
211 | ### How do I make it work with snapshot testing?
|
212 |
|
213 | See issue [#216](https://github.com/mmazzarolo/react-native-modal-datetime-picker/issues/216) for a possible workaround.
|
214 |
|
215 | ## Contributing
|
216 |
|
217 | Please see the [contributing guide](./.github/CONTRIBUTING.md).
|
218 |
|
219 | ## License
|
220 |
|
221 | The library is released under the MIT license. For more details see [`LICENSE`](/LICENSE.md).
|