-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.stories.tsx
More file actions
180 lines (159 loc) · 6.17 KB
/
checkbox.stories.tsx
File metadata and controls
180 lines (159 loc) · 6.17 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
import { zodResolver } from '@hookform/resolvers/zod';
import { Checkbox } from '@lambdacurry/forms/remix-hook-form/checkbox';
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({
terms: z.boolean().refine((val) => val === true, 'You must accept the terms and conditions'),
marketing: z.boolean().optional(),
required: z.boolean().refine((val) => val === true, 'This field is required'),
});
type FormData = z.infer<typeof formSchema>;
const ControlledCheckboxExample = () => {
const fetcher = useFetcher<{ message: string }>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
terms: false as true, // Note: ZOD Schema expects a true value
marketing: false,
required: false as true, //Note: ZOD Schema expects a true value
},
fetcher,
submitConfig: {
action: '/',
method: 'post',
},
});
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit}>
<div className="grid gap-8">
<Checkbox name="terms" label="Accept terms and conditions" />
<Checkbox
name="marketing"
label="Receive marketing emails"
description="We will send you hourly updates about our products"
/>
<Checkbox name="required" label="This is a required checkbox" />
</div>
<Button type="submit" className="mt-8">
Submit
</Button>
{fetcher.data?.message && <p className="mt-2 text-green-600">{fetcher.data.message}</p>}
</fetcher.Form>
</RemixFormProvider>
);
};
const handleFormSubmission = async (request: Request) => {
const { errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
return { message: 'Form submitted successfully' };
};
const meta: Meta<typeof Checkbox> = {
title: 'RemixHookForm/Checkbox',
component: Checkbox,
parameters: { layout: 'centered' },
tags: ['autodocs'],
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: ControlledCheckboxExample,
action: async ({ request }: ActionFunctionArgs) => handleFormSubmission(request),
},
],
}),
],
} satisfies Meta<typeof Checkbox>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Default: Story = {
parameters: {
docs: {
description: {
story: 'The default checkbox component.',
},
source: {
code: `
const formSchema = z.object({
terms: z.boolean().refine(val => val === true, 'You must accept the terms and conditions'),
marketing: z.boolean().optional(),
required: z.boolean().refine(val => val === true, 'This field is required'),
});
const ControlledCheckboxExample = () => {
const fetcher = useFetcher<{ message: string }>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
terms: false as true, // Note: ZOD Schema expects a true value
marketing: false,
required: false as true //Note: ZOD Schema expects a true value
},
fetcher,
submitConfig: {
action: '/',
method: 'post',
},
});
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit}>
<div className='grid gap-8'>
<Checkbox name="terms" label="Accept terms and conditions" />
<Checkbox name="marketing" label="Receive marketing emails" description="We will send you hourly updates about our products" />
<Checkbox name="required" label="This is a required checkbox" />
</div>
<Button type="submit" className="mt-8">
Submit
</Button>
{fetcher.data?.message && <p className="mt-2 text-green-600">{fetcher.data.message}</p>}
</fetcher.Form>
</RemixFormProvider>
);
};`,
},
},
},
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement);
await step('Verify initial state', async () => {
// Verify all checkboxes are unchecked initially
const termsCheckbox = canvas.getByLabelText('Accept terms and conditions');
const marketingCheckbox = canvas.getByLabelText('Receive marketing emails');
const requiredCheckbox = canvas.getByLabelText('This is a required checkbox');
expect(termsCheckbox).not.toBeChecked();
expect(marketingCheckbox).not.toBeChecked();
expect(requiredCheckbox).not.toBeChecked();
// Verify submit button is present
const submitButton = canvas.getByRole('button', { name: 'Submit' });
expect(submitButton).toBeInTheDocument();
});
await step('Test validation errors on invalid submission', async () => {
// Submit form without checking required checkboxes
const submitButton = canvas.getByRole('button', { name: 'Submit' });
await userEvent.click(submitButton);
// Verify validation error messages appear
await expect(canvas.findByText('You must accept the terms and conditions')).resolves.toBeInTheDocument();
await expect(canvas.findByText('This field is required')).resolves.toBeInTheDocument();
});
await step('Test successful form submission', async () => {
// Check required checkboxes
const termsCheckbox = canvas.getByLabelText('Accept terms and conditions');
const requiredCheckbox = canvas.getByLabelText('This is a required checkbox');
await userEvent.click(termsCheckbox);
await userEvent.click(requiredCheckbox);
// Submit form
const submitButton = canvas.getByRole('button', { name: 'Submit' });
await userEvent.click(submitButton);
// Verify success message
await expect(canvas.findByText('Form submitted successfully')).resolves.toBeInTheDocument();
});
},
};