@@ -3,6 +3,7 @@ import Data from '../mdb/dom/data';
33import EventHandler from '../bootstrap/src/dom/event-handler' ;
44import Manipulator from '../mdb/dom/manipulator' ;
55import SelectorEngine from '../mdb/dom/selector-engine' ;
6+ import 'detect-autofill' ;
67
78/**
89 * ------------------------------------------------------------------------
@@ -47,10 +48,26 @@ class Input {
4748 this . _getLabelData ( ) ;
4849 this . _applyDivs ( ) ;
4950 this . _applyNotch ( ) ;
50- this . _applyActiveClass ( ) ;
51+ this . _activate ( ) ;
52+ }
53+
54+ update ( ) {
55+ this . _getLabelData ( ) ;
56+ this . _getNotchData ( ) ;
57+ this . _applyNotch ( ) ;
58+ this . _activate ( ) ;
59+ }
60+
61+ forceActive ( ) {
62+ const input =
63+ SelectorEngine . findOne ( 'input' , this . _element ) ||
64+ SelectorEngine . findOne ( 'textarea' , this . _element ) ;
65+ Manipulator . addClass ( input , 'active' ) ;
5166 }
5267
5368 dispose ( ) {
69+ this . _removeBorder ( ) ;
70+
5471 Data . removeData ( this . _element , DATA_KEY ) ;
5572 this . _element = null ;
5673 }
@@ -65,6 +82,11 @@ class Input {
6582 this . _getLabelPositionInInputGroup ( ) ;
6683 }
6784
85+ _getNotchData ( ) {
86+ this . _notchMiddle = SelectorEngine . findOne ( '.form-notch-middle' , this . _element ) ;
87+ this . _notchLeading = SelectorEngine . findOne ( '.form-notch-leading' , this . _element ) ;
88+ }
89+
6890 _getLabelWidth ( ) {
6991 this . _labelWidth = this . _label . clientWidth * 0.8 + 8 ;
7092 }
@@ -102,7 +124,24 @@ class Input {
102124 this . _label . style . marginLeft = `${ this . _labelMarginLeft } px` ;
103125 }
104126
105- _applyActiveClass ( event ) {
127+ _removeBorder ( ) {
128+ const border = SelectorEngine . findOne ( '.form-notch' , this . _element ) ;
129+ if ( border ) border . remove ( ) ;
130+ }
131+
132+ _activate ( event ) {
133+ if ( event ) this . _label = SelectorEngine . findOne ( 'label' , event . target . parentNode ) ;
134+ if ( event && this . _label ) {
135+ const prevLabelWidth = this . _labelWidth ;
136+ this . _getLabelWidth ( ) ;
137+
138+ if ( prevLabelWidth !== this . _labelWidth ) {
139+ this . _notchMiddle = SelectorEngine . findOne ( '.form-notch-middle' , event . target . parentNode ) ;
140+ this . _notchLeading = SelectorEngine . findOne ( '.form-notch-leading' , event . target . parentNode ) ;
141+ this . _applyNotch ( ) ;
142+ }
143+ }
144+
106145 const input = event
107146 ? event . target
108147 : SelectorEngine . findOne ( 'input' , this . _element ) ||
@@ -112,7 +151,7 @@ class Input {
112151 }
113152 }
114153
115- _removeActiveClass ( event ) {
154+ _deactivate ( event ) {
116155 const input = event
117156 ? event . target
118157 : SelectorEngine . findOne ( 'input' , this . _element ) ||
@@ -122,15 +161,15 @@ class Input {
122161 }
123162 }
124163
125- static applyActiveClass ( instance ) {
164+ static activate ( instance ) {
126165 return function ( event ) {
127- instance . _applyActiveClass ( event ) ;
166+ instance . _activate ( event ) ;
128167 } ;
129168 }
130169
131- static removeActiveClass ( instance ) {
170+ static deactivate ( instance ) {
132171 return function ( event ) {
133- instance . _removeActiveClass ( event ) ;
172+ instance . _deactivate ( event ) ;
134173 } ;
135174 }
136175
@@ -139,17 +178,27 @@ class Input {
139178 }
140179}
141180
142- EventHandler . on ( document , 'focus' , OUTLINE_INPUT , Input . applyActiveClass ( new Input ( ) ) ) ;
143- EventHandler . on ( document , 'input' , OUTLINE_INPUT , Input . applyActiveClass ( new Input ( ) ) ) ;
144- EventHandler . on ( document , 'blur' , OUTLINE_INPUT , Input . removeActiveClass ( new Input ( ) ) ) ;
181+ EventHandler . on ( document , 'focus' , OUTLINE_INPUT , Input . activate ( new Input ( ) ) ) ;
182+ EventHandler . on ( document , 'input' , OUTLINE_INPUT , Input . activate ( new Input ( ) ) ) ;
183+ EventHandler . on ( document , 'blur' , OUTLINE_INPUT , Input . deactivate ( new Input ( ) ) ) ;
184+
185+ EventHandler . on ( document , 'focus' , OUTLINE_TEXTAREA , Input . activate ( new Input ( ) ) ) ;
186+ EventHandler . on ( document , 'input' , OUTLINE_TEXTAREA , Input . activate ( new Input ( ) ) ) ;
187+ EventHandler . on ( document , 'blur' , OUTLINE_TEXTAREA , Input . deactivate ( new Input ( ) ) ) ;
145188
146- EventHandler . on ( document , 'focus' , OUTLINE_TEXTAREA , Input . applyActiveClass ( new Input ( ) ) ) ;
147- EventHandler . on ( document , 'input' , OUTLINE_TEXTAREA , Input . applyActiveClass ( new Input ( ) ) ) ;
148- EventHandler . on ( document , 'blur' , OUTLINE_TEXTAREA , Input . removeActiveClass ( new Input ( ) ) ) ;
189+ EventHandler . on ( window , 'shown.bs.modal' , Input . activate ( new Input ( ) ) ) ;
190+ EventHandler . on ( window , 'shown.bs.dropdown' , Input . activate ( new Input ( ) ) ) ;
149191
150192// auto-init
151193SelectorEngine . find ( `.${ CLASSNAME_WRAPPER } ` ) . forEach ( ( element ) => {
152194 new Input ( element ) . init ( ) ;
153195} ) ;
154196
197+ // auto-fill
198+ EventHandler . on ( window , 'onautocomplete' , ( e ) => {
199+ const instance = Input . getInstance ( e . target . parentNode ) ;
200+ if ( ! instance ) return ;
201+ instance . forceActive ( ) ;
202+ } ) ;
203+
155204export default Input ;
0 commit comments