-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-error-custom.stories.tsx
More file actions
233 lines (200 loc) · 7.16 KB
/
form-error-custom.stories.tsx
File metadata and controls
233 lines (200 loc) · 7.16 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { zodResolver } from '@hookform/resolvers/zod';
import { FormError, TextField } from '@lambdacurry/forms';
import { FormMessage } from '@lambdacurry/forms/remix-hook-form/form';
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>;
// Custom error message component with icon
const AlertErrorMessage = (props: React.ComponentProps<typeof FormMessage>) => (
<div className="flex items-center p-4 bg-red-50 border-l-4 border-red-400 rounded-md">
<svg
className="h-5 w-5 text-red-400 mr-3"
viewBox="0 0 20 20"
fill="currentColor"
role="img"
aria-labelledby="alert-icon-title"
>
<title id="alert-icon-title">Error</title>
<path
fillRule="evenodd"
d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z"
clipRule="evenodd"
/>
</svg>
<FormMessage className="text-red-800 font-medium" {...props} />
</div>
);
const CustomStyledFormErrorExample = () => {
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">Sign In</h2>
{/* Custom styled form error */}
<FormError
components={{
FormMessage: AlertErrorMessage,
}}
/>
<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 };
}
// Always show form error for demo purposes
return {
errors: {
_form: { message: 'Authentication service is temporarily unavailable. Please try again in a few minutes.' },
},
};
};
const meta: Meta<typeof FormError> = {
title: 'RemixHookForm/FormError/Custom',
component: FormError,
parameters: {
layout: 'centered',
docs: {
description: {
component: `
Custom styled FormError with branded error message component. The \`components\` prop allows you to completely customize how form errors are displayed.
**Component Override:**
\`\`\`typescript
<FormError
components={{
FormMessage: CustomErrorMessage,
}}
/>
\`\`\`
This example shows an alert-style error message with an icon and custom styling.
`,
},
},
},
tags: ['autodocs'],
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: CustomStyledFormErrorExample,
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: `
Custom styled FormError with branded error message component.
**Features:**
- Alert-style error message with warning icon
- Custom background colors and borders
- Enhanced typography and spacing
- Maintains accessibility attributes
The custom component receives all the same props as the default FormMessage component.
`,
},
},
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Verify initial state with custom styling', 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(/authentication service is temporarily unavailable/i)).not.toBeInTheDocument();
});
await step('Test custom styled form error display', async () => {
const emailInput = canvas.getByLabelText(/email address/i);
const passwordInput = canvas.getByLabelText(/password/i);
await userEvent.type(emailInput, 'test@example.com');
await userEvent.type(passwordInput, 'password123');
const submitButton = canvas.getByRole('button', { name: /sign in/i });
await userEvent.click(submitButton);
// Wait for error to appear
await new Promise((resolve) => setTimeout(resolve, 500));
// Check for error message
const errorMessage = canvas.queryByText(/authentication service is temporarily unavailable/i);
expect(errorMessage).toBeInTheDocument();
});
await step('Verify custom error styling and structure', async () => {
// Wait for error message to be available
await new Promise((resolve) => setTimeout(resolve, 500));
const errorMessage = canvas.queryByText(/authentication service is temporarily unavailable/i);
expect(errorMessage).toBeInTheDocument();
const errorContainer = errorMessage?.closest('div');
expect(errorContainer).toHaveClass(
'flex',
'items-center',
'p-4',
'bg-red-50',
'border-l-4',
'border-red-400',
'rounded-md',
);
const icon = errorContainer?.querySelector('svg');
expect(icon).toBeInTheDocument();
expect(icon).toHaveClass('h-5', 'w-5', 'text-red-400');
expect(errorMessage).toHaveClass('text-destructive', 'font-medium');
});
},
};