Skip to content

Commit

Permalink
fix: [QDateTime] Month 'forward' button has undesirable behaviour #847
Browse files Browse the repository at this point in the history
Also fixed glitch in "date" utils: adjustDate, addToDate & subtractFromDate methods
  • Loading branch information
rstoenescu committed Sep 3, 2017
1 parent 8f819ce commit 39f6138
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/components/datetime/InlineDatetimeIOS.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import { between, capitalize } from '../../utils/format'
import { position } from '../../utils/event'
import { css } from '../../utils/dom'
import { isSameDate } from '../../utils/date'
import { isSameDate, adjustDate } from '../../utils/date'
import DateMixin from './datetime-mixin'
import TouchPan from '../../directives/touch-pan'
Expand Down Expand Up @@ -197,7 +197,7 @@ export default {
},
setMonth (value) {
if (this.editable) {
this.model = new Date(this.model.setMonth(this.__parseTypeValue('month', value) - 1))
this.model = adjustDate(this.model, {month: value})
}
},
setDay (value) {
Expand Down
10 changes: 5 additions & 5 deletions src/components/datetime/InlineDatetimeMat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
small
flat
:color="color"
@click="setMonth(month - 1, true)"
@click="setMonth(month - 1)"
:disabled="beforeMinDays"
icon="keyboard_arrow_left"
></q-btn>
Expand All @@ -118,7 +118,7 @@
small
flat
:color="color"
@click="setMonth(month + 1, true)"
@click="setMonth(month + 1)"
:disabled="afterMaxDays"
icon="keyboard_arrow_right"
></q-btn>
Expand Down Expand Up @@ -222,7 +222,7 @@
import { height, width, offset, cssTransform } from '../../utils/dom'
import { position } from '../../utils/event'
import { QBtn } from '../btn'
import { isSameDate } from '../../utils/date'
import { isSameDate, adjustDate } from '../../utils/date'
import DateMixin from './datetime-mixin'
import Ripple from '../../directives/ripple'
Expand Down Expand Up @@ -383,10 +383,10 @@ export default {
this.model = new Date(this.model.setFullYear(this.__parseTypeValue('year', value)))
}
},
setMonth (value, force) {
setMonth (value) {
if (this.editable) {
this.view = 'day'
this.model = new Date(this.model.setMonth((force ? value : this.__parseTypeValue('month', value)) - 1))
this.model = adjustDate(this.model, {month: value})
}
},
setDay (value) {
Expand Down
22 changes: 21 additions & 1 deletion src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,28 @@ function formatTimezone (offset, delimeter = '') {
return sign + pad(hours) + delimeter + pad(minutes)
}

function setMonth (date, newMonth /* 1-based */) {
const
test = new Date(date.getFullYear(), newMonth, 0, 0, 0, 0, 0),
days = test.getDate()

date.setMonth(newMonth - 1, Math.min(days, date.getDate()))
}

function getChange (date, mod, add) {
const
t = new Date(date),
sign = (add ? 1 : -1)

Object.keys(mod).forEach(key => {
const op = capitalize(key === 'days' ? 'date' : key)
if (key === 'month') {
setMonth(t, t.getMonth() + 1 + sign * mod.month)
return
}

const op = key === 'year'
? 'FullYear'
: capitalize(key === 'days' ? 'date' : key)
t[`set${op}`](t[`get${op}`]() + sign * mod[key])
})
return t
Expand Down Expand Up @@ -98,6 +113,11 @@ export function adjustDate (date, mod, utc) {
prefix = `set${utc ? 'UTC' : ''}`

Object.keys(mod).forEach(key => {
if (key === 'month') {
setMonth(t, mod.month)
return
}

const op = key === 'year'
? 'FullYear'
: key.charAt(0).toUpperCase() + key.slice(1)
Expand Down

0 comments on commit 39f6138

Please sign in to comment.