-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-error-basic.stories.tsx
More file actions
199 lines (168 loc) · 6.3 KB
/
form-error-basic.stories.tsx
File metadata and controls
199 lines (168 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { zodResolver } from '@hookform/resolvers/zod';
import { FormError, TextField } from '@lambdacurry/forms';
import { Button } from '@lambdacurry/forms/ui/button';
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, userEvent, within } from '@storybook/test';
import { type ActionFunctionArgs, useFetcher } from 'react-router';
import { getValidatedFormData, RemixFormProvider, useRemixForm } from 'remix-hook-form';
import { z } from 'zod';
import { withReactRouterStubDecorator } from '../lib/storybook/react-router-stub';
const formSchema = z.object({
email: z.string().email('Please enter a valid email address'),
password: z.string().min(6, 'Password must be at least 6 characters'),
});
type FormData = z.infer<typeof formSchema>;
const BasicFormErrorExample = () => {
const fetcher = useFetcher<{
message?: string;
errors?: Record<string, { message: string }>;
}>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
email: '',
password: '',
},
fetcher,
submitConfig: {
action: '/',
method: 'post',
},
});
const isSubmitting = fetcher.state === 'submitting';
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit} className="max-w-md mx-auto p-6 space-y-4">
<h2 className="text-xl font-semibold text-gray-900">Login Form</h2>
{/* Form-level error display */}
<FormError className="mb-4" />
<TextField
name="email"
type="email"
label="Email Address"
placeholder="Enter your email"
disabled={isSubmitting}
/>
<TextField
name="password"
type="password"
label="Password"
placeholder="Enter your password"
disabled={isSubmitting}
/>
<Button type="submit" disabled={isSubmitting} className="w-full">
{isSubmitting ? 'Signing In...' : 'Sign In'}
</Button>
{fetcher.data?.message && (
<div className="mt-4 p-4 bg-green-50 border border-green-200 rounded-md">
<p className="text-green-700 font-medium">{fetcher.data.message}</p>
</div>
)}
</fetcher.Form>
</RemixFormProvider>
);
};
const handleFormSubmission = async (request: Request) => {
const { data, errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
// Simulate server-side authentication
if (data.email === 'wrong@email.com' && data.password === 'wrongpass') {
return {
errors: {
_form: { message: 'Invalid email or password. Please try again.' },
},
};
}
if (data.email === 'user@example.com' && data.password === 'password123') {
return { message: 'Login successful! Welcome back.' };
}
return {
errors: {
_form: { message: 'Invalid email or password. Please try again.' },
},
};
};
const meta: Meta<typeof FormError> = {
title: 'RemixHookForm/FormError/Basic',
component: FormError,
parameters: {
layout: 'centered',
docs: {
description: {
component: `
The FormError component provides standardized form-level error handling for server failures, authentication issues, and other form-wide errors.
**Key Features:**
- Automatic integration with remix-hook-form context
- Uses \`_form\` as the default error key
- Flexible placement anywhere in forms
- Component override support for custom styling
`,
},
},
},
tags: ['autodocs'],
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: BasicFormErrorExample,
action: async ({ request }: ActionFunctionArgs) => handleFormSubmission(request),
},
],
}),
],
} satisfies Meta<typeof FormError>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Default: Story = {
parameters: {
docs: {
description: {
story: `
Basic form error handling with server-side validation failure.
**Try this:**
1. Click "Sign In" without filling fields (shows field-level errors)
2. Enter invalid credentials like \`wrong@email.com\` and \`wrongpass\` (shows form-level error)
3. Enter \`user@example.com\` and \`password123\` for success
The FormError component automatically displays when \`errors._form\` exists in the server response.
`,
},
},
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Verify initial state', async () => {
const emailInput = canvas.getByLabelText(/email address/i);
const passwordInput = canvas.getByLabelText(/password/i);
const submitButton = canvas.getByRole('button', { name: /sign in/i });
expect(emailInput).toBeInTheDocument();
expect(passwordInput).toBeInTheDocument();
expect(submitButton).toBeInTheDocument();
expect(canvas.queryByText(/invalid email or password/i)).not.toBeInTheDocument();
});
await step('Test field-level validation errors', async () => {
const submitButton = canvas.getByRole('button', { name: /sign in/i });
await userEvent.click(submitButton);
await expect(canvas.findByText(/please enter a valid email address/i)).resolves.toBeInTheDocument();
await expect(canvas.findByText(/password must be at least 6 characters/i)).resolves.toBeInTheDocument();
expect(canvas.queryByText(/invalid email or password/i)).not.toBeInTheDocument();
});
await step('Test form-level error with invalid credentials', async () => {
const emailInput = canvas.getByLabelText(/email address/i);
const passwordInput = canvas.getByLabelText(/password/i);
await userEvent.clear(emailInput);
await userEvent.clear(passwordInput);
await userEvent.type(emailInput, 'wrong@email.com');
await userEvent.type(passwordInput, 'wrongpass');
const submitButton = canvas.getByRole('button', { name: /sign in/i });
await userEvent.click(submitButton);
// Wait for form-level error to appear
await expect(canvas.findByText(/invalid email or password/i)).resolves.toBeInTheDocument();
// Verify field-level errors are cleared
expect(canvas.queryByText(/please enter a valid email address/i)).not.toBeInTheDocument();
});
},
};