Skip to content

Commit debe6f2

Browse files
feat: improve story for the useWatchToTrigger hook (#527)
1 parent c75363d commit debe6f2

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

src/lib/form/useWatchToTrigger/docs.stories.tsx

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Button, Stack } from '@chakra-ui/react';
1+
import {
2+
Button,
3+
Code,
4+
ListItem,
5+
OrderedList,
6+
Stack,
7+
Text,
8+
} from '@chakra-ui/react';
29
import { zodResolver } from '@hookform/resolvers/zod';
310
import { useForm } from 'react-hook-form';
411
import { z } from 'zod';
@@ -39,7 +46,7 @@ const formSchema = () =>
3946
}
4047
});
4148

42-
export const WithoutHook = () => {
49+
export const WithHook = () => {
4350
const form = useForm({
4451
mode: 'onBlur',
4552
resolver: zodResolver(formSchema()),
@@ -50,9 +57,32 @@ export const WithoutHook = () => {
5057
},
5158
});
5259

60+
useWatchToTrigger({ form, names: ['min', 'default', 'max'] });
61+
5362
return (
5463
<Form {...form}>
5564
<Stack>
65+
<Text>
66+
This is the story with the hook. You can find the story without the
67+
hook below. Here is the scenario for maximal reproduction:
68+
</Text>
69+
<OrderedList>
70+
<ListItem>
71+
Set the <Code>min</Code> to 5
72+
</ListItem>
73+
<ListItem>The validation is updated</ListItem>
74+
<ListItem>
75+
Set the <Code>default</Code> to 5.5
76+
</ListItem>
77+
<ListItem>
78+
The validation is updated even if the validation is on the{' '}
79+
<Code>min</Code> field
80+
</ListItem>
81+
<ListItem>
82+
You have to go to the <Code>min</Code> field to trigger the{' '}
83+
<Code>onBlur</Code> event
84+
</ListItem>
85+
</OrderedList>
5686
<FormField>
5787
<FormFieldLabel>Min</FormFieldLabel>
5888
<FormFieldController
@@ -85,7 +115,7 @@ export const WithoutHook = () => {
85115
);
86116
};
87117

88-
export const WithHook = () => {
118+
export const WithoutHook = () => {
89119
const form = useForm({
90120
mode: 'onBlur',
91121
resolver: zodResolver(formSchema()),
@@ -96,11 +126,27 @@ export const WithHook = () => {
96126
},
97127
});
98128

99-
useWatchToTrigger({ form, names: ['min', 'default', 'max'] });
100-
101129
return (
102130
<Form {...form}>
103131
<Stack>
132+
<Text>
133+
This is the story without the hook. Here is the scenario for maximal
134+
reproduction:
135+
</Text>
136+
<OrderedList>
137+
<ListItem>
138+
Set the <Code>min</Code> to 5
139+
</ListItem>
140+
<ListItem>The validation is updated only on blur</ListItem>
141+
<ListItem>
142+
Set the <Code>default</Code> to 5.5
143+
</ListItem>
144+
<ListItem>
145+
The validation is not updated for the <Code>min</Code> field, that
146+
is because we are in the <Code>default</Code> field. Even the blur
147+
event will not trigger the validation.
148+
</ListItem>
149+
</OrderedList>
104150
<FormField>
105151
<FormFieldLabel>Min</FormFieldLabel>
106152
<FormFieldController

0 commit comments

Comments
 (0)