Skip to content

Commit

Permalink
Merge branch 'master' into document-util-and-logic-fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrox committed May 19, 2024
2 parents dc2ec87 + 3e374dc commit 6d2b21d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions reports/api-extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ export type WatchInternal<TFieldValues> = (fieldNames?: InternalFieldName | Inte
export type WatchObserver<TFieldValues extends FieldValues> = (value: DeepPartial<TFieldValues>, info: {
name?: FieldPath<TFieldValues>;
type?: EventType;
value?: unknown;
}) => void;

// Warnings were encountered during analysis:
Expand Down
87 changes: 87 additions & 0 deletions src/__tests__/useForm/setError.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,91 @@ describe('setError', () => {
screen.findByText('not found');
});
});

it('should allow sequential calls to set with child after ancestor', async () => {
const { result } = renderHook(() =>
useForm<{ input: { first: string; last: string } }>(),
);
result.current.formState.errors;

act(() => {
result.current.setError('input', {
type: 'test',
message: 'Some error that depends on both fields',
});
});

expect(result.current.formState.errors).toEqual({
input: {
type: 'test',
message: 'Some error that depends on both fields',
ref: undefined,
},
});

act(() => {
result.current.setError('input.first', {
type: 'test',
message: 'Name must be capitalized',
});
});

expect(result.current.formState.errors).toEqual({
input: {
type: 'test',
message: 'Some error that depends on both fields',
ref: undefined,
first: {
type: 'test',
message: 'Name must be capitalized',
ref: undefined,
},
},
});
});

it('should allow sequential calls to set with ancestor after child', async () => {
const { result } = renderHook(() =>
useForm<{ input: { first: string; last: string } }>(),
);

result.current.formState.errors;

act(() => {
result.current.setError('input.first', {
type: 'test',
message: 'Name must be capitalized',
});
});

expect(result.current.formState.errors).toEqual({
input: {
first: {
type: 'test',
message: 'Name must be capitalized',
ref: undefined,
},
},
});

act(() => {
result.current.setError('input', {
type: 'test',
message: 'Some error that depends on both fields',
});
});

expect(result.current.formState.errors).toEqual({
input: {
type: 'test',
message: 'Some error that depends on both fields',
ref: undefined,
first: {
type: 'test',
message: 'Name must be capitalized',
ref: undefined,
},
},
});
});
});
5 changes: 5 additions & 0 deletions src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,13 @@ export function createFormControl<

const setError: UseFormSetError<TFieldValues> = (name, error, options) => {
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
const currentError = get(_formState.errors, name) || {};

// Don't override existing error messages elsewhere in the object tree.
const { ref: currentRef, message, type, ...restOfErrorTree } = currentError;

set(_formState.errors, name, {
...restOfErrorTree,
...error,
ref,
});
Expand Down
1 change: 1 addition & 0 deletions src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ export type WatchObserver<TFieldValues extends FieldValues> = (
info: {
name?: FieldPath<TFieldValues>;
type?: EventType;
value?: unknown;
},
) => void;

Expand Down

0 comments on commit 6d2b21d

Please sign in to comment.