@@ -10,7 +10,14 @@ const withNextInputAutoFocusContextType = {
1010 getReturnKeyType : PropTypes . func
1111} ;
1212
13- const isInput = component => component && ! ! component . props . name ;
13+ getInputs = children =>
14+ ( isArray ( children ) ? children : [ children ] ) . reduce ( ( partialInputs , child ) => {
15+ if ( child . props . children ) {
16+ return partialInputs . concat ( getInputs ( child . props . children ) ) ;
17+ }
18+ if ( child && ! ! child . props . name ) return partialInputs . concat ( child ) ;
19+ return partialInputs ;
20+ } , [ ] ) ;
1421
1522export const withNextInputAutoFocusForm = WrappedComponent => {
1623 class WithNextInputAutoFocusForm extends React . PureComponent {
@@ -20,14 +27,15 @@ export const withNextInputAutoFocusForm = WrappedComponent => {
2027 constructor ( props ) {
2128 super ( props ) ;
2229 const { children } = props ;
23- this . inputs = ( isArray ( children ) ? children : [ children ] ) . filter ( isInput ) ;
30+ this . inputs = getInputs ( children ) ;
2431 }
2532
2633 inputs ;
2734 inputNameMap ;
2835 inputRefs = { } ;
2936
30- getInputPosition = name => this . inputs . findIndex ( input => input . props . name === name ) ;
37+ getInputPosition = name =>
38+ this . inputs . findIndex ( input => input . props . name === name ) ;
3139
3240 getChildContext = ( ) => ( {
3341 setInput : ( name , component ) => {
@@ -40,8 +48,13 @@ export const withNextInputAutoFocusForm = WrappedComponent => {
4048 if ( isLastInput ) {
4149 this . context . formik . submitForm ( ) ;
4250 } else {
43- const nextInput = this . inputs [ inputPosition + 1 ] ;
44- this . inputRefs [ nextInput . props . name ] . focus ( ) ;
51+ const nextInputs = this . inputs . slice ( inputPosition + 1 ) ;
52+ const nextFocusableInput = nextInputs . find (
53+ element =>
54+ this . inputRefs [ element . props . name ] &&
55+ this . inputRefs [ element . props . name ] . focus
56+ ) ;
57+ this . inputRefs [ nextFocusableInput . props . name ] . focus ( ) ;
4558 }
4659 } ,
4760 getReturnKeyType : name => {
@@ -61,7 +74,10 @@ export const withNextInputAutoFocusForm = WrappedComponent => {
6174} ;
6275
6376export const withNextInputAutoFocusInput = Input => {
64- class WithNextInputAutoFocusInput extends React . Component < $FlowFixMeProps , $FlowFixMeState > {
77+ class WithNextInputAutoFocusInput extends React . Component <
78+ $FlowFixMeProps ,
79+ $FlowFixMeState
80+ > {
6581 static contextTypes = withNextInputAutoFocusContextType ;
6682
6783 setInput = component => {
0 commit comments