-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone-input.stories.tsx
More file actions
251 lines (226 loc) · 7.91 KB
/
phone-input.stories.tsx
File metadata and controls
251 lines (226 loc) · 7.91 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
import { zodResolver } from '@hookform/resolvers/zod';
import { PhoneInput } from '@lambdacurry/forms/remix-hook-form/phone-input';
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 successMessageRegex = /Form submitted successfully/;
// Define a schema for phone number validation
const formSchema = z.object({
usaPhone: z.string().min(1, 'USA phone number is required'),
internationalPhone: z.string().min(1, 'International phone number is required'),
});
type FormData = z.infer<typeof formSchema>;
const ControlledPhoneInputExample = () => {
const fetcher = useFetcher<{ message: string }>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
usaPhone: '',
internationalPhone: '',
},
fetcher,
submitConfig: {
action: '/',
method: 'post',
},
});
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit}>
<div className="grid gap-8">
<PhoneInput name="usaPhone" label="Phone Number" description="Enter a US phone number" />
<PhoneInput
name="internationalPhone"
label="International Phone Number"
description="Enter an international phone number"
isInternational={true}
/>
</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 { data, errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema));
if (errors) {
return { errors };
}
return {
message: `Form submitted successfully! USA: ${data.usaPhone}, International: ${data.internationalPhone}`,
};
};
const meta: Meta<typeof PhoneInput> = {
title: 'RemixHookForm/PhoneInput',
component: PhoneInput,
parameters: { layout: 'centered' },
tags: ['autodocs'],
} satisfies Meta<typeof PhoneInput>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Default: Story = {
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
Component: ControlledPhoneInputExample,
action: async ({ request }: ActionFunctionArgs) => handleFormSubmission(request),
},
],
}),
],
parameters: {
docs: {
description: {
story: 'Phone input component with US and international number support.',
},
source: {
code: `
const formSchema = z.object({
usaPhone: z.string().min(1, 'USA phone number is required'),
internationalPhone: z.string().min(1, 'International phone number is required'),
});
const ControlledPhoneInputExample = () => {
const fetcher = useFetcher<{ message: string }>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
usaPhone: '',
internationalPhone: '',
},
fetcher,
submitConfig: {
action: '/',
method: 'post',
},
});
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit}>
<div className="grid gap-8">
<PhoneInput
name="usaPhone"
label="Phone Number"
description="Enter a US phone number"
/>
<PhoneInput
name="internationalPhone"
label="International Phone Number"
description="Enter an international phone number"
isInternational
/>
</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 () => {
// Wait for inputs to be mounted and associated with their labels
const usaPhoneLabel = await canvas.findByLabelText('Phone Number');
const internationalPhoneLabel = await canvas.findByLabelText('International Phone Number');
expect(usaPhoneLabel).toBeInTheDocument();
expect(internationalPhoneLabel).toBeInTheDocument();
// Wait for submit button to be present
const submitButton = await canvas.findByRole('button', { name: 'Submit' });
expect(submitButton).toBeInTheDocument();
});
await step('Test validation errors on invalid submission', async () => {
// Submit form without entering phone numbers
const submitButton = await canvas.findByRole('button', { name: 'Submit' });
await userEvent.click(submitButton);
// Verify validation error messages appear
await expect(canvas.findByText('USA phone number is required')).resolves.toBeInTheDocument();
await expect(canvas.findByText('International phone number is required')).resolves.toBeInTheDocument();
});
await step('Test successful form submission with valid phone numbers', async () => {
// Enter valid phone numbers (await the inputs before typing)
const usaPhoneInput = await canvas.findByLabelText('Phone Number');
const internationalPhoneInput = await canvas.findByLabelText('International Phone Number');
// Enter a US phone number (should format to (202) 555-0123)
await userEvent.type(usaPhoneInput, '2025550123');
// Enter an international phone number (UK example digits; component will normalize & format with + and spaces)
await userEvent.type(internationalPhoneInput, '7911123456');
// Submit form
const submitButton = await canvas.findByRole('button', { name: 'Submit' });
await userEvent.click(submitButton);
// Verify success message (regex matches the prefix of the success text)
await expect(canvas.findByText(successMessageRegex)).resolves.toBeInTheDocument();
});
},
};
export const WithCustomStyling: Story = {
decorators: [
withReactRouterStubDecorator({
routes: [
{
path: '/',
},
],
}),
],
render: () => {
const fetcher = useFetcher<{ message: string }>();
const methods = useRemixForm<FormData>({
resolver: zodResolver(formSchema),
defaultValues: {
usaPhone: '',
internationalPhone: '',
},
fetcher,
submitConfig: {
action: '/',
method: 'post',
},
});
return (
<RemixFormProvider {...methods}>
<fetcher.Form onSubmit={methods.handleSubmit}>
<div className="grid gap-8">
<PhoneInput
name="usaPhone"
label="Custom Styled Phone Input"
description="With custom styling applied"
className="border-2 border-blue-500 p-4 rounded-lg"
inputClassName="bg-gray-100"
/>
<PhoneInput
name="internationalPhone"
label="Custom Styled Intl Phone Input"
description="With custom styling applied"
isInternational
className="border-2 border-blue-500 p-4 rounded-lg"
inputClassName="bg-gray-100"
/>
</div>
<Button type="submit" className="mt-8">
Submit
</Button>
</fetcher.Form>
</RemixFormProvider>
);
},
parameters: {
docs: {
description: {
story: 'Phone input with custom styling applied for US and International modes.',
},
},
},
};