Skip to content

Commit

Permalink
fix: initialize advanced fields with empty values
Browse files Browse the repository at this point in the history
fixes #52
  • Loading branch information
MiroslavPetrik committed Nov 30, 2023
1 parent 5d00dca commit 67e8ab5
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/fields/array-field/arrayField.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { renderHook } from "@testing-library/react";
import { formAtom, useFormValues } from "form-atoms";
import { describe, expect, it } from "vitest";
import { z } from "zod";

import { arrayField } from "./arrayField";

const elementSchema = z.string();

describe("arrayField()", () => {
it("is initialized as empty string", () => {
const classic = arrayField({ elementSchema });
const explicitUndefined = arrayField({ elementSchema, value: undefined });

const form = formAtom({ classic, explicitUndefined });
const { result } = renderHook(() => useFormValues(form));

expect(result.current).toEqual({
classic: [],
explicitUndefined: [],
});
});
});
3 changes: 2 additions & 1 deletion src/fields/array-field/arrayField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export type ArrayFieldParams<ElementSchema extends z.Schema> = Partial<

export const arrayField = <ElementSchema extends z.Schema>({
required_error = defaultParams.required_error,
value = [],
elementSchema,
...config
}: { elementSchema: ElementSchema } & ArrayFieldParams<ElementSchema>) =>
zodField({
value: [],
value,
schema: z.array(elementSchema).nonempty(required_error),
optionalSchema: z.array(elementSchema),
...config,
Expand Down
15 changes: 14 additions & 1 deletion src/fields/checkbox-field/checkboxField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { act, renderHook } from "@testing-library/react";
import { formAtom, useFormSubmit } from "form-atoms";
import { formAtom, useFormSubmit, useFormValues } from "form-atoms";
import { describe, expect, it, vi } from "vitest";

import { checkboxField } from "./checkboxField";

describe("checkboxField()", () => {
it("is initialized as false boolean", () => {
const classic = checkboxField();
const explicitUndefined = checkboxField({ value: undefined });

const form = formAtom({ classic, explicitUndefined });
const { result } = renderHook(() => useFormValues(form));

expect(result.current).toEqual({
classic: false,
explicitUndefined: false,
});
});

describe("when required", () => {
it("doesn't submit empty", async () => {
const field = checkboxField();
Expand Down
3 changes: 2 additions & 1 deletion src/fields/checkbox-field/checkboxField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type CheckboxFieldValue = ExtractAtomValue<

export const checkboxField = ({
required_error = defaultParams.required_error,
value = false,
...config
}: Partial<
Omit<
Expand All @@ -21,7 +22,7 @@ export const checkboxField = ({
> &
ZodParams = {}) =>
zodField({
value: false,
value,
/**
* When checkbox is required, it must be checked, so the value must be true.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/fields/files-field/filesField.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { renderHook } from "@testing-library/react";
import { formAtom, useFormValues } from "form-atoms";
import { describe, expect, it } from "vitest";

import { filesField } from "./filesField";

describe("filesField()", () => {
it("is initialized as empty string", () => {
const classic = filesField();
const explicitUndefined = filesField({ value: undefined });

const form = formAtom({ classic, explicitUndefined });
const { result } = renderHook(() => useFormValues(form));

expect(result.current).toEqual({
classic: [],
explicitUndefined: [],
});
});
});
20 changes: 20 additions & 0 deletions src/fields/text-field/textField.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { renderHook } from "@testing-library/react";
import { formAtom, useFormValues } from "form-atoms";
import { describe, expect, it } from "vitest";

import { textField } from "./textField";

describe("textField()", () => {
it("is initialized as empty string", () => {
const classic = textField();
const explicitUndefined = textField({ value: undefined });

const form = formAtom({ classic, explicitUndefined });
const { result } = renderHook(() => useFormValues(form));

expect(result.current).toEqual({
classic: "",
explicitUndefined: "",
});
});
});
3 changes: 2 additions & 1 deletion src/fields/text-field/textField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export type TextFieldValue = ExtractAtomValue<

export const textField = ({
required_error = defaultParams.required_error,
value = "",
...config
}: Partial<ZodFieldConfig<ZodString, ZodString>> & ZodParams = {}) =>
zodField({
value: "",
value,
// https://github.com/colinhacks/zod/issues/63
schema: z.string().trim().min(1, required_error),
optionalSchema: z.string().trim(),
Expand Down

0 comments on commit 67e8ab5

Please sign in to comment.