From ef4f249526d61a7dbdc0297d4bc49f707a667eb5 Mon Sep 17 00:00:00 2001 From: Alex Johnson Date: Thu, 29 Aug 2024 05:19:45 -0500 Subject: [PATCH] disc. union --- src/Field.tsx | 4 ++-- src/types.ts | 46 +++++++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Field.tsx b/src/Field.tsx index 16edc9b..1ea488c 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -257,10 +257,10 @@ export const Field = < && !!component && checked != undefined; - const fieldValidationOptions: FieldValidationOptions = { + const fieldValidationOptions = { shouldValidate , validationEvent - }; + } as FieldValidationOptions; const fieldProps = { name diff --git a/src/types.ts b/src/types.ts index e10872d..6645139 100644 --- a/src/types.ts +++ b/src/types.ts @@ -77,26 +77,30 @@ export type SingleFieldValidator< , TFieldName extends DeepKeys = DeepKeys > = ( value: DeepValue ) => Promise | string | Nullish; -export type FieldValidationOptions = { - /** - * Whether to run validation after field value is updated or the field is blurred. - * Setting this prop to `false` will cancel any validation set for the field. - * Granular configuration of the field validation can be set with the `validationEvent` - * prop if set to `true` or not set. - * - * @default true - */ - shouldValidate?: TShouldValidate; - /** - * The field events for which validation should occur. *Only applies if - * `shouldValidate` is set to `true` or not set at all.* - * - * Not setting this prop or setting this prop to `'all'` will run validation on every field - * change or blur event. - * - * @default 'all' - */ - validationEvent?: NoInfer extends true ? never : 'onChange' | 'onBlur' | 'all'; +/** + * @param shouldValidate + * Whether to run validation after field value is updated or the field is blurred. + * Setting this prop to `false` will cancel any validation set for the field. + * Granular configuration of the field validation can be set with the `validationEvent` + * prop if set to `true` or not set. + * + * @default true + * ----------------------- + * @param validationEvent + * The field events for which validation should occur. *Only applies if + * `shouldValidate` is set to `true` or not set at all.* + * + * Not setting this prop or setting this prop to `'all'` will run validation on every field + * change or blur event. + * + * @default 'all' + */ +export type FieldValidationOptions = { + shouldValidate?: true; + validationEvent?: 'onChange' | 'onBlur' | 'all'; +} | { + shouldValidate: false; + validationEvent?: never; }; ////// STORE ////// @@ -169,7 +173,7 @@ export type FormHandlers = { setFieldValue: < TFieldName extends DeepKeys = DeepKeys , TFieldValue extends DeepValue = DeepValue - , TFieldValidationOptions extends FieldValidationOptions = FieldValidationOptions + , TFieldValidationOptions extends FieldValidationOptions = FieldValidationOptions >( fieldName: TFieldName , newValue: TFieldValue