Skip to content

Commit

Permalink
Merge pull request iway1#104 from iway1/remove-enum-warning
Browse files Browse the repository at this point in the history
removes enum warning
  • Loading branch information
iway1 committed Apr 13, 2023
2 parents ea98dd5 + caed296 commit 863599c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
14 changes: 10 additions & 4 deletions src/FieldContext.tsx
@@ -1,12 +1,11 @@
import React, { useContext, useState } from "react";
import React, { useContext, useEffect, useState } from "react";
import { createContext, ReactNode } from "react";
import {
Control,
DeepPartial,
useController,
UseControllerReturn,
} from "react-hook-form";
import { printUseEnumWarning } from "./logging";
import { errorFromRhfErrorObject } from "./zodObjectErrors";
import { RTFSupportedZodTypes } from "./supportedZodTypes";
import { UnwrapZodType, unwrap } from "./unwrap";
Expand Down Expand Up @@ -118,7 +117,7 @@ export function useTsController<FieldType extends any>() {
};
const {
fieldState,
field: { onChange },
field: { onChange, value },
} = controller;
const [isUndefined, setIsUndefined] = useState(false);

Expand All @@ -132,6 +131,14 @@ export function useTsController<FieldType extends any>() {
onChange(value);
}
}

useEffect(() => {
if (value && isUndefined) {
setIsUndefined(false);
context.removeFromCoerceUndefined(context.name);
}
}, [value]);

return {
...controller,
error: errorFromRhfErrorObject<FieldType>(fieldState.error),
Expand Down Expand Up @@ -251,7 +258,6 @@ export function fieldSchemaMismatchHookError(
*/
export function useEnumValues() {
const { enumValues } = useContextProt("useEnumValues");
printUseEnumWarning();
if (!enumValues) throw new Error(enumValuesNotPassedError());
return enumValues;
}
Expand Down
9 changes: 0 additions & 9 deletions src/logging.ts
Expand Up @@ -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;
}
}

0 comments on commit 863599c

Please sign in to comment.