Skip to content

Commit

Permalink
Merge pull request #1 from corymsmith/Add-datetime-support
Browse files Browse the repository at this point in the history
Adding support for datetime
  • Loading branch information
mmazzarolo authored Oct 5, 2016
2 parents 009ad57 + e03526e commit 4d1ed41
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class DateTimePickerTester extends Component {
| visible | bool | false | Show the datetime picker? |
| onConfirm | func | **REQUIRED** | Funcion called on date picked. |
| onCancel | func | **REQUIRED** | Funcion called on dismiss. |
| mode | string | 'date' | Datepicker? 'date' Timepicker? 'time' |
| mode | string | 'date' | Datepicker? 'date' Timepicker? 'time' Both? 'datetime' |
| date | obj | new Date() | Initial selected date/time |
| titleIOS | string | 'Pick a date' | The title text on iOS |
| confirmTextIOS | string | 'Confirm' | The text on the confirm button on iOS |
Expand Down
22 changes: 19 additions & 3 deletions src/CustomDatePickerAndroid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import moment from 'moment'
export default class CustomDatePickerAndroid extends Component {
static propTypes = {
date: PropTypes.instanceOf(Date),
mode: PropTypes.oneOf(['date', 'time']),
mode: PropTypes.oneOf(['date', 'time', 'datetime']),
onCancel: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired,
visible: PropTypes.bool
Expand All @@ -19,7 +19,7 @@ export default class CustomDatePickerAndroid extends Component {

componentDidUpdate = (prevProps) => {
if (!prevProps.visible && this.props.visible) {
if (this.props.mode === 'date') {
if (this.props.mode === 'date' || this.props.mode === 'datetime') {
this._showDatePickerAndroid()
} else {
this._showTimePickerAndroid()
Expand All @@ -34,7 +34,23 @@ export default class CustomDatePickerAndroid extends Component {
})
if (action !== DatePickerAndroid.dismissedAction) {
const date = moment({ year, month, day }).toDate()
this.props.onConfirm(date)

if (this.props.mode === 'datetime') {
// Prepopulate and show time picker
const timeOptions = !this.props.date ? {} : {
hour: this.props.date.getHours(),
minute: this.props.date.getMinutes()
};

TimePickerAndroid.open(timeOptions).then(({action, minute, hour}) => {
if (action === TimePickerAndroid.timeSetAction) {
let selectedDate = new Date(year, month, day, hour, minute);
this.props.onConfirm(selectedDate);
}
});
} else {
this.props.onConfirm(date)
}
} else {
this.props.onCancel()
}
Expand Down
2 changes: 1 addition & 1 deletion src/CustomDatePickerIOS/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class CustomDatePickerIOS extends Component {
cancelTextIOS: PropTypes.string,
confirmTextIOS: PropTypes.string,
date: PropTypes.instanceOf(Date),
mode: PropTypes.oneOf(['date', 'time']),
mode: PropTypes.oneOf(['date', 'time', 'datetime']),
onConfirm: PropTypes.func.isRequired,
onCancel: PropTypes.func.isRequired,
titleIOS: PropTypes.string,
Expand Down

0 comments on commit 4d1ed41

Please sign in to comment.