Skip to content

Commit

Permalink
🧱 prevent runtime error startsWith called with undefined (#9223)
Browse files Browse the repository at this point in the history
* 🧱 prevent issue with startsWith called with undefined

* fix tests

* fix build
  • Loading branch information
bluebill1049 authored Oct 14, 2022
1 parent 8eeac44 commit 55335fc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
6 changes: 1 addition & 5 deletions examples/V7/asyncFieldValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ export default function App() {

<div>
<label htmlFor="lastName">Last Name</label>
<input
id="lastName"
placeholder="Luo"
{...register('lastName')}
/>
<input id="lastName" placeholder="Luo" {...register('lastName')} />
</div>

<div>
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/logic/focusFieldBy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('focusFieldBy', () => {
type: 'required',
},
},
key,
String(key),
),
);

Expand Down Expand Up @@ -58,7 +58,7 @@ describe('focusFieldBy', () => {
type: 'required',
},
},
key,
String(key),
),
);

Expand All @@ -79,7 +79,7 @@ describe('focusFieldBy', () => {
type: 'required',
},
},
key,
String(key),
),
);
}).not.toThrow();
Expand Down
8 changes: 6 additions & 2 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ export function createFormControl<
!validationResult &&
focusFieldBy(
_fields,
(key) => get(_formState.errors, key),
(key) => key && get(_formState.errors, key),
name ? fieldNames : _names.mount,
);

Expand Down Expand Up @@ -1016,7 +1016,11 @@ export function createFormControl<

const _focusError = () =>
_options.shouldFocusError &&
focusFieldBy(_fields, (key) => get(_formState.errors, key), _names.mount);
focusFieldBy(
_fields,
(key) => key && get(_formState.errors, key),
_names.mount,
);

const handleSubmit: UseFormHandleSubmit<TFieldValues> =
(onValid, onInvalid) => async (e) => {
Expand Down
2 changes: 1 addition & 1 deletion src/logic/focusFieldBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import isObject from '../utils/isObject';

const focusFieldBy = (
fields: FieldRefs,
callback: (name: string) => boolean,
callback: (name?: string) => boolean,
fieldsNames?: Set<InternalFieldName> | InternalFieldName[],
) => {
for (const key of fieldsNames || Object.keys(fields)) {
Expand Down
5 changes: 3 additions & 2 deletions src/useFieldArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,9 @@ export function useFieldArray<
});

control._names.focus &&
focusFieldBy(control._fields, (key: string) =>
key.startsWith(control._names.focus),
focusFieldBy(
control._fields,
(key) => !!key && key.startsWith(control._names.focus),
);

control._names.focus = '';
Expand Down

0 comments on commit 55335fc

Please sign in to comment.