Skip to content

Commit

Permalink
fix: year and month disable don't right(#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiong10 committed Sep 28, 2018
1 parent 994aa6e commit 758008d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
77 changes: 50 additions & 27 deletions src/calendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default {
},
type: {
type: String,
default: 'date'
default: 'date' // ['date', 'datetime'] zendy added 'month', 'year', mxie added "time"
},
dateFormat: {
type: String,
Expand Down Expand Up @@ -179,6 +179,12 @@ export default {
},
months () {
return this.t('months')
},
notBeforeTime () {
return this.getCriticalTime(this.notBefore)
},
notAfterTime () {
return this.getCriticalTime(this.notAfter)
}
},
watch: {
Expand Down Expand Up @@ -226,39 +232,56 @@ export default {
updateNow (value) {
this.now = value ? new Date(value) : new Date()
},
isDisabledTime (date, startAt, endAt) {
const time = new Date(date).getTime()
const notBefore = this.notBefore && (time < new Date(this.notBefore))
const notAfter = this.notAfter && (time > new Date(this.notAfter))
startAt = startAt === undefined ? this.startAt : startAt
startAt = startAt && (time < new Date(startAt))
endAt = endAt === undefined ? this.endAt : endAt
endAt = endAt && (time > new Date(endAt))
return notBefore || notAfter || startAt || endAt
},
isDisabledDate (date, startAt, endAt) {
const time = new Date(date).getTime()
const notBefore = this.notBefore && (time < new Date(this.notBefore).setHours(0, 0, 0, 0))
const notAfter = this.notAfter && (time > new Date(this.notAfter).setHours(0, 0, 0, 0))
startAt = startAt === undefined ? this.startAt : startAt
startAt = startAt && (time < new Date(startAt).setHours(0, 0, 0, 0))
endAt = endAt === undefined ? this.endAt : endAt
endAt = endAt && (time > new Date(endAt).setHours(0, 0, 0, 0))
let disabledDays = false
getCriticalTime (value) {
if (!value) {
return null
}
const date = new Date(value)
if (this.type === 'year') {
return new Date(date.getFullYear(), 0).getTime()
} else if (this.type === 'month') {
return new Date(date.getFullYear(), date.getMonth()).getTime()
} else if (this.type === 'date') {
return date.setHours(0, 0, 0, 0)
}
return date.getTime()
},
inBefore (time, startAt) {
startAt = startAt || this.startAt
return (this.notBeforeTime && time < this.notBeforeTime) ||
(startAt && time < this.getCriticalTime(startAt))
},
inAfter (time, endAt) {
endAt = endAt || this.endAt
return (this.notAfterTime && time > this.notAfterTime) ||
(endAt && time > this.getCriticalTime(endAt))
},
inDisabledDays (time) {
if (Array.isArray(this.disabledDays)) {
disabledDays = this.disabledDays.some(v => new Date(v).setHours(0, 0, 0, 0) === time)
return this.disabledDays.some(v => this.getCriticalTime(v) === time)
} else if (typeof this.disabledDays === 'function') {
disabledDays = this.disabledDays(new Date(date))
return this.disabledDays(new Date(time))
}
return notBefore || notAfter || disabledDays || startAt || endAt
return false
},
isDisabledYear (year) {
const date = new Date(year, this.calendarMonth)
return this.isDisabledDate(date)
const time = new Date(year, 0).getTime()
const maxTime = new Date(year + 1, 0).getTime() - 1
return this.inBefore(maxTime) || this.inAfter(time) || (this.type === 'year' && this.inDisabledDays(time))
},
isDisabledMonth (month) {
const date = new Date(this.calendarYear, month)
return this.isDisabledDate(date)
const time = new Date(this.calendarYear, month).getTime()
const maxTime = new Date(this.calendarYear, month + 1).getTime() - 1
return this.inBefore(maxTime) || this.inAfter(time) || (this.type === 'month' && this.inDisabledDays(time))
},
isDisabledDate (date) {
const time = new Date(date).getTime()
const maxTime = new Date(date).setHours(23, 59, 59, 999)
return this.inBefore(maxTime) || this.inAfter(time) || this.inDisabledDays(time)
},
isDisabledTime (date, startAt, endAt) {
const time = new Date(date).getTime()
return this.inBefore(time, startAt) || this.inAfter(time, endAt) || this.inDisabledDays(time)
},
selectDate (date) {
if (this.type === 'datetime') {
Expand Down
2 changes: 1 addition & 1 deletion src/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ export default {
const value = event.target.value
if (this.editable && this.userInput !== null) {
const calendar = this.$children[0]
const checkDate = calendar.type === 'date' ? calendar.isDisabledDate : calendar.isDisabledTime
const checkDate = calendar.isDisabledTime
if (this.range) {
const range = value.split(` ${this.rangeSeparator} `)
if (range.length === 2) {
Expand Down

0 comments on commit 758008d

Please sign in to comment.