Skip to content

Commit

Permalink
feature: add extracgin options from z.array(z.enum([]) and z.enum([])…
Browse files Browse the repository at this point in the history
….array() via useEnumValues hook
  • Loading branch information
golebiowskib committed May 16, 2023
1 parent fe3102b commit 3a9cc2a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
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)
if (!(type._def.typeName === z.ZodFirstPartyTypeKind.ZodEnum)) return;
return type._def.values as readonly string[];
}
Expand Down

0 comments on commit 3a9cc2a

Please sign in to comment.