-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-error.stories.tsx
More file actions
856 lines (710 loc) · 29.4 KB
/
Copy pathform-error.stories.tsx
File metadata and controls
856 lines (710 loc) · 29.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
import type { Meta, StoryObj } from '@storybook/react';
import { zodResolver } from '@hookform/resolvers/zod';
import { RemixFormProvider, useRemixForm, getValidatedFormData } from 'remix-hook-form';
import { useFetcher, type ActionFunctionArgs } from 'react-router';
import { z } from 'zod';
import { FormError, TextField } from '@lambdacurry/forms';
import { Button } from '@lambdacurry/forms/ui/button';
import { FormMessage } from '@lambdacurry/forms/remix-hook-form/form';
import { expect, userEvent, within } from '@storybook/test';
import { withReactRouterStubDecorator } from '../lib/storybook/react-router-stub';
// Form schema for testing
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>;
// Basic Form Error Story
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: '/login',
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>
);
};
// Mixed Errors Story (Field + Form level)
const MixedErrorsExample = () => {
const fetcher = useFetcher<{
message?: string;
errors?: Record<string, { message: string }>
}>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
email: '',
password: '',
},
fetcher,
submitConfig: {
action: '/register',
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">Registration Form</h2>
{/* Form-level error at the top */}
<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 ? 'Creating Account...' : 'Create Account'}
</Button>
{/* Form-level error at the bottom as well */}
<FormError className="mt-4" />
{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>
);
};
// Custom Styling Story
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: '/custom-login',
method: 'post',
},
});
// Custom error message component
const CustomErrorMessage = (props: React.ComponentPropsWithoutRef<typeof FormMessage>) => (
<div className="flex items-center p-4 bg-red-50 border-l-4 border-red-400 rounded-md">
<div className="flex-shrink-0">
<svg className="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
</svg>
</div>
<div className="ml-3">
<FormMessage className="text-red-800 font-medium" {...props} />
</div>
</div>
);
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">Custom Styled Form</h2>
{/* Custom styled form error */}
<FormError
className="mb-4"
components={{
FormMessage: CustomErrorMessage,
}}
/>
<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.Form>
</RemixFormProvider>
);
};
// Placement Variations Story
const PlacementVariationsExample = () => {
const fetcher = useFetcher<{
message?: string;
errors?: Record<string, { message: string }>
}>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
email: '',
password: '',
},
fetcher,
submitConfig: {
action: '/placement-test',
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">Error Placement Variations</h2>
{/* Top placement */}
<FormError className="p-3 bg-red-50 border border-red-200 rounded-lg" />
<TextField
name="email"
type="email"
label="Email Address"
placeholder="Enter your email"
disabled={isSubmitting}
/>
{/* Inline placement between fields */}
<FormError className="text-center p-2 bg-yellow-50 border border-yellow-200 rounded text-yellow-800" />
<TextField
name="password"
type="password"
label="Password"
placeholder="Enter your password"
disabled={isSubmitting}
/>
<Button type="submit" disabled={isSubmitting} className="w-full">
{isSubmitting ? 'Processing...' : 'Submit'}
</Button>
{/* Bottom placement */}
<FormError className="mt-4 p-3 bg-red-100 border-l-4 border-red-500 rounded-r" />
</fetcher.Form>
</RemixFormProvider>
);
};
// Server action handlers for different scenarios
const handleBasicFormError = async (request: Request) => {
const { data, errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
// Simulate server-side authentication failure
if (data.email !== 'user@example.com' || data.password !== 'password123') {
return {
errors: {
_form: { message: 'Invalid email or password. Please try again.' }
}
};
}
return { message: 'Login successful!' };
};
const handleMixedErrors = async (request: Request) => {
const { data, errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
// Simulate email already exists + server error
if (data.email === 'taken@example.com') {
return {
errors: {
email: { message: 'This email address is already registered' },
_form: { message: 'Registration failed. Please check your information and try again.' }
}
};
}
// Simulate network/server error
if (data.password === 'servererror') {
return {
errors: {
_form: { message: 'Server error occurred. Please try again later.' }
}
};
}
return { message: 'Account created successfully!' };
};
const handleCustomStyledError = async (request: Request) => {
const { data, errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
// Always return a form-level error for demonstration
return {
errors: {
_form: { message: 'Authentication service is temporarily unavailable. Please try again in a few minutes.' }
}
};
};
const handlePlacementTest = async (request: Request) => {
const { data, errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
// Always return a form-level error to show placement variations
return {
errors: {
_form: { message: 'This error appears in multiple locations to demonstrate placement flexibility.' }
}
};
};
const meta: Meta<typeof FormError> = {
title: 'RemixHookForm/FormError',
component: FormError,
decorators: [withReactRouterStubDecorator],
parameters: {
layout: 'centered',
docs: {
description: {
component: `
The FormError component provides a standardized way to display form-level errors in your forms.
It automatically looks for errors with the key "_form" by default, but can be configured to use any error key.
## Key Features
- **Automatic Error Detection**: Looks for \`errors._form.message\` by default
- **Flexible Placement**: Can be placed anywhere in your form
- **Component Override**: Supports custom styling via the \`components\` prop
- **Consistent API**: Follows the same patterns as other form components
## Usage Patterns
1. **Basic Usage**: \`<FormError />\` - Displays errors._form.message
2. **Custom Error Key**: \`<FormError name="general" />\` - Displays errors.general.message
3. **Custom Styling**: Use the \`components\` prop to override FormMessage
4. **Multiple Placement**: Place multiple FormError components for different layouts
## Server Action Pattern
Return form-level errors from your server actions:
\`\`\`typescript
return {
errors: {
_form: { message: 'Server error occurred. Please try again.' }
}
};
\`\`\`
`,
},
},
},
argTypes: {
name: {
control: 'text',
description: 'The error key to look for in the form errors object',
defaultValue: '_form',
},
className: {
control: 'text',
description: 'Additional CSS classes for styling and positioning',
},
},
};
export default meta;
type Story = StoryObj<typeof FormError>;
export const BasicFormError: Story = {
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: BasicFormErrorExample,
action: async ({ request }: ActionFunctionArgs) => handleBasicFormError(request),
},
],
}),
],
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 \`badpass\` (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 () => {
// Verify form elements are present
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();
// Verify no form-level error is shown initially
expect(canvas.queryByText(/invalid email or password/i)).not.toBeInTheDocument();
expect(canvas.queryByText(/server error/i)).not.toBeInTheDocument();
});
await step('Test field-level validation errors', async () => {
// Submit form without filling fields
const submitButton = canvas.getByRole('button', { name: /sign in/i });
await userEvent.click(submitButton);
// Verify field-level validation errors appear
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();
// Verify no form-level error is shown for validation errors
expect(canvas.queryByText(/invalid email or password/i)).not.toBeInTheDocument();
});
await step('Test form-level error with invalid credentials', async () => {
// Fill in invalid credentials
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');
// Submit form
const submitButton = canvas.getByRole('button', { name: /sign in/i });
await userEvent.click(submitButton);
// Verify form-level error appears
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();
expect(canvas.queryByText(/password must be at least 6 characters/i)).not.toBeInTheDocument();
});
await step('Test successful form submission', async () => {
// Fill in correct credentials
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, 'user@example.com');
await userEvent.type(passwordInput, 'password123');
// Submit form
const submitButton = canvas.getByRole('button', { name: /sign in/i });
await userEvent.click(submitButton);
// Verify success message appears and form error is cleared
await expect(canvas.findByText(/login successful/i)).resolves.toBeInTheDocument();
expect(canvas.queryByText(/invalid email or password/i)).not.toBeInTheDocument();
});
},
};
export const MixedErrors: Story = {
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: MixedErrorsExample,
action: async ({ request }: ActionFunctionArgs) => handleMixedErrors(request),
},
],
}),
],
parameters: {
docs: {
description: {
story: `
Demonstrates handling both field-level and form-level errors simultaneously.
**Try this:**
1. Enter \`taken@example.com\` as email (shows both field and form errors)
2. Enter password \`servererror\` (shows only form-level error)
3. Notice FormError appears both at top and bottom of form
This pattern is useful when you want to show form-level context alongside specific field errors.
`,
},
},
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Verify initial state with multiple FormError placements', async () => {
// Verify form elements are present
const emailInput = canvas.getByLabelText(/email address/i);
const passwordInput = canvas.getByLabelText(/password/i);
const submitButton = canvas.getByRole('button', { name: /create account/i });
expect(emailInput).toBeInTheDocument();
expect(passwordInput).toBeInTheDocument();
expect(submitButton).toBeInTheDocument();
// Verify no form-level errors are shown initially (multiple FormError components)
expect(canvas.queryByText(/registration failed/i)).not.toBeInTheDocument();
expect(canvas.queryByText(/server error occurred/i)).not.toBeInTheDocument();
});
await step('Test mixed errors - field and form level together', async () => {
// Fill in email that already exists
const emailInput = canvas.getByLabelText(/email address/i);
const passwordInput = canvas.getByLabelText(/password/i);
await userEvent.type(emailInput, 'taken@example.com');
await userEvent.type(passwordInput, 'validpass123');
// Submit form
const submitButton = canvas.getByRole('button', { name: /create account/i });
await userEvent.click(submitButton);
// Verify both field-level and form-level errors appear
await expect(canvas.findByText(/this email address is already registered/i)).resolves.toBeInTheDocument();
await expect(canvas.findByText(/registration failed/i)).resolves.toBeInTheDocument();
// Verify form-level error appears in multiple locations (top and bottom)
const formErrors = canvas.getAllByText(/registration failed/i);
expect(formErrors).toHaveLength(2); // Should appear in both FormError components
});
await step('Test server error only (form-level error)', async () => {
// Clear and fill with server error trigger
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, 'test@example.com');
await userEvent.type(passwordInput, 'servererror');
// Submit form
const submitButton = canvas.getByRole('button', { name: /create account/i });
await userEvent.click(submitButton);
// Verify only form-level error appears
await expect(canvas.findByText(/server error occurred/i)).resolves.toBeInTheDocument();
// Verify field-level error is cleared
expect(canvas.queryByText(/this email address is already registered/i)).not.toBeInTheDocument();
// Verify form-level error appears in multiple locations
const formErrors = canvas.getAllByText(/server error occurred/i);
expect(formErrors).toHaveLength(2); // Should appear in both FormError components
});
await step('Test successful submission clears all errors', async () => {
// Fill in valid data
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, 'valid@example.com');
await userEvent.type(passwordInput, 'validpass123');
// Submit form
const submitButton = canvas.getByRole('button', { name: /create account/i });
await userEvent.click(submitButton);
// Verify success message appears
await expect(canvas.findByText(/account created successfully/i)).resolves.toBeInTheDocument();
// Verify all errors are cleared
expect(canvas.queryByText(/registration failed/i)).not.toBeInTheDocument();
expect(canvas.queryByText(/server error occurred/i)).not.toBeInTheDocument();
expect(canvas.queryByText(/this email address is already registered/i)).not.toBeInTheDocument();
});
},
};
export const CustomStyling: Story = {
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: CustomStyledFormErrorExample,
action: async ({ request }: ActionFunctionArgs) => handleCustomStyledError(request),
},
],
}),
],
parameters: {
docs: {
description: {
story: `
Custom styled FormError with branded error message component.
The \`components\` prop allows you to completely customize how form errors are displayed:
\`\`\`typescript
<FormError
components={{
FormMessage: CustomErrorMessage,
}}
/>
\`\`\`
This example shows an alert-style error message with an icon and custom styling.
`,
},
},
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Verify initial state with custom styling', async () => {
// Verify form elements are present
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();
// Verify no custom styled error is shown initially
expect(canvas.queryByText(/authentication service is temporarily unavailable/i)).not.toBeInTheDocument();
});
await step('Test custom styled form error display', async () => {
// Fill in any valid data (this story always shows form error for demo)
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');
// Submit form
const submitButton = canvas.getByRole('button', { name: /sign in/i });
await userEvent.click(submitButton);
// Verify custom styled error appears
await expect(canvas.findByText(/authentication service is temporarily unavailable/i)).resolves.toBeInTheDocument();
});
await step('Verify custom error styling and structure', async () => {
// Find the error message
const errorMessage = canvas.getByText(/authentication service is temporarily unavailable/i);
expect(errorMessage).toBeInTheDocument();
// Verify the custom styling structure
const errorContainer = errorMessage.closest('div');
expect(errorContainer).toHaveClass('flex', 'items-center', 'p-4', 'bg-red-50', 'border-l-4', 'border-red-400', 'rounded-md');
// Verify the icon is present (SVG element)
const icon = errorContainer?.querySelector('svg');
expect(icon).toBeInTheDocument();
expect(icon).toHaveClass('h-5', 'w-5', 'text-red-400');
// Verify the message has custom styling
expect(errorMessage).toHaveClass('text-red-800', 'font-medium');
});
await step('Test accessibility of custom styled error', async () => {
// Verify the error message has proper accessibility attributes
const errorMessage = canvas.getByText(/authentication service is temporarily unavailable/i);
// Check that it has the form-message data attribute
expect(errorMessage).toHaveAttribute('data-slot', 'form-message');
// Verify it's properly structured for screen readers
expect(errorMessage.tagName.toLowerCase()).toBe('p');
});
},
};
export const PlacementVariations: Story = {
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: PlacementVariationsExample,
action: async ({ request }: ActionFunctionArgs) => handlePlacementTest(request),
},
],
}),
],
parameters: {
docs: {
description: {
story: `
Shows different placement options for FormError components within a form.
**Placement Options:**
- **Top**: Above all form fields for immediate visibility
- **Inline**: Between form fields for contextual placement
- **Bottom**: After form fields and submit button
- **Multiple**: Use several FormError components with different styling
Each FormError instance shows the same error but can be styled differently using the \`className\` prop.
`,
},
},
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Verify initial state with multiple placement options', async () => {
// Verify form elements are present
const emailInput = canvas.getByLabelText(/email address/i);
const passwordInput = canvas.getByLabelText(/password/i);
const submitButton = canvas.getByRole('button', { name: /submit/i });
expect(emailInput).toBeInTheDocument();
expect(passwordInput).toBeInTheDocument();
expect(submitButton).toBeInTheDocument();
// Verify no errors are shown initially
expect(canvas.queryByText(/this error appears in multiple locations/i)).not.toBeInTheDocument();
});
await step('Test multiple FormError placements with different styling', async () => {
// Fill in valid data (this story always shows form error for demo)
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');
// Submit form
const submitButton = canvas.getByRole('button', { name: /submit/i });
await userEvent.click(submitButton);
// Verify error appears in multiple locations
await expect(canvas.findByText(/this error appears in multiple locations/i)).resolves.toBeInTheDocument();
// Verify multiple instances of the same error message
const errorMessages = canvas.getAllByText(/this error appears in multiple locations/i);
expect(errorMessages).toHaveLength(3); // Top, inline, and bottom placements
});
await step('Verify different styling for each placement', async () => {
const errorMessages = canvas.getAllByText(/this error appears in multiple locations/i);
// Check that each error message has different styling based on placement
const topError = errorMessages[0];
const inlineError = errorMessages[1];
const bottomError = errorMessages[2];
// Verify top placement styling (red background with border)
const topContainer = topError.closest('div');
expect(topContainer).toHaveClass('p-3', 'bg-red-50', 'border', 'border-red-200', 'rounded-lg');
// Verify inline placement styling (yellow background, centered)
const inlineContainer = inlineError.closest('div');
expect(inlineContainer).toHaveClass('text-center', 'p-2', 'bg-yellow-50', 'border', 'border-yellow-200', 'rounded', 'text-yellow-800');
// Verify bottom placement styling (red background with left border)
const bottomContainer = bottomError.closest('div');
expect(bottomContainer).toHaveClass('mt-4', 'p-3', 'bg-red-100', 'border-l-4', 'border-red-500', 'rounded-r');
});
await step('Test accessibility across all placements', async () => {
const errorMessages = canvas.getAllByText(/this error appears in multiple locations/i);
// Verify each error message has proper accessibility attributes
errorMessages.forEach((errorMessage) => {
expect(errorMessage).toHaveAttribute('data-slot', 'form-message');
expect(errorMessage.tagName.toLowerCase()).toBe('p');
});
});
await step('Verify form structure and error placement order', async () => {
// Get all form elements in order
const formElements = canvas.getByRole('form') || canvasElement;
const allElements = Array.from(formElements.querySelectorAll('*'));
// Find positions of error messages and form fields
const errorElements = allElements.filter(el =>
el.textContent?.includes('This error appears in multiple locations')
);
const emailInput = canvas.getByLabelText(/email address/i);
const passwordInput = canvas.getByLabelText(/password/i);
const submitButton = canvas.getByRole('button', { name: /submit/i });
// Verify error placement order: top error before email, inline error between fields, bottom error after submit
expect(errorElements).toHaveLength(3);
// This verifies the structural placement without relying on exact DOM order
expect(errorElements[0]).toBeInTheDocument(); // Top error
expect(errorElements[1]).toBeInTheDocument(); // Inline error
expect(errorElements[2]).toBeInTheDocument(); // Bottom error
});
},
};
// Action handlers for Storybook (not exported to avoid being treated as a story)
const actionHandlers = {
'/login': handleBasicFormError,
'/register': handleMixedErrors,
'/custom-login': handleCustomStyledError,
'/placement-test': handlePlacementTest,
};