diff --git a/API.md b/API.md
index 147ecce..dd1663f 100644
--- a/API.md
+++ b/API.md
@@ -1,9 +1,16 @@
# API
-- [createTsForm](#createtsform)
-- [createUniqueFieldSchema](#createuniquefieldschema)
-- [FormComponent](#formcomponent)
-- [Hooks](#hooks)
+- [API](#api)
+ - [createTsForm](#createtsform)
+ - [createTsForm params](#createtsform-params)
+ - [createUniqueFieldSchema](#createuniquefieldschema)
+ - [FormComponent](#formcomponent)
+ - [Props](#props)
+ - [Hooks](#hooks)
+ - [`useTsController`](#usetscontroller)
+ - [`useDescription`](#usedescription)
+ - [`useReqDescription`](#usereqdescription)
+ - [`useEnumValues` (**deprecated**, don't use)](#useenumvalues-deprecated-dont-use)
## createTsForm
@@ -205,3 +212,22 @@ const FormSchema = z.object({
favoriteColor: z.enum(["red", "green", "blue"]),
});
```
+
+It can be used to extract the enum values passed to `z.enum()` from array of enums:
+
+```tsx
+function MyDropdown() {
+ const values = useEnumValues(); // ['red', 'green', 'blue']
+ return (
+
+ );
+}
+
+const FormSchema = z.object({
+ favoriteColors: z.array(z.enum(["red", "green", "blue"])),
+});
+```
diff --git a/src/__tests__/getMetaInformationForZodType.test.ts b/src/__tests__/getMetaInformationForZodType.test.ts
index c667d97..7ff8b0d 100644
--- a/src/__tests__/getMetaInformationForZodType.test.ts
+++ b/src/__tests__/getMetaInformationForZodType.test.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import {
+ getEnumValues,
getMetaInformationForZodType,
SPLIT_DESCRIPTION_SYMBOL,
} from "../getMetaInformationForZodType";
@@ -58,4 +59,24 @@ describe("getMetaInformationForZodType", () => {
placeholder: undefined,
});
});
+
+ it("should get the enum values if the schema is an enum", () => {
+ const result = getEnumValues(z.enum(["a", "b", "c"]));
+ expect(result).toStrictEqual(["a", "b", "c"]);
+ })
+
+ it("should get the enum values if the schema is an array of enums", () => {
+ const result = getEnumValues(z.array(z.enum(["a", "b", "c"])));
+ expect(result).toStrictEqual(["a", "b", "c"]);
+ })
+
+ it("should get the enum values if the schema is an array of enums 2", () => {
+ const result = getEnumValues(z.enum(["a", "b", "c"]).array());
+ expect(result).toStrictEqual(["a", "b", "c"]);
+ })
+
+ it("should return undefined if the schema is not an enum", () => {
+ const result = getEnumValues(z.string());
+ expect(result).toBeUndefined();
+ })
});
diff --git a/src/getMetaInformationForZodType.ts b/src/getMetaInformationForZodType.ts
index 2ae5538..6b964a7 100644
--- a/src/getMetaInformationForZodType.ts
+++ b/src/getMetaInformationForZodType.ts
@@ -16,7 +16,8 @@ export function parseDescription(description?: string) {
};
}
-export function getEnumValues(type: RTFSupportedZodTypes) {
+export function getEnumValues(type: RTFSupportedZodTypes): readonly string[] | undefined {
+ if (type._def.typeName === z.ZodFirstPartyTypeKind.ZodArray) return getEnumValues(type._def.type)
if (!(type._def.typeName === z.ZodFirstPartyTypeKind.ZodEnum)) return;
return type._def.values as readonly string[];
}
diff --git a/www/docs/api/api-docs/hooks/useenumvalues.md b/www/docs/api/api-docs/hooks/useenumvalues.md
index d8f7e75..7c41e30 100644
--- a/www/docs/api/api-docs/hooks/useenumvalues.md
+++ b/www/docs/api/api-docs/hooks/useenumvalues.md
@@ -23,3 +23,22 @@ const FormSchema = z.object({
favoriteColor: z.enum(["red", "green", "blue"]),
});
```
+
+It can be used to extract the enum values passed to `z.enum()` from array of enums:
+
+```tsx
+function MyDropdown() {
+ const values = useEnumValues(); // ['red', 'green', 'blue']
+ return (
+
+ );
+}
+
+const FormSchema = z.object({
+ favoriteColors: z.array(z.enum(["red", "green", "blue"])),
+});
+```
\ No newline at end of file