Skip to content

Commit

Permalink
fix(date-picker): allow external switching between date and month view
Browse files Browse the repository at this point in the history
switching between date and month view using type prop was not working

closes #2809
  • Loading branch information
nekosaur committed Dec 21, 2017
1 parent 2e9d6af commit d6935aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/components/VDatePicker/VDatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ export default {
this.activePicker = 'MONTH'
} else if (val === 'year') {
this.activePicker = 'YEAR'
} else if (val === 'date' && this.activePicker === 'MONTH') {
const date = this.sanitizeDateString(`${this.tableYear}-${this.month + 1}-01`, 'date')
if (this.isAllowed(date)) this.inputDate = date
this.activePicker = 'DATE'
}
}
},
Expand Down Expand Up @@ -338,7 +342,7 @@ export default {
// Adds leading zero to month/day if necessary, returns 'YYYY' if type = 'year',
// 'YYYY-MM' if 'month' and 'YYYY-MM-DD' if 'date'
sanitizeDateString (dateString, type) {
const [year, month, date] = dateString.split('-')
const [year, month = 1, date = 1] = dateString.split('-')
return `${year}-${pad(month)}-${pad(date)}`.substr(0, { date: 10, month: 7, year: 4 }[type])
},
// For month = 12 it sets the tableDate to January next year
Expand Down
10 changes: 8 additions & 2 deletions src/components/VDatePicker/VDatePicker.month.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,20 @@ test('VDatePicker.js', ({ mount, compileToFunctions }) => {
it('should update the active picker if type has changed', () => {
const wrapper = mount(VDatePicker, {
propsData: {
type: 'month'
value: '1999-12-01',
type: 'date'
}
})

expect(wrapper.vm.activePicker).toBe('DATE')
wrapper.setProps({ type: 'month' })
expect(wrapper.vm.inputDate).toBe('1999-12')
expect(wrapper.vm.activePicker).toBe('MONTH')
wrapper.setProps({ type: 'date' })
expect(wrapper.vm.activePicker).toBe('MONTH')
expect(wrapper.vm.inputDate).toBe('1999-12-01')
expect(wrapper.vm.activePicker).toBe('DATE')
wrapper.setProps({ type: 'year' })
expect(wrapper.vm.inputDate).toBe('1999')
expect(wrapper.vm.activePicker).toBe('YEAR')
})

Expand Down

0 comments on commit d6935aa

Please sign in to comment.