Skip to content

Commit

Permalink
馃馃徎 ensure field is focused when selected (#8566)
Browse files Browse the repository at this point in the history
As noted by MDN, calling `element.select()` doesn鈥檛 necessarily focus the
element. As such, they recommend calling `element.focus()` too.

See https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select#notes
  • Loading branch information
elstgav committed Jun 24, 2022
1 parent d3fa1b1 commit 6ec36a8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/__tests__/useController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ describe('useController', () => {
});

React.useEffect(() => {
setFocus('test');
setFocus('test', { shouldSelect: true });
}, [setFocus]);

Expand Down
3 changes: 2 additions & 1 deletion src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,8 @@ export function createFormControl<
const setFocus: UseFormSetFocus<TFieldValues> = (name, options = {}) => {
const field = get(_fields, name)._f;
const fieldRef = field.refs ? field.refs[0] : field.ref;
options.shouldSelect ? fieldRef.select() : fieldRef.focus();
fieldRef.focus();
options.shouldSelect && fieldRef.select();
};

return {
Expand Down

0 comments on commit 6ec36a8

Please sign in to comment.