diff --git a/src/FieldContext.tsx b/src/FieldContext.tsx index 1453bcb..e5e2537 100644 --- a/src/FieldContext.tsx +++ b/src/FieldContext.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useState } from "react"; +import React, { useContext, useEffect, useState } from "react"; import { createContext, ReactNode } from "react"; import { Control, @@ -6,7 +6,6 @@ import { useController, UseControllerReturn, } from "react-hook-form"; -import { printUseEnumWarning } from "./logging"; import { errorFromRhfErrorObject } from "./zodObjectErrors"; import { RTFSupportedZodTypes } from "./supportedZodTypes"; import { UnwrapZodType, unwrap } from "./unwrap"; @@ -118,7 +117,7 @@ export function useTsController() { }; const { fieldState, - field: { onChange }, + field: { onChange, value }, } = controller; const [isUndefined, setIsUndefined] = useState(false); @@ -132,6 +131,14 @@ export function useTsController() { onChange(value); } } + + useEffect(() => { + if (value && isUndefined) { + setIsUndefined(false); + context.removeFromCoerceUndefined(context.name); + } + }, [value]); + return { ...controller, error: errorFromRhfErrorObject(fieldState.error), @@ -251,7 +258,6 @@ export function fieldSchemaMismatchHookError( */ export function useEnumValues() { const { enumValues } = useContextProt("useEnumValues"); - printUseEnumWarning(); if (!enumValues) throw new Error(enumValuesNotPassedError()); return enumValues; } diff --git a/src/logging.ts b/src/logging.ts index 51d8cfd..570d731 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -21,12 +21,3 @@ export function duplicateTypeError() { export function printWarningsForSchema(_type: RTFSupportedZodTypes) { // placeholder in case we need future schema warnings } - -export function printUseEnumWarning() { - if (!shownWarnings.useEnum) { - err( - "useEnumValues is deprecated and will be removed in future versions. See https://github.com/iway1/react-ts-form/blob/main/field-examples.md for examples of how to implement selects/dropdowns etc without enums." - ); - shownWarnings.useEnum = true; - } -}