22
33export function init ( id ) {
44 const el = document . getElementById ( id ) ;
5+ const skipKeys = [ 'Enter' , 'Tab' , 'Shift' , 'Control' , 'Alt' ] ;
6+ EventHandler . on ( el , 'input' , 'input' , e => {
7+ const isNumber = e . target . getAttribute ( 'type' ) === 'number' ;
8+ if ( isNumber ) {
9+ if ( e . target . value . length > 1 ) {
10+ e . target . value = e . target . value . slice ( 1 , 2 ) ;
11+ }
12+ }
13+ } ) ;
514 EventHandler . on ( el , 'keydown' , 'input' , e => {
615 const isNumber = e . target . getAttribute ( 'type' ) === 'number' ;
7- if ( e . key === 'Tab' ) {
16+ if ( skipKeys . indexOf ( e . key ) > - 1 ) {
817
918 }
1019 else if ( e . key === 'Backspace' || e . key === 'ArrowLeft' ) {
@@ -14,14 +23,14 @@ export function init(id) {
1423 setNextFocus ( el , e . target ) ;
1524 }
1625 else if ( isNumber ) {
17- if ( e . key >= '0' && e . key <= '9' ) {
26+ if ( "0123456789" . indexOf ( e . key ) > - 1 ) {
1827 setNextFocus ( el , e . target ) ;
1928 }
2029 else {
2130 e . preventDefault ( ) ;
2231 }
2332 }
24- else if ( ( e . key >= 'a' && e . key <= 'z' ) || ( e . key >= 'A' && e . key <= 'Z' ) ) {
33+ else if ( "abcdefghijklmnopqrstuvwxyzABCDEDFHIJKLMNOPQRSTUVWXYZ0123456789" . indexOf ( e . key ) > - 1 ) {
2534 setNextFocus ( el , e . target ) ;
2635 }
2736 else {
@@ -41,9 +50,6 @@ const setPrevFocus = (el, target) => {
4150 if ( index > 0 ) {
4251 setFocus ( inputs [ index - 1 ] ) ;
4352 }
44- else {
45- setBlur ( target ) ;
46- }
4753}
4854
4955const setNextFocus = ( el , target ) => {
@@ -52,18 +58,6 @@ const setNextFocus = (el, target) => {
5258 if ( index < inputs . length - 1 ) {
5359 setFocus ( inputs [ index + 1 ] ) ;
5460 }
55- else {
56- setBlur ( target ) ;
57- }
58- }
59-
60- const setBlur = target => {
61- const handler = setTimeout ( function ( ) {
62- clearTimeout ( handler ) ;
63- if ( target . blur ) {
64- target . blur ( ) ;
65- }
66- } , 0 ) ;
6761}
6862
6963const setFocus = target => {
@@ -77,5 +71,7 @@ const setFocus = target => {
7771
7872export function dispose ( id ) {
7973 const el = document . getElementById ( id ) ;
74+ EventHandler . off ( el , 'input' ) ;
8075 EventHandler . off ( el , 'keydown' ) ;
76+ EventHandler . off ( el , 'focus' ) ;
8177}
0 commit comments