@@ -32,6 +32,30 @@ const updateInputWidth = (element) => {
3232 container . removeChild ( span ) ;
3333} ;
3434
35+ const findPreviousInput = ( element ) => {
36+ const previousElement = element . previousElementSibling ; // Divider between inputs
37+ if ( ! previousElement ) {
38+ return null ;
39+ }
40+ return previousElement . previousElementSibling ; // Actual input
41+ } ;
42+
43+ const findNextInput = ( element ) => {
44+ const nextElement = element . nextElementSibling ; // Divider between inputs
45+ if ( ! nextElement ) {
46+ return null ;
47+ }
48+ return nextElement . nextElementSibling ; // Actual input
49+ } ;
50+
51+ const selectIfPossible = ( element ) => {
52+ if ( ! element ) {
53+ return ;
54+ }
55+ element . focus ( ) ;
56+ element . select ( ) ;
57+ } ;
58+
3559const removeUnwantedCharacters = str => str
3660 . split ( '' )
3761 // We don't want spaces in dates
@@ -269,43 +293,13 @@ export default class DateInput extends Component {
269293 }
270294
271295 onKeyDown = ( event ) => {
272- const findPreviousInput = ( element ) => {
273- const previousElement = element . previousElementSibling ; // Divider between inputs
274-
275- if ( ! previousElement ) {
276- return null ;
277- }
278-
279- const previousInput = previousElement . previousElementSibling ; // Actual input
280-
281- return previousInput ;
282- } ;
283-
284- const findNextInput = ( element ) => {
285- const nextElement = element . nextElementSibling ; // Divider between inputs
286-
287- if ( ! nextElement ) {
288- return null ;
289- }
290-
291- const nextInput = nextElement . nextElementSibling ; // Actual input
292-
293- return nextInput ;
294- } ;
295-
296296 switch ( event . key ) {
297297 case 'ArrowLeft' : {
298298 event . preventDefault ( ) ;
299299
300300 const input = event . target ;
301301 const previousInput = findPreviousInput ( input ) ;
302-
303- if ( ! previousInput ) {
304- return ;
305- }
306-
307- previousInput . focus ( ) ;
308- previousInput . select ( ) ;
302+ selectIfPossible ( previousInput ) ;
309303 break ;
310304 }
311305 case 'ArrowRight' :
@@ -314,13 +308,7 @@ export default class DateInput extends Component {
314308
315309 const input = event . target ;
316310 const nextInput = findNextInput ( input ) ;
317-
318- if ( ! nextInput ) {
319- return ;
320- }
321-
322- nextInput . focus ( ) ;
323- nextInput . select ( ) ;
311+ selectIfPossible ( nextInput ) ;
324312 break ;
325313 }
326314 default :
0 commit comments