Skip to content

Commit 5576577

Browse files
committed
fix(withNextInputAutoFocus): fix crash if no focusable input is found
1 parent ea91538 commit 5576577

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/withNextInputAutoFocus.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ const getInputs = children =>
1414
if (child && child.props && child.props.children) {
1515
return partialInputs.concat(getInputs(child.props.children));
1616
}
17-
if (child && child.props && !!child.props.name) return partialInputs.concat(child);
17+
if (child && child.props && !!child.props.name)
18+
return partialInputs.concat(child);
1819
return partialInputs;
1920
}, []);
2021

21-
export const withNextInputAutoFocusForm = (WrappedComponent, { submitAfterLastInput } = { submitAfterLastInput: true }) => {
22+
export const withNextInputAutoFocusForm = (
23+
WrappedComponent,
24+
{ submitAfterLastInput } = { submitAfterLastInput: true }
25+
) => {
2226
class WithNextInputAutoFocusForm extends React.PureComponent {
2327
static childContextTypes = withNextInputAutoFocusContextType;
2428

@@ -32,24 +36,26 @@ export const withNextInputAutoFocusForm = (WrappedComponent, { submitAfterLastIn
3236
inputNameMap;
3337
inputRefs = {};
3438

35-
getInputPosition = name => this.inputs.findIndex(input => input.props.name === name);
39+
getInputPosition = name =>
40+
this.inputs.findIndex(input => input.props.name === name);
3641

3742
getChildContext = () => ({
3843
setInput: (name, component) => {
3944
this.inputRefs[name] = component;
4045
},
4146
handleSubmitEditing: name => {
4247
const inputPosition = this.getInputPosition(name);
43-
const isLastInput = inputPosition === this.inputs.length - 1;
44-
45-
if (isLastInput) {
46-
if (submitAfterLastInput) this.props.formik.submitForm();
47-
} else {
48-
const nextInputs = this.inputs.slice(inputPosition + 1);
49-
const nextFocusableInput = nextInputs.find(
50-
element => this.inputRefs[element.props.name] && this.inputRefs[element.props.name].focus
51-
);
48+
const nextInputs = this.inputs.slice(inputPosition + 1);
49+
const nextFocusableInput = nextInputs.find(
50+
element =>
51+
this.inputRefs[element.props.name] &&
52+
this.inputRefs[element.props.name].focus
53+
);
54+
55+
if (nextFocusableInput) {
5256
this.inputRefs[nextFocusableInput.props.name].focus();
57+
} else {
58+
if (submitAfterLastInput) this.props.formik.submitForm();
5359
}
5460
},
5561
getReturnKeyType: name => {
@@ -69,7 +75,10 @@ export const withNextInputAutoFocusForm = (WrappedComponent, { submitAfterLastIn
6975
};
7076

7177
export const withNextInputAutoFocusInput = Input => {
72-
class WithNextInputAutoFocusInput extends React.Component<$FlowFixMeProps, $FlowFixMeState> {
78+
class WithNextInputAutoFocusInput extends React.Component<
79+
$FlowFixMeProps,
80+
$FlowFixMeState
81+
> {
7382
static contextTypes = withNextInputAutoFocusContextType;
7483

7584
setInput = component => {

0 commit comments

Comments
 (0)