Skip to content

Commit

Permalink
馃悶 update isDirty when setting disabled in register (#10805)
Browse files Browse the repository at this point in the history
* add failing test

* updateTouchAndDirty in register

* fix vscode settings

---------

Co-authored-by: Selim Belhaouane <selim.belhaouane@garda.com>
  • Loading branch information
selimb and selimb committed Aug 18, 2023
1 parent 38ff4ef commit 75ae6e7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"cwd": "${workspaceFolder}",
"runtimeExecutable": "yarn",
"args": ["test", "--runInBand", "--watchAll=false"]
"runtimeExecutable": "pnpm",
"args": ["test", "--", "--runInBand", "--watchAll=false"]
}
]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"source.fixAll.eslint": true
},
"typescript.tsdk": "node_modules/typescript/lib",
"jest.jestCommandLine": "yarn test",
"jest.jestCommandLine": "pnpm test --",
"jest.autoRun": {
"watch": false,
"onSave": "test-file",
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/useForm/formState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,38 @@ describe('formState', () => {
expect(dirtyFieldsState).toEqual({});
});

it('should recompute isDirty after toggling disabled', async () => {
let isDirty: null | boolean = null;

const App = () => {
const defaultValues = { name: 'initial', disableName: false };
const { formState, register, watch } = useForm({ defaultValues });

isDirty = formState.isDirty;

const disableName = watch('disableName', defaultValues.disableName);

return (
<form>
<input type="text" {...register('name', { disabled: disableName })} />
<input type="checkbox" {...register('disableName')} />
</form>
);
};

render(<App />);

const checkbox = screen.getByRole('checkbox');

fireEvent.click(checkbox);

expect(isDirty).toBe(true);

fireEvent.click(checkbox);

expect(isDirty).toBe(false);
});

describe('when delay config is set', () => {
const message = 'required.';

Expand Down
21 changes: 11 additions & 10 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,16 +949,17 @@ export function createFormControl<
});
_names.mount.add(name);

field
? disabledIsDefined &&
set(
_formValues,
name,
options.disabled
? undefined
: get(_formValues, name, getFieldValue(field._f)),
)
: updateValidAndValue(name, true, options.value);
if (field) {
if (disabledIsDefined) {
const value = options.disabled
? undefined
: get(_formValues, name, getFieldValue(field._f));
set(_formValues, name, value);
updateTouchAndDirty(name, value, false, false, true);
}
} else {
updateValidAndValue(name, true, options.value);
}

return {
...(disabledIsDefined ? { disabled: options.disabled } : {}),
Expand Down

0 comments on commit 75ae6e7

Please sign in to comment.