Skip to content

Commit

Permalink
fix(useTextFieldProps): make textField initializable via options (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Jan 18, 2024
1 parent ce15cf3 commit 256cc14
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/fields/text-field/Docs.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Meta, Markdown } from "@storybook/blocks";

import { Meta, Markdown, Stories } from "@storybook/blocks";
import * as TextFieldStories from "./textField.stories";
import Config from "./config.md?raw";

<Meta title="fields/textField" />
<Meta title="fields/textField" of={TextFieldStories} />

# `textField(): ZodField<string>`

Expand All @@ -19,3 +19,5 @@ const bio = textField().optional();
## Initial Config

<Markdown>{Config}</Markdown>

<Stories />
21 changes: 21 additions & 0 deletions src/fields/text-field/TextInput.mock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComponentProps } from "react";

import { FieldErrors, FieldLabel } from "../../components";
import { TextFieldProps, useTextFieldProps } from "../../hooks";

export const TextInput = ({
field,
label,
initialValue,
...inputProps
}: TextFieldProps & ComponentProps<"input">) => {
const props = useTextFieldProps(field, { initialValue });

return (
<p>
<FieldLabel field={field} label={label} />
<input {...inputProps} {...props} />
<FieldErrors field={field} />
</p>
);
};
45 changes: 45 additions & 0 deletions src/fields/text-field/textField.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { textField } from "./textField";
import { TextInput } from "./TextInput.mock";
import { formStory, meta } from "../../scenarios/StoryForm";

export default {
...meta,
title: "fields/textField",
};

export const Required = formStory({
args: {
fields: {
name: textField({ name: "name" }),
},
children: ({ fields }) => (
<TextInput field={fields.name} label="Your Name" />
),
},
});

export const Optional = formStory({
args: {
fields: {
nick: textField({ name: "nick" }).optional(),
},
children: ({ fields }) => (
<TextInput field={fields.nick} label="Your Nick" />
),
},
});

export const Initialized = formStory({
args: {
fields: {
title: textField({ name: "title" }).optional(),
},
children: ({ fields }) => (
<TextInput
field={fields.title}
label="Blog Post Title"
initialValue="How to initialize input"
/>
),
},
});
17 changes: 17 additions & 0 deletions src/hooks/use-text-field-props/useTextFieldProps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { renderHook } from "@testing-library/react";
import { describe, expect, it } from "vitest";

import { useTextFieldProps } from "./useTextFieldProps";
import { textField } from "../../fields";

describe("useTextFieldProps()", () => {
it("initializes the field via options", async () => {
const field = textField();

const props = renderHook(() =>
useTextFieldProps(field, { initialValue: "hello" }),
);

expect(props.result.current.value).toBe("hello");
});
});
9 changes: 7 additions & 2 deletions src/hooks/use-text-field-props/useTextFieldProps.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { UseFieldOptions } from "form-atoms";
import { ChangeEvent } from "react";

import { FieldProps, useFieldProps } from "../";
import { type TextField } from "../../fields";
import type { TextField, TextFieldValue } from "../../fields";

export type TextFieldProps = FieldProps<TextField>;

const getEventValue = (
event: ChangeEvent<HTMLInputElement> | ChangeEvent<HTMLTextAreaElement>,
) => event.currentTarget.value;

export const useTextFieldProps = (field: TextField) =>
export const useTextFieldProps = (
field: TextField,
options: UseFieldOptions<TextFieldValue>,
) =>
useFieldProps<TextField, HTMLInputElement | HTMLTextAreaElement>(
field,
getEventValue,
options,
);

0 comments on commit 256cc14

Please sign in to comment.