Skip to content

Commit fd25a6c

Browse files
committed
test: add react-hook-form example
Refs: #109
1 parent 12e8ccd commit fd25a6c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"prettier": "^3.4.2",
6969
"react": "^19.0.0",
7070
"react-dom": "^19.0.0",
71+
"react-hook-form": "^7.69.0",
7172
"sass": "^1.83.0",
7273
"size-limit": "^11.1.6",
7374
"storybook": "^8.4.7",

src/pin-field.stories.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { FC, StrictMode as ReactStrictMode } from "react";
1+
import React, { FC } from "react";
2+
import { Controller, useForm } from "react-hook-form";
23
import type { Meta, StoryObj } from "@storybook/react";
34
import { fn } from "@storybook/test";
45

@@ -40,9 +41,9 @@ export const Default: StoryObj<typeof PinField> = {
4041
*/
4142
export const StrictMode: StoryObj<typeof PinField> = {
4243
render: props => (
43-
<ReactStrictMode>
44+
<React.StrictMode>
4445
<PinField {...props} />
45-
</ReactStrictMode>
46+
</React.StrictMode>
4647
),
4748
args: defaultArgs,
4849
};
@@ -165,4 +166,29 @@ export const HTMLInputAttributes: StoryObj<FC<Props & { formatAriaLabelEval: str
165166
},
166167
};
167168

169+
/**
170+
* Exemple with react-hook-form.
171+
* Refs: https://github.com/soywod/react-pin-field/issues/109
172+
*/
173+
export const ReactHookForm: StoryObj<typeof PinField> = {
174+
render: () => {
175+
const { control, handleSubmit } = useForm();
176+
177+
return (
178+
<form onSubmit={handleSubmit(console.log)}>
179+
<Controller
180+
name="pin"
181+
control={control}
182+
render={({ field: { onChange } }) => (
183+
<PinField autoFocus length={6} placeholder="0" onChange={onChange} />
184+
)}
185+
/>
186+
<button type="submit">Submit</button>
187+
</form>
188+
);
189+
},
190+
argTypes: {},
191+
args: {},
192+
};
193+
168194
export default meta;

0 commit comments

Comments
 (0)