Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isValidating to field state #10657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d313059
Add isValidating to field state
pbankonier Jul 10, 2023
cf0a8e0
Fix type tests for isValidating field state
pbankonier Jul 10, 2023
0e3c731
Adjust bundlewatch maxSize
pbankonier Jul 10, 2023
21ba2c4
Merge branch 'master' into feature/fieldState_isValidating
pbankonier Jul 11, 2023
915f342
Merge branch 'master' into feature/fieldState_isValidating
pbankonier Aug 1, 2023
f546c9a
Fix api-extractor.md merge fail
pbankonier Aug 1, 2023
aacdb55
Merge branch 'master' into feature/fieldState_isValidating
pbankonier Aug 23, 2023
696dced
Merge branch 'master' into feature/fieldState_isValidating
pbankonier Sep 27, 2023
d585569
Adjust formState validatingFields property
pbankonier Sep 27, 2023
63d2d95
Merge branch 'master' into feature/fieldState_isValidating
pbankonier Feb 7, 2024
d7be98b
Adjust bundlewatch maxSize
pbankonier Feb 7, 2024
22b72c9
Merge branch 'master' into feature/fieldState_isValidating
bluebill1049 Feb 16, 2024
aa9b1ad
Refactoring for isValidating fieldState
pbankonier Feb 22, 2024
23491ff
Fix multi async validators behavior
pbankonier Feb 22, 2024
fd22d19
Remove unnecessary explicit type definition in isValidating test
pbankonier Feb 22, 2024
d4432dc
Merge branch 'master' into feature/fieldState_isValidating
pbankonier Feb 22, 2024
e320eba
Improve _updateIsValidating method
pbankonier Feb 22, 2024
c901dca
Add proxyFormState check to updateIsValidating method
pbankonier Feb 22, 2024
717a247
Merge branch 'master' into feature/fieldState_isValidating
bluebill1049 Feb 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -118,7 +118,7 @@
"files": [
{
"path": "./dist/index.cjs.js",
"maxSize": "10 kB"
"maxSize": "10.1 kB"
}
]
},
Expand Down
7 changes: 6 additions & 1 deletion reports/api-extractor.md
Expand Up @@ -91,6 +91,7 @@ export type ControllerFieldState = {
invalid: boolean;
isTouched: boolean;
isDirty: boolean;
isValidating: boolean;
error?: FieldError;
};

Expand Down Expand Up @@ -307,6 +308,7 @@ export type FormState<TFieldValues extends FieldValues> = {
defaultValues?: undefined | Readonly<DeepPartial<TFieldValues>>;
dirtyFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
touchedFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
validatingFields: Partial<Readonly<FieldNamesMarkedBoolean<TFieldValues>>>;
errors: FieldErrors<TFieldValues>;
};

Expand All @@ -316,6 +318,7 @@ export type FormStateProxy<TFieldValues extends FieldValues = FieldValues> = {
isValidating: boolean;
dirtyFields: FieldNamesMarkedBoolean<TFieldValues>;
touchedFields: FieldNamesMarkedBoolean<TFieldValues>;
validatingFields: FieldNamesMarkedBoolean<TFieldValues>;
errors: boolean;
isValid: boolean;
};
Expand Down Expand Up @@ -384,6 +387,7 @@ export type KeepStateOptions = Partial<{
keepIsSubmitted: boolean;
keepIsSubmitSuccessful: boolean;
keepTouched: boolean;
keepIsValidating: boolean;
keepIsValid: boolean;
keepSubmitCount: boolean;
}>;
Expand Down Expand Up @@ -660,6 +664,7 @@ export type UseFormGetFieldState<TFieldValues extends FieldValues> = <TFieldName
invalid: boolean;
isDirty: boolean;
isTouched: boolean;
isValidating: boolean;
error?: FieldError;
};

Expand Down Expand Up @@ -866,7 +871,7 @@ export type WatchObserver<TFieldValues extends FieldValues> = (value: DeepPartia

// Warnings were encountered during analysis:
//
// src/types/form.ts:440:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts
// src/types/form.ts:444:3 - (ae-forgotten-export) The symbol "Subscription" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
58 changes: 57 additions & 1 deletion src/__tests__/controller.test.tsx
@@ -1,5 +1,6 @@
import React from 'react';
import {
act as actComponent,
fireEvent,
render,
screen,
Expand All @@ -8,7 +9,7 @@ import {
} from '@testing-library/react';

import { Controller } from '../controller';
import { ControllerRenderProps, FieldValues } from '../types';
import { ControllerRenderProps, FieldValues, ValidateResult } from '../types';
import { useFieldArray } from '../useFieldArray';
import { useForm } from '../useForm';
import { FormProvider } from '../useFormContext';
Expand Down Expand Up @@ -261,6 +262,61 @@ describe('Controller', () => {
expect(touched).toEqual({ test: true });
});

it('should set field to formState validatingFields and render field isValidating state', async () => {
jest.useFakeTimers();

const getValidateMock: (timeout: number) => Promise<ValidateResult> = (
timeout: number,
) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, timeout);
});
};

let validatingFields: any;
const Component = () => {
const { control, formState } = useForm({ mode: 'onBlur' });

validatingFields = formState.validatingFields;

return (
<Controller
defaultValue=""
name="test"
render={({ field, fieldState }) => (
<>
<div>isValidating: {String(fieldState.isValidating)}</div>
<input {...field} />
</>
)}
control={control}
rules={{
validate: () => getValidateMock(1000),
}}
/>
);
};

render(<Component />);

expect(validatingFields).toEqual({});
expect(screen.getByText('isValidating: false')).toBeVisible();

fireEvent.blur(screen.getByRole('textbox'));

expect(validatingFields).toEqual({ test: true });
expect(screen.getByText('isValidating: true')).toBeVisible();

await actComponent(async () => {
jest.advanceTimersByTime(1100);
});

expect(validatingFields).toEqual({ test: false });
expect(screen.getByText('isValidating: false')).toBeVisible();
});

it('should call trigger method when re-validate mode is onBlur with blur event', async () => {
const Component = () => {
const {
Expand Down