@@ -254,8 +254,10 @@ export class Timepicker extends Component<TimepickerOptions> {
254
254
this . plate . addEventListener ( 'mousedown' , this . _handleClockClickStart ) ;
255
255
this . plate . addEventListener ( 'touchstart' , this . _handleClockClickStart ) ;
256
256
this . digitalClock . addEventListener ( 'keyup' , this . _inputFromTextField ) ;
257
- this . inputHours . addEventListener ( 'click' , ( ) => this . showView ( 'hours' ) ) ;
258
- this . inputMinutes . addEventListener ( 'click' , ( ) => this . showView ( 'minutes' ) ) ;
257
+ this . inputHours . addEventListener ( 'focus' , ( ) => this . showView ( 'hours' ) ) ;
258
+ this . inputHours . addEventListener ( 'focusout' , ( ) => this . formatHours ( ) ) ;
259
+ this . inputMinutes . addEventListener ( 'focus' , ( ) => this . showView ( 'minutes' ) ) ;
260
+ this . inputMinutes . addEventListener ( 'focusout' , ( ) => this . formatMinutes ( ) ) ;
259
261
}
260
262
261
263
_removeEventHandlers ( ) {
@@ -359,9 +361,23 @@ export class Timepicker extends Component<TimepickerOptions> {
359
361
360
362
_setupModal ( ) {
361
363
this . modal = Modal . init ( this . modalEl , {
362
- onOpenStart : this . options . onOpenStart ,
364
+ onOpenStart : ( ) => {
365
+ if ( typeof this . options . onOpenStart === 'function' ) {
366
+ this . options . onOpenStart . call ( this ) ;
367
+ }
368
+ this . modalEl . querySelectorAll ( '.btn' ) . forEach ( ( e : HTMLButtonElement ) => {
369
+ if ( e . style . visibility !== 'hidden' ) e . tabIndex = 0 ;
370
+ } ) ;
371
+ } ,
363
372
onOpenEnd : this . options . onOpenEnd ,
364
- onCloseStart : this . options . onCloseStart ,
373
+ onCloseStart : ( ) => {
374
+ if ( typeof this . options . onCloseStart === 'function' ) {
375
+ this . options . onCloseStart . call ( this ) ;
376
+ }
377
+ this . modalEl . querySelectorAll ( '.btn' ) . forEach ( ( e : HTMLButtonElement ) => {
378
+ e . tabIndex = - 1 ;
379
+ } ) ;
380
+ } ,
365
381
onCloseEnd : ( ) => {
366
382
if ( typeof this . options . onCloseEnd === 'function' ) {
367
383
this . options . onCloseEnd . call ( this ) ;
@@ -392,10 +408,10 @@ export class Timepicker extends Component<TimepickerOptions> {
392
408
393
409
private _createButton ( text : string , visibility : string ) : HTMLButtonElement {
394
410
const button = document . createElement ( 'button' ) ;
395
- button . classList . add ( 'btn-flat' , 'waves-effect' ) ;
411
+ button . classList . add ( 'btn' , 'btn -flat', 'waves-effect' , 'text ') ;
396
412
button . style . visibility = visibility ;
397
413
button . type = 'button' ;
398
- button . tabIndex = this . options . twelveHour ? 3 : 1 ;
414
+ button . tabIndex = - 1 ;
399
415
button . innerText = text ;
400
416
return button ;
401
417
}
@@ -566,7 +582,7 @@ export class Timepicker extends Component<TimepickerOptions> {
566
582
}
567
583
this . hours = + value [ 0 ] || 0 ;
568
584
this . minutes = + value [ 1 ] || 0 ;
569
- this . inputHours . value = this . hours ;
585
+ this . inputHours . value = Timepicker . _addLeadingZero ( this . hours ) ;
570
586
this . inputMinutes . value = Timepicker . _addLeadingZero ( this . minutes ) ;
571
587
572
588
this . _updateAmPmView ( ) ;
@@ -635,31 +651,26 @@ export class Timepicker extends Component<TimepickerOptions> {
635
651
636
652
_inputFromTextField = ( ) => {
637
653
const isHours = this . currentView === 'hours' ;
638
- if ( isHours ) {
654
+ if ( isHours && this . inputHours . value !== '' ) {
639
655
const value = parseInt ( this . inputHours . value ) ;
640
- if ( value > 0 && value < 13 ) {
641
- this . drawClockFromTimeInput ( value , isHours ) ;
642
- this . showView ( 'minutes' , this . options . duration / 2 ) ;
656
+ if ( value > 0 && value < ( this . options . twelveHour ? 13 : 24 ) ) {
643
657
this . hours = value ;
644
- this . inputMinutes . focus ( ) ;
645
658
}
646
659
else {
647
- const hour = new Date ( ) . getHours ( ) ;
648
- this . inputHours . value = ( hour % 12 ) . toString ( ) ;
660
+ this . setHoursDefault ( ) ;
649
661
}
662
+ this . drawClockFromTimeInput ( this . hours , isHours ) ;
650
663
}
651
- else {
664
+ else if ( ! isHours && this . inputMinutes . value !== '' ) {
652
665
const value = parseInt ( this . inputMinutes . value ) ;
653
666
if ( value >= 0 && value < 60 ) {
654
- this . inputMinutes . value = Timepicker . _addLeadingZero ( value ) ;
655
- this . drawClockFromTimeInput ( value , isHours ) ;
656
667
this . minutes = value ;
657
- ( < HTMLElement > this . modalEl . querySelector ( '.confirmation-btns :nth-child(2)' ) ) . focus ( ) ;
658
668
}
659
669
else {
660
- const minutes = new Date ( ) . getMinutes ( ) ;
661
- this . inputMinutes . value = Timepicker . _addLeadingZero ( minutes ) ;
670
+ this . minutes = new Date ( ) . getMinutes ( ) ;
671
+ this . inputMinutes . value = this . minutes ;
662
672
}
673
+ this . drawClockFromTimeInput ( this . minutes , isHours ) ;
663
674
}
664
675
}
665
676
@@ -669,15 +680,10 @@ export class Timepicker extends Component<TimepickerOptions> {
669
680
let radius ;
670
681
if ( this . options . twelveHour ) {
671
682
radius = this . options . outerRadius ;
683
+ } else {
684
+ radius = isHours && value > 0 && value < 13 ? this . options . innerRadius : this . options . outerRadius ;
672
685
}
673
- let cx1 = Math . sin ( radian ) * ( radius - this . options . tickRadius ) ,
674
- cy1 = - Math . cos ( radian ) * ( radius - this . options . tickRadius ) ,
675
- cx2 = Math . sin ( radian ) * radius ,
676
- cy2 = - Math . cos ( radian ) * radius ;
677
- this . hand . setAttribute ( 'x2' , cx1 . toString ( ) ) ;
678
- this . hand . setAttribute ( 'y2' , cy1 . toString ( ) ) ;
679
- this . bg . setAttribute ( 'cx' , cx2 . toString ( ) ) ;
680
- this . bg . setAttribute ( 'cy' , cy2 . toString ( ) ) ;
686
+ this . setClockAttributes ( radian , radius ) ;
681
687
}
682
688
683
689
setHand ( x , y , roundBy5 : boolean = false ) {
@@ -742,13 +748,17 @@ export class Timepicker extends Component<TimepickerOptions> {
742
748
743
749
this [ this . currentView ] = value ;
744
750
if ( isHours ) {
745
- this . inputHours . value = value . toString ( ) ;
751
+ this . inputHours . value = Timepicker . _addLeadingZero ( value ) ;
746
752
}
747
753
else {
748
754
this . inputMinutes . value = Timepicker . _addLeadingZero ( value ) ;
749
755
}
750
756
751
757
// Set clock hand and others' position
758
+ this . setClockAttributes ( radian , radius ) ;
759
+ }
760
+
761
+ setClockAttributes ( radian : number , radius : number ) {
752
762
let cx1 = Math . sin ( radian ) * ( radius - this . options . tickRadius ) ,
753
763
cy1 = - Math . cos ( radian ) * ( radius - this . options . tickRadius ) ,
754
764
cx2 = Math . sin ( radian ) * radius ,
@@ -759,6 +769,21 @@ export class Timepicker extends Component<TimepickerOptions> {
759
769
this . bg . setAttribute ( 'cy' , cy2 . toString ( ) ) ;
760
770
}
761
771
772
+ formatHours ( ) {
773
+ if ( this . inputHours . value == '' ) this . setHoursDefault ( ) ;
774
+ this . inputHours . value = Timepicker . _addLeadingZero ( Number ( this . inputHours . value ) ) ;
775
+ }
776
+
777
+ formatMinutes ( ) {
778
+ if ( this . inputMinutes . value == '' ) this . minutes = new Date ( ) . getMinutes ( ) ;
779
+ this . inputMinutes . value = Timepicker . _addLeadingZero ( Number ( this . inputMinutes . value ) ) ;
780
+ }
781
+
782
+ setHoursDefault ( ) {
783
+ this . hours = new Date ( ) . getHours ( ) ;
784
+ this . inputHours . value = ( this . hours % ( this . options . twelveHour ? 12 : 24 ) ) . toString ( ) ;
785
+ }
786
+
762
787
/**
763
788
* Open timepicker.
764
789
*/
0 commit comments