-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useTextFieldProps): make textField initializable via options (#88)
- Loading branch information
1 parent
ce15cf3
commit 256cc14
Showing
5 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
/> | ||
), | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); |