File tree Expand file tree Collapse file tree 3 files changed +56
-10
lines changed
test/unit/specs/DateInput Expand file tree Collapse file tree 3 files changed +56
-10
lines changed Original file line number Diff line number Diff line change @@ -119,10 +119,7 @@ export default {
119
119
return this .inputClass
120
120
},
121
121
formattedValue () {
122
- if (! this .selectedDate ) {
123
- return null
124
- }
125
- if (this .typedDate .length ) {
122
+ if (this .typeable ) {
126
123
return this .typedDate
127
124
}
128
125
Original file line number Diff line number Diff line change @@ -493,16 +493,29 @@ export default {
493
493
}
494
494
},
495
495
/**
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
498
498
*/
499
499
handleTypedDate (date ) {
500
- if (this . selectedDate ) {
501
- this . setTransitionAndFocusDelay ( this . selectedDate , date)
500
+ if (! date ) {
501
+ return
502
502
}
503
503
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 ()
506
519
},
507
520
/**
508
521
* Focus the relevant element when the view changes
@@ -533,6 +546,18 @@ export default {
533
546
hasClass (element , className ) {
534
547
return element && element .className .split (' ' ).includes (className)
535
548
},
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
+ },
536
561
/**
537
562
* Initiate the component
538
563
*/
Original file line number Diff line number Diff line change @@ -167,6 +167,30 @@ describe('Datepicker mount', () => {
167
167
expect ( wrapper . vm . selectedDate ) . toEqual ( today )
168
168
} )
169
169
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
+
170
194
it ( 'shows the correct month as you type' , async ( ) => {
171
195
const input = wrapper . find ( 'input' )
172
196
You can’t perform that action at this time.
0 commit comments