Skip to content

Commit fe7b189

Browse files
committed
feat(datepicker): Don't select date on typed-date event
1 parent 3478953 commit fe7b189

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

src/components/DateInput.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ export default {
119119
return this.inputClass
120120
},
121121
formattedValue() {
122-
if (!this.selectedDate) {
123-
return null
124-
}
125-
if (this.typedDate.length) {
122+
if (this.typeable) {
126123
return this.typedDate
127124
}
128125

src/components/Datepicker.vue

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,29 @@ export default {
493493
}
494494
},
495495
/**
496-
* Set the date from a 'typed-date' event
497-
* @param {Date} date
496+
* Updates the page (if necessary) after a 'typed-date' event and sets `tabbableCell` & `latestValidTypedDate`
497+
* @param {Date=} date
498498
*/
499499
handleTypedDate(date) {
500-
if (this.selectedDate) {
501-
this.setTransitionAndFocusDelay(this.selectedDate, date)
500+
if (!date) {
501+
return
502502
}
503503
504-
this.selectDate(date ? date.valueOf() : null)
505-
this.reviewFocus()
504+
const originalPageDateValue = new Date(this.pageDate)
505+
506+
this.setTransitionAndFocusDelay(this.latestValidTypedDate, date)
507+
this.latestValidTypedDate = date
508+
this.setPageDate(date)
509+
510+
if (this.isPageChange(originalPageDateValue)) {
511+
this.handlePageChange({
512+
focusRefs: [],
513+
pageDate: this.pageDate,
514+
})
515+
return
516+
}
517+
518+
this.setTabbableCell()
506519
},
507520
/**
508521
* Focus the relevant element when the view changes
@@ -533,6 +546,18 @@ export default {
533546
hasClass(element, className) {
534547
return element && element.className.split(' ').includes(className)
535548
},
549+
/**
550+
* Used for typeable datepicker: returns true if a typed date causes the page to change
551+
* @param {Date} originalPageDate
552+
* @returns {Boolean}
553+
*/
554+
isPageChange(originalPageDate) {
555+
if (!this.isOpen) {
556+
return false
557+
}
558+
559+
return originalPageDate.valueOf() !== this.pageDate.valueOf()
560+
},
536561
/**
537562
* Initiate the component
538563
*/

test/unit/specs/DateInput/typedDates.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,30 @@ describe('Datepicker mount', () => {
167167
expect(wrapper.vm.selectedDate).toEqual(today)
168168
})
169169

170+
it('emits `selected` when a valid date is typed and the `enter` key is pressed', async () => {
171+
const input = wrapper.find('input')
172+
173+
input.setValue('Jan')
174+
await input.trigger('keyup')
175+
176+
expect(wrapper.emitted('selected')).toBeUndefined()
177+
178+
await input.trigger('keydown.enter')
179+
180+
expect(wrapper.emitted('selected')).toBeDefined()
181+
expect(wrapper.emitted('selected')[0][0]).toBeInstanceOf(Date)
182+
})
183+
184+
it('does not emit `selected` when an invalid date is typed and the `enter` key is pressed', async () => {
185+
const input = wrapper.find('input')
186+
187+
input.setValue('invalid date')
188+
await input.trigger('keyup')
189+
await input.trigger('keydown.enter')
190+
191+
expect(wrapper.emitted('selected')).toBeUndefined()
192+
})
193+
170194
it('shows the correct month as you type', async () => {
171195
const input = wrapper.find('input')
172196

0 commit comments

Comments
 (0)