Skip to content

Commit

Permalink
fix(picker): isseu with default date in disabled range
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWook committed Jan 15, 2021
1 parent 0a2195f commit 01a947d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/components/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import PickerDay from '~/components/PickerDay.vue'
import PickerMonth from '~/components/PickerMonth.vue'
import PickerYear from '~/components/PickerYear.vue'
import Popup from '~/components/Popup.vue'
import DisabledDate from '~/utils/DisabledDate'
const validDate = (val) =>
val === null ||
Expand Down Expand Up @@ -266,6 +267,9 @@ export default {
translation() {
return this.language
},
disabledDatesClass() {
return new DisabledDate(this.utils, this.disabledDates)
},
},
watch: {
initialView() {
Expand All @@ -275,7 +279,8 @@ export default {
this.setPageDate()
},
value(value) {
this.setValue(value)
const parsedValue = this.parseValue(value)
this.setValue(parsedValue)
},
},
mounted() {
Expand Down Expand Up @@ -326,7 +331,14 @@ export default {
*/
init() {
if (this.value) {
this.setValue(this.value)
let parsedValue = this.parseValue(this.value)
const isDateDisabled =
parsedValue && this.disabledDatesClass.isDateDisabled(parsedValue)
if (isDateDisabled) {
parsedValue = null
this.$emit('input', parsedValue)
}
this.setValue(parsedValue)
}
if (this.isInline) {
this.setInitialView()
Expand Down Expand Up @@ -414,18 +426,25 @@ export default {
* @param {Date|String|Number|null} date
*/
setValue(date) {
if (!date) {
this.setPageDate()
this.selectedDate = null
return
}
this.selectedDate = date
this.setPageDate(date)
},
/**
* parse a datepicker value from string/number to date
* @param {Date|String|Number|null} date
*/
parseValue(date) {
let dateTemp = date
if (typeof dateTemp === 'string' || typeof dateTemp === 'number') {
const parsed = new Date(dateTemp)
dateTemp = Number.isNaN(parsed.valueOf()) ? null : parsed
}
if (!dateTemp) {
this.setPageDate()
this.selectedDate = null
return
}
this.selectedDate = dateTemp
this.setPageDate(dateTemp)
return dateTemp
},
/**
* @param {Object} date
Expand Down
21 changes: 21 additions & 0 deletions test/unit/specs/Datepicker/Datepicker.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mount, shallowMount } from '@vue/test-utils'
import { addDays } from 'date-fns'
import DateInput from '~/components/DateInput.vue'
import Datepicker from '~/components/Datepicker.vue'

Expand Down Expand Up @@ -255,6 +256,26 @@ describe('Datepicker shallowMounted', () => {
})
expect(wrapper.emitted()['changed-month']).toBeTruthy()
})

it('should clear date on default date disabled', async () => {
const someDate = new Date('2021-01-15')
const wrapperTemp = shallowMount(Datepicker, {
propsData: {
value: someDate,
disabledDates: {
customPredictor(customPredictorDate) {
if (customPredictorDate < addDays(someDate, 4)) {
return true
}
return false
},
},
},
})
await wrapperTemp.vm.$nextTick()
expect(wrapperTemp.vm.selectedDate).toEqual(null)
expect(wrapperTemp.emitted().input).toBeTruthy()
})
})

describe('Datepicker.vue set by string', () => {
Expand Down

0 comments on commit 01a947d

Please sign in to comment.