@@ -286,7 +286,9 @@ export class JBDateInputWebComponent extends HTMLElement {
286286 this . elements . input . addEventListener ( 'keyup' , this . onInputKeyup . bind ( this ) , { passive :true } ) ;
287287 this . elements . input . addEventListener ( 'keydown' , this . onInputKeydown . bind ( this ) ) ;
288288 this . elements . input . addEventListener ( 'beforeinput' , this . onInputBeforeInput . bind ( this ) ) ;
289- this . elements . calendarTriggerButton . addEventListener ( 'click' , this . onCalendarButtonClicked . bind ( this ) ) ;
289+ this . elements . calendarTriggerButton . addEventListener ( 'focus' , this . onCalendarButtonFocused . bind ( this ) ) ;
290+ this . elements . calendarTriggerButton . addEventListener ( 'blur' , this . onCalendarButtonBlur . bind ( this ) ) ;
291+ this . elements . calendarTriggerButton . addEventListener ( 'click' , this . onCalendarButtonClick . bind ( this ) ) ;
290292 this . elements . calendar . addEventListener ( 'select' , ( e ) => this . onCalendarSelect ( e as CustomEvent ) ) ;
291293 this . elements . calendar . addEventListener ( 'init' , this . onCalendarElementinitiated . bind ( this ) ) ;
292294 this . elements . calendar . addEventListener ( 'blur' , this . onCalendarBlur . bind ( this ) , { passive :true } ) ;
@@ -775,15 +777,15 @@ export class JBDateInputWebComponent extends HTMLElement {
775777 }
776778 return false ;
777779 }
778- onInputFocus ( ) {
780+ onInputFocus ( e : FocusEvent ) {
779781 this . #lastInputStringValue = this . #sInputValue;
780782 this . focus ( ) ;
781783 document . addEventListener ( 'selectionchange' , this . handleCarretPosOnInputFocus . bind ( this ) ) ;
782784 }
783785 onInputBlur ( e : FocusEvent ) {
784786 document . removeEventListener ( 'selectionchange' , this . handleCarretPosOnInputFocus . bind ( this ) ) ;
785787 const focusedElement = e . relatedTarget ;
786- if ( focusedElement !== this . elements . calendar ) {
788+ if ( focusedElement !== this . elements . calendar && focusedElement !== this . elements . calendarTriggerButton ) {
787789 this . showCalendar = false ;
788790 }
789791 const inputText = this . #sInputValue;
@@ -796,7 +798,7 @@ export class JBDateInputWebComponent extends HTMLElement {
796798 }
797799 onCalendarBlur ( e : FocusEvent ) {
798800 const focusedElement = e . relatedTarget ;
799- if ( focusedElement !== this . elements . input ) {
801+ if ( focusedElement !== this . elements . input && focusedElement !== this . elements . calendarTriggerButton ) {
800802 this . showCalendar = false ;
801803 }
802804 }
@@ -954,8 +956,32 @@ export class JBDateInputWebComponent extends HTMLElement {
954956 } ;
955957 this . updateCalendarView ( ) ;
956958 }
957- onCalendarButtonClicked ( ) {
958- this . showCalendar = ! this . showCalendar ;
959+ #isCalendarButtonClickEventIsAfterFocusEvent = false ;
960+ onCalendarButtonFocused ( e :FocusEvent ) {
961+ const prevFocused = e . relatedTarget ;
962+ if ( this . showCalendar && prevFocused && [ this . elements . calendar as EventTarget , this . elements . input as EventTarget ] . includes ( prevFocused ) ) {
963+ //if calendar was displayed but user click on icon we hide it here
964+ ( prevFocused as HTMLInputElement ) . focus ( ) ;
965+ this . showCalendar = false ;
966+ } else {
967+ // if user focus on calendar button from outside of calendar area we show calendar
968+ this . #isCalendarButtonClickEventIsAfterFocusEvent = true ;
969+ this . showCalendar = true ;
970+ }
971+
972+ }
973+ onCalendarButtonBlur ( e :FocusEvent ) {
974+ if ( ! [ this . elements . calendar as EventTarget , this . elements . input as EventTarget ] . includes ( e . relatedTarget ! ) ) {
975+ this . showCalendar = false ;
976+ }
977+ }
978+ onCalendarButtonClick ( ) {
979+ const focusedElement = this . shadowRoot ?. activeElement ;
980+ if ( focusedElement && ! this . #isCalendarButtonClickEventIsAfterFocusEvent && focusedElement == this . elements . calendarTriggerButton ) {
981+ //check if this click is event exactly after focus or not if its after focus we just pass but if its not and its a second click we close menu or reopen menu if closed before
982+ this . showCalendar = ! this . showCalendar ;
983+ }
984+ this . #isCalendarButtonClickEventIsAfterFocusEvent = false ;
959985 }
960986 onCalendarSelect ( e : CustomEvent ) {
961987 const target = e . target as JBCalendarWebComponent ;
@@ -993,7 +1019,6 @@ export class JBDateInputWebComponent extends HTMLElement {
9931019 }
9941020 #fixCalendarContainerPos = ( ) => {
9951021 const bcr = this . elements . calendarContainer . getBoundingClientRect ( ) ;
996- console . log ( bcr ) ;
9971022 const overflowSize = document . body . clientHeight - bcr . bottom ;
9981023 if ( overflowSize < 0 ) {
9991024 this . elements . calendarContainer . style . transform = `translateY(${ overflowSize } px)` ;
0 commit comments