Skip to content

Commit

Permalink
🐞 fix #11842 radio buttons not disabled when multiple share a name (#…
Browse files Browse the repository at this point in the history
…11873)

* feat: allow `iterateFieldsByAction` to iterate over multiple refs

* fix: `_disableForm` only disables one input when inputs share a name

* test: add unit tests for action iteration over multiple refs

* refactor: invert `abortEarly` param's logic to respect semantic meaning

* refactor: move ref iteration logic to if block

* chore: fix lint errors

* test: rename tests to reflect changes

* Update createFormControl.ts

* Update iterateFieldsByAction.ts

* Update iterateFieldsByAction.test.ts

---------

Co-authored-by: Beier (Bill) <[email protected]>
  • Loading branch information
cj-young and bluebill1049 authored May 18, 2024
1 parent 4549afd commit 52611a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/logic/iterateFieldsByAction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import iterateFieldsByAction from '../../logic/iterateFieldsByAction';

describe('focusFieldBy', () => {
describe('iterateFieldsByAction', () => {
it('should focus on the first error it encounter', () => {
const focus = jest.fn();
iterateFieldsByAction(
Expand Down
15 changes: 9 additions & 6 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1098,13 +1098,16 @@ export function createFormControl<
iterateFieldsByAction(
_fields,
(ref, name) => {
let requiredDisabledState = disabled;
const currentField = get(_fields, name);
if (currentField && isBoolean(currentField._f.disabled)) {
requiredDisabledState ||= currentField._f.disabled;
const currentField: Field = get(_fields, name);
if (currentField) {
ref.disabled = currentField._f.disabled || disabled;

if (Array.isArray(currentField._f.refs)) {
currentField._f.refs.forEach((inputRef) => {
inputRef.disabled = currentField._f.disabled || disabled;
});
}
}

ref.disabled = requiredDisabledState;
},
0,
false,
Expand Down

0 comments on commit 52611a5

Please sign in to comment.