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

feature: add extracgin options from z.array(z.enum([]) and z.enum([])… #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 (
<select>
{values.map((e) => {
//...
})}
</select>
);
}

const FormSchema = z.object({
favoriteColors: z.array(z.enum(["red", "green", "blue"])),
});
```
21 changes: 21 additions & 0 deletions src/__tests__/getMetaInformationForZodType.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import {
getEnumValues,
getMetaInformationForZodType,
SPLIT_DESCRIPTION_SYMBOL,
} from "../getMetaInformationForZodType";
Expand Down Expand Up @@ -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();
})
});
3 changes: 2 additions & 1 deletion src/getMetaInformationForZodType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think type.element is preferred here

if (!(type._def.typeName === z.ZodFirstPartyTypeKind.ZodEnum)) return;
return type._def.values as readonly string[];
}
Expand Down
19 changes: 19 additions & 0 deletions www/docs/api/api-docs/hooks/useenumvalues.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<select multiple>
{values.map((e) => {
//...
})}
</select>
);
}

const FormSchema = z.object({
favoriteColors: z.array(z.enum(["red", "green", "blue"])),
});
```