|
1 | | -# react-native-datepicker-timepicker-dialog |
2 | | -This react-native library provides the cross-platform components for to open the date, time picker dialogs in an effective way. It uses the native DatePickerAndroid & DatePickerIOS components. |
| 1 | +# react-native-datepicker-dialog |
| 2 | +This library provides the cross-platform components for to open the date picker dialogs in an effective way. It uses the native DatePickerAndroid & DatePickerIOS components. It will work for both ios & android. |
| 3 | + |
| 4 | +- [Getting Started](https://github.com/pandiaraj44/react-native-datepicker-dialog#getting-started) |
| 5 | +- [ Example](https://github.com/pandiaraj44/react-native-datepicker-dialog#example) |
| 6 | +- [Documentation](https://github.com/pandiaraj44/react-native-datepicker-dialog#documentation) |
| 7 | +- [References](https://github.com/pandiaraj44/react-native-datepicker-dialog#references) |
| 8 | +- [Demo](https://github.com/pandiaraj44/react-native-datepicker-dialog#demo) |
| 9 | + |
| 10 | +# Getting Started |
| 11 | + |
| 12 | +**1. Install** |
| 13 | +` npm i react-native-datepicker-dialog --save. ` |
| 14 | + |
| 15 | +**2. Import DatePickerDialog component** |
| 16 | + |
| 17 | +`import { DatePickerDialog } from 'react-native-datepicker-dialog'` |
| 18 | + |
| 19 | +**3. Place the dialog component at end of your views and assign the references, event handlers to it** |
| 20 | +``` |
| 21 | +<DatePickerDialog ref="dobDialog" onDatePicked={this.onDOBDatePicked.bind(this)} /> |
| 22 | +<DatePickerDialog ref="journeyDialog" onDatePicked={this.onJourneyDatePicked.bind(this)} /> |
| 23 | +
|
| 24 | +``` |
| 25 | +``` |
| 26 | +onDOBDatePicked = (date) => { |
| 27 | + //Here you will get the selected date |
| 28 | + this.setState({ |
| 29 | + dobDate: date, |
| 30 | + dobText: moment(date).format('DD-MMM-YYYY') |
| 31 | + }); |
| 32 | + } |
| 33 | +``` |
| 34 | +**4. Open the date picker dialog** |
| 35 | +``` |
| 36 | +this.refs.dobDialog.open({ |
| 37 | + date: new Date(), |
| 38 | + maxDate: new Date() //To restirct future date |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +It will work for both ios & android. |
| 43 | + |
| 44 | +# Example |
| 45 | +``` |
| 46 | +/** |
| 47 | + * Sample React Native App |
| 48 | + * https://github.com/facebook/react-native |
| 49 | + * @flow |
| 50 | + */ |
| 51 | +
|
| 52 | +import React, { Component } from 'react'; |
| 53 | +import { |
| 54 | + AppRegistry, |
| 55 | + StyleSheet, |
| 56 | + Text, |
| 57 | + View, |
| 58 | + TouchableOpacity, |
| 59 | +} from 'react-native'; |
| 60 | +
|
| 61 | +import { DatePickerDialog } from 'react-native-datepicker-dialog' |
| 62 | +import moment from 'moment'; |
| 63 | +
|
| 64 | +export default class DatePickerTimePickerDialog extends Component { |
| 65 | +
|
| 66 | + constructor(props){ |
| 67 | + super(props); |
| 68 | + this.state = { |
| 69 | + dobText: '', |
| 70 | + dobDate: null, |
| 71 | + journeyText: '', |
| 72 | + journeyDate: null, |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + /** |
| 77 | + * DOB textbox click listener |
| 78 | + */ |
| 79 | + onDOBPress = () => { |
| 80 | + let dobDate = this.state.dobDate; |
| 81 | +
|
| 82 | + if(!dobDate || dobDate == null){ |
| 83 | + dobDate = new Date(); |
| 84 | + this.setState({ |
| 85 | + dobDate: dobDate |
| 86 | + }); |
| 87 | + } |
| 88 | +
|
| 89 | + //To open the dialog |
| 90 | + this.refs.dobDialog.open({ |
| 91 | + date: dobDate, |
| 92 | + maxDate: new Date() //To restirct future date |
| 93 | + }); |
| 94 | +
|
| 95 | + } |
| 96 | +
|
| 97 | + /** |
| 98 | + * Call back for dob date picked event |
| 99 | + * |
| 100 | + */ |
| 101 | + onDOBDatePicked = (date) => { |
| 102 | + this.setState({ |
| 103 | + dobDate: date, |
| 104 | + dobText: moment(date).format('DD-MMM-YYYY') |
| 105 | + }); |
| 106 | + } |
| 107 | +
|
| 108 | +
|
| 109 | + /** |
| 110 | + * Journey date textbox click listener |
| 111 | + */ |
| 112 | + onJourneyDatePress = () => { |
| 113 | + let journeyDate = this.state.journeyDate; |
| 114 | +
|
| 115 | + if(!journeyDate || journeyDate == null){ |
| 116 | + journeyDate = new Date(); |
| 117 | + this.setState({ |
| 118 | + journeyDate: journeyDate |
| 119 | + }); |
| 120 | + } |
| 121 | +
|
| 122 | + //To open the dialog |
| 123 | + this.refs.journeyDialog.open({ |
| 124 | + date: journeyDate, |
| 125 | + minDate: new Date() //To restirct past date |
| 126 | + }); |
| 127 | +
|
| 128 | + } |
| 129 | +
|
| 130 | + /** |
| 131 | + * Call back for journey date picked event |
| 132 | + * |
| 133 | + */ |
| 134 | + onJourneyDatePicked = (date) => { |
| 135 | + this.setState({ |
| 136 | + journeyDate: date, |
| 137 | + journeyText: moment(date).format('DD MMM, YYYY') |
| 138 | + }); |
| 139 | + } |
| 140 | +
|
| 141 | + render() { |
| 142 | + return ( |
| 143 | + <View style={styles.container}> |
| 144 | +
|
| 145 | + <Text style={styles.content}> |
| 146 | + Date Picker Dialog Example |
| 147 | + </Text> |
| 148 | +
|
| 149 | + <View style={{flex:1, marginTop:10}}> |
| 150 | + <Text>DOB</Text> |
| 151 | + <TouchableOpacity onPress={this.onDOBPress.bind(this)} > |
| 152 | + <View style={styles.datePickerBox}> |
| 153 | + <Text style={styles.datePickerText}>{this.state.dobText}</Text> |
| 154 | + </View> |
| 155 | + </TouchableOpacity> |
| 156 | +
|
| 157 | + <Text style={{marginTop: 20}}>Journey Date</Text> |
| 158 | + <TouchableOpacity onPress={this.onJourneyDatePress.bind(this)} > |
| 159 | + <View style={styles.datePickerBox}> |
| 160 | + <Text style={styles.datePickerText}>{this.state.journeyText}</Text> |
| 161 | + </View> |
| 162 | + </TouchableOpacity> |
| 163 | +
|
| 164 | + </View> |
| 165 | +
|
| 166 | + {/* Place the dialog component at end of your views and assign the references, event handlers to it.*/} |
| 167 | + <DatePickerDialog ref="dobDialog" onDatePicked={this.onDOBDatePicked.bind(this)} /> |
| 168 | + <DatePickerDialog ref="journeyDialog" onDatePicked={this.onJourneyDatePicked.bind(this)} /> |
| 169 | +
|
| 170 | + </View> |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
| 174 | +
|
| 175 | +const styles = StyleSheet.create({ |
| 176 | + container: { |
| 177 | + flex: 1, |
| 178 | + padding: 10, |
| 179 | + backgroundColor: '#FFFFFF' |
| 180 | + }, |
| 181 | + content: { |
| 182 | + fontSize: 20, |
| 183 | + textAlign: 'center', |
| 184 | + margin: 10, |
| 185 | + }, |
| 186 | + datePickerBox:{ |
| 187 | + marginTop: 9, |
| 188 | + borderColor: '#ABABAB', |
| 189 | + borderWidth: 0.5, |
| 190 | + padding: 0, |
| 191 | + borderTopLeftRadius: 4, |
| 192 | + borderTopRightRadius: 4, |
| 193 | + borderBottomLeftRadius: 4, |
| 194 | + borderBottomRightRadius: 4, |
| 195 | + height: 38, |
| 196 | + justifyContent:'center' |
| 197 | + }, |
| 198 | + datePickerText: { |
| 199 | + fontSize: 14, |
| 200 | + marginLeft: 5, |
| 201 | + borderWidth: 0, |
| 202 | + color: '#121212', |
| 203 | + }, |
| 204 | +}); |
| 205 | +
|
| 206 | +AppRegistry.registerComponent('DatePickerTimePickerDialog', () => DatePickerTimePickerDialog); |
| 207 | +
|
| 208 | +``` |
| 209 | +# Documentation |
| 210 | +### Instance Methods |
| 211 | +**1. open(options: Object)** - Opens the standard date picker dialog |
| 212 | + |
| 213 | + The available keys for the `options` object are: |
| 214 | + * `date` (`Date` object or timestamp in milliseconds) - date to show by default |
| 215 | + * `minDate` (`Date` or timestamp in milliseconds) - minimum date that can be selected |
| 216 | + * `maxDate` (`Date` object or timestamp in milliseconds) - minimum date that can be selected |
| 217 | + |
| 218 | + |
| 219 | +**2. getSelectedDate** - It will return the selected date (@return date object) |
| 220 | + |
| 221 | +### Props |
| 222 | +**1. onDatePicked: PropTypes.func** - Date picked handler. |
| 223 | +`This method will be called when the user selected the date from picker.The first and only argument is a Date object representing the picked date and time.` |
| 224 | + |
| 225 | +**2. onCancel: PropTypes.func** - Date Cancelled handler. |
| 226 | +`This method will be called when the user dismissed the picker.` |
| 227 | + |
| 228 | +# References |
| 229 | + |
| 230 | +- https://facebook.github.io/react-native/docs/datepickerandroid.html |
| 231 | +- http://facebook.github.io/react-native/releases/0.38/docs/datepickerios.html#datepickerios |
| 232 | +- https://github.com/facebook/react-native/commit/eaccd7e82e228d744e6aef34b332d99c1ff616a5 |
| 233 | + |
| 234 | +# Demo |
| 235 | + |
| 236 | +## Android |
| 237 | + |
| 238 | + |
| 239 | +### Ios |
| 240 | + |
0 commit comments