A declarative cross-platform react-native date and time picker.
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.
Under the hood this library is using @react-native-community/datetimepicker
.
If your project is not using Expo, install the library and the community date/time picker using npm or yarn:
# using npm
$ npm i react-native-modal-datetime-picker @react-native-community/datetimepicker
# using yarn
$ yarn add react-native-modal-datetime-picker @react-native-community/datetimepicker
Please notice that the @react-native-community/datetimepicker
package is a native module so it might require manual linking.
If your project is using Expo, install the library and the community date/time picker using the Expo CLI:
expo install react-native-modal-datetime-picker @react-native-community/datetimepicker
To ensure the picker theme respects the device theme, you should also configure the appearance styles in your app.json
this way:
{
"expo": {
"userInterfaceStyle": "automatic"
}
}
Refer to the Appearance documentation on Expo for more info.
import React, { useState } from "react";
import { Button, View } from "react-native";
import DateTimePickerModal from "react-native-modal-datetime-picker";
const Example = () => {
const [isDatePickerVisible, setDatePickerVisibility] = useState(false);
const showDatePicker = () => {
setDatePickerVisibility(true);
};
const hideDatePicker = () => {
setDatePickerVisibility(false);
};
const handleConfirm = (date) => {
console.warn("A date has been picked: ", date);
hideDatePicker();
};
return (
<View>
<Button title="Show Date Picker" onPress={showDatePicker} />
<DateTimePickerModal
isVisible={isDatePickerVisible}
mode="date"
onConfirm={handleConfirm}
onCancel={hideDatePicker}
/>
</View>
);
};
export default Example;
Name | Type | Default | Description |
---|---|---|---|
cancelButtonTestID | string | Used to locate cancel button in end-to-end tests. | |
confirmButtonTestID | string | Used to locate confirm button in end-to-end tests. | |
cancelTextIOS | string | 'Cancel' | The label of the cancel button (iOS) |
confirmTextIOS | string | 'Confirm' | The label of the confirm button (iOS) |
customCancelButtonIOS | component | Overrides the default cancel button component (iOS) | |
customConfirmButtonIOS | component | Overrides the default confirm button component (iOS) | |
customHeaderIOS | component | Overrides the default header component (iOS) | |
customPickerIOS | component | Overrides the default native picker component (iOS) | |
date | obj | new Date() | Initial selected date/time |
headerTextIOS | string | "Pick a date" | The title text of header (iOS) |
isVisible | bool | false | Show the datetime picker? |
isDarkModeEnabled | bool? | undefined | Forces the picker dark/light mode if set (otherwise fallbacks to the Appearance color scheme) (iOS) |
isHeaderVisibleIOS | bool? | false | Show the built-in header on iOS (deprecated — use customHeaderIOS instead) |
modalPropsIOS | object | {} | Additional modal props for iOS |
modalStyleIOS | style | Style of the modal content (iOS) | |
mode | string | "date" | Choose between 'date', 'time', and 'datetime' |
onCancel | func | REQUIRED | Function called on dismiss |
onConfirm | func | REQUIRED | Function called on date or time picked. It returns the date or time as a JavaScript Date object |
onHide | func | () => null | Called after the hide animation |
pickerContainerStyleIOS | style | The style of the picker container (iOS) | |
pickerStyleIOS | style | The style of the picker component wrapper (iOS) |
@react-native-community/react-native-datetimepicker
props are also supported!
Under the hood react-native-modal-datetime-picker
uses @react-native-community/datetimepicker
.Before reporting a bug, try swapping react-native-datetime-picker
with @react-native-community/datetimepicker
and, if the issue persists, check if it has already been reported as a an issue there.
Set the mode
prop to time
.You can also display both the datepicker and the timepicker in one step by setting the mode
prop to datetime
.
Please make sure you're using the date
props (and not the value
one).
Yes!
You can set the display
prop (that we'll pass down to react-native-datetimepicker
) to inline
to use the new iOS 14 picker.
Please notice that you should probably avoid using this new style with a time-only picker (so with
mode
set totime
) because it doesn't suit well this use case.
This seems to be a known issue of the @react-native-community/datetimepicker
. Please see this thread for a couple of workarounds. The solution, as described in this reply is hiding the modal, before doing anything else.
The most common approach for solving this issue when using an Input
is:
Input
with a "Pressable
"/Button
(TouchableWithoutFeedback
/TouchableOpacity
+ activeOpacity={1}
for example)Input
from being focused. You could set editable={false}
too for preventing Keyboard openinghideModal()
callback as a first thing inside onConfirm
/onCancel
callback propsconst [isVisible, setVisible] = useState(false);
const [date, setDate] = useState('');
<TouchableOpacity
activeOpaticy={1}
onPress={() => setVisible(true)}>
<Input
value={value}
editable={false} // optional
/>
</TouchableOpacity>
<DatePicker
isVisible={isVisible}
onConfirm={(date) => {
setVisible(false); // <- first thing
setValue(parseDate(date));
}}
onCancel={() => setVisible(false)}
/>
You can use the minimumDate
and maximumDate
props from @react-native-community/datetimepicker
.
This is more a React-Native specific question than a react-native-modal-datetime-picker one.
See issue #29 and #106 for some solutions.
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
:
<DatePicker
mode="time"
locale="en_GB" // Use "en_GB" here
date={new Date()}
/>
Under the hood this library is using @react-native-community/datetimepicker
. You can't change the language/locale from react-native-modal-datetime-picker
. Locale/language is set at the native level, on the device itself.
On iOS, you can set an automatic detection of the locale (fr_FR
, en_GB
, ...) depending on the user's device locale.To do so, edit your AppDelegate.m
file and add the following to didFinishLaunchingWithOptions
.
// Force DatePicker locale to current language (for: 24h or 12h format, full day names etc...)
NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
[[UIDatePicker appearance] setLocale:[[NSLocale alloc]initWithLocaleIdentifier:currentLanguage]];
Please make sure you're on the latest version of react-native-modal-datetime-picker
and of the @react-native-community/datetimepicker
.We already closed several iOS 14 issues that were all caused by outdated/cached versions of the community datetimepicker.
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
const handleHide = () => {
setTimeout(() => Alert.alert("Hello"), 0);
};
See issue #512 for more info.
onConfirm
not match the picked date (on iOS)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.
See this GitHub gist 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.
Related issue in the React-Native repo here.
See issue #216 for a possible workaround.
Please see the contributing guide.
The library is released under the MIT license. For more details see LICENSE
.
属性 该控件可以使用View控件的全部属性 onValueChange function 某一项被选中时执行此回调。调用时带有如下参数: itemValue: 被选中项的value属性 itemPosition: 被选中项在picker中的索引位置 selectedValue any 可以是一个字符串或者一个数字,item所选中的值 style pickerStyleType 传入style样式,
原帖: https://github.com/mmazzarolo/react-native-modal-datetime-picker DatePickerIOS选择24小时的方法: How to set 24 hours in iOS ? The is24Hour prop is only available on Android but you use a small hack for
轻提示 react-native-root-toast git 链接:https://github.com/magicismight/react-native-root-toast 获取设备信息 react-native-device-info git 链接:https://github.com/rebeccahughes/react-native-device-info 极光推送 jpush-r
npm install react-native-datepicker --save import DatePicker from 'react-native-datepicker'; <View> <DatePicker style={{width: 200}} date={this.state.date} mode="date"
1.安装依赖: npm i react-native-date-picker --save // 日期选择组件 npm i react-native-modal --save // 模态框组件 2.代码: import React, { Component } from 'react' import { StyleSheet, SafeAreaView, Touchab
react-native-modal-datetime-picker 转载于:https://www.cnblogs.com/lude1994/p/10800114.html
1.数据状态管理工具原理解析 数据状态管理工具的原理是通过mobx创建一个“可观察”的数据,然后使用mobx中名为 observer 的方法包裹组件,将我们封装的组件变成观察者,成为观察者组件。 成为观察者组件,当组件内使用的“可观察数据”发生改变时,会触发render,更新视图。从而实现数据驱动视图的功能。 2.数据状态管理工具封装基础思想 在封装数据状态管理工具的时候,我们可以将其封装成一个类
react-native-modal 是 React Native 的 <Modal> 组件。 使用示例: 'use strict';var React = require('react-native');var Modal = require('react-native-modal');var { AppRegistry, StyleSheet, View, Text } = React;cla
本文向大家介绍react-native 启动React Native Packager,包括了react-native 启动React Native Packager的使用技巧和注意事项,需要的朋友参考一下 示例 在最新版本的React Native上,无需运行打包程序。它将自动运行。 默认情况下,这将在端口8081上启动服务器。要指定服务器所在的端口
百度移动统计SDK支持使用react native框架的H5页面统计,封装好的插件已经在github上开源,相关用法具体请参考:https://github.com/BaiduMobileAnalysis/baidumobstat-react-native。
The React Native environment has a lot of little quirks, so this documentation is aimed at helping smooth those over. Please feel free to create issues on GitHub for recommendations and additions to t
React Native 可以基于目前大热的开源JavaScript库React.js来开发iOS和Android原生App。而且React Native已经用于生产环境——Facebook Groups iOS 应用就是基于它开发的。 React Native的原理是在JavaScript中用React抽象操作系统原生的UI组件,代替DOM元素来渲染,比如以<View>取代<div>,以<Ima
本文向大家介绍react-native setState,包括了react-native setState的使用技巧和注意事项,需要的朋友参考一下 示例 要在应用程序中更改视图,可以使用setState-这将重新渲染您的组件及其任何子组件。setState在新状态和先前状态之间执行浅表合并,并触发组件的重新呈现。 setState 接受键值对象或返回键值对象的函数 键值对象 功能 使用函数对于基于