Skip to content

Commit 4d1ed41

Browse files
authored
Merge pull request #1 from corymsmith/Add-datetime-support
Adding support for datetime
2 parents 009ad57 + e03526e commit 4d1ed41

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class DateTimePickerTester extends Component {
6161
| visible | bool | false | Show the datetime picker? |
6262
| onConfirm | func | **REQUIRED** | Funcion called on date picked. |
6363
| onCancel | func | **REQUIRED** | Funcion called on dismiss. |
64-
| mode | string | 'date' | Datepicker? 'date' Timepicker? 'time' |
64+
| mode | string | 'date' | Datepicker? 'date' Timepicker? 'time' Both? 'datetime' |
6565
| date | obj | new Date() | Initial selected date/time |
6666
| titleIOS | string | 'Pick a date' | The title text on iOS |
6767
| confirmTextIOS | string | 'Confirm' | The text on the confirm button on iOS |

src/CustomDatePickerAndroid/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import moment from 'moment'
55
export default class CustomDatePickerAndroid extends Component {
66
static propTypes = {
77
date: PropTypes.instanceOf(Date),
8-
mode: PropTypes.oneOf(['date', 'time']),
8+
mode: PropTypes.oneOf(['date', 'time', 'datetime']),
99
onCancel: PropTypes.func.isRequired,
1010
onConfirm: PropTypes.func.isRequired,
1111
visible: PropTypes.bool
@@ -19,7 +19,7 @@ export default class CustomDatePickerAndroid extends Component {
1919

2020
componentDidUpdate = (prevProps) => {
2121
if (!prevProps.visible && this.props.visible) {
22-
if (this.props.mode === 'date') {
22+
if (this.props.mode === 'date' || this.props.mode === 'datetime') {
2323
this._showDatePickerAndroid()
2424
} else {
2525
this._showTimePickerAndroid()
@@ -34,7 +34,23 @@ export default class CustomDatePickerAndroid extends Component {
3434
})
3535
if (action !== DatePickerAndroid.dismissedAction) {
3636
const date = moment({ year, month, day }).toDate()
37-
this.props.onConfirm(date)
37+
38+
if (this.props.mode === 'datetime') {
39+
// Prepopulate and show time picker
40+
const timeOptions = !this.props.date ? {} : {
41+
hour: this.props.date.getHours(),
42+
minute: this.props.date.getMinutes()
43+
};
44+
45+
TimePickerAndroid.open(timeOptions).then(({action, minute, hour}) => {
46+
if (action === TimePickerAndroid.timeSetAction) {
47+
let selectedDate = new Date(year, month, day, hour, minute);
48+
this.props.onConfirm(selectedDate);
49+
}
50+
});
51+
} else {
52+
this.props.onConfirm(date)
53+
}
3854
} else {
3955
this.props.onCancel()
4056
}

src/CustomDatePickerIOS/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class CustomDatePickerIOS extends Component {
99
cancelTextIOS: PropTypes.string,
1010
confirmTextIOS: PropTypes.string,
1111
date: PropTypes.instanceOf(Date),
12-
mode: PropTypes.oneOf(['date', 'time']),
12+
mode: PropTypes.oneOf(['date', 'time', 'datetime']),
1313
onConfirm: PropTypes.func.isRequired,
1414
onCancel: PropTypes.func.isRequired,
1515
titleIOS: PropTypes.string,

0 commit comments

Comments
 (0)