|
| 1 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 2 | +import { HiddenField } from '@lambdacurry/forms/remix-hook-form/hidden-field'; |
| 3 | +import { TextField } from '@lambdacurry/forms/remix-hook-form/text-field'; |
| 4 | +import { Button } from '@lambdacurry/forms/ui/button'; |
| 5 | +import type { Meta, StoryObj } from '@storybook/react-vite'; |
| 6 | +import { type ActionFunctionArgs, useFetcher } from 'react-router'; |
| 7 | +import { RemixFormProvider, getValidatedFormData, useRemixForm } from 'remix-hook-form'; |
| 8 | +import { z } from 'zod'; |
| 9 | +import { withReactRouterStubDecorator } from '../lib/storybook/react-router-stub'; |
| 10 | + |
| 11 | +const formSchema = z.object({ |
| 12 | + // Hidden field still participates in validation and submission |
| 13 | + lineItemId: z.string().min(1, 'Missing line item id'), |
| 14 | + note: z.string().optional(), |
| 15 | +}); |
| 16 | + |
| 17 | +type FormData = z.infer<typeof formSchema>; |
| 18 | + |
| 19 | +const DEFAULT_ID = 'abc-123'; |
| 20 | + |
| 21 | +const HiddenFieldExample = () => { |
| 22 | + const fetcher = useFetcher<{ message: string; submittedId: string }>(); |
| 23 | + const methods = useRemixForm<FormData>({ |
| 24 | + resolver: zodResolver(formSchema), |
| 25 | + defaultValues: { |
| 26 | + lineItemId: DEFAULT_ID, |
| 27 | + note: '', |
| 28 | + }, |
| 29 | + fetcher, |
| 30 | + submitConfig: { |
| 31 | + action: '/', |
| 32 | + method: 'post', |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + return ( |
| 37 | + <RemixFormProvider {...methods}> |
| 38 | + <fetcher.Form onSubmit={methods.handleSubmit}> |
| 39 | + {/* This registers the field without rendering any extra DOM */} |
| 40 | + <HiddenField name="lineItemId" /> |
| 41 | + |
| 42 | + {/* A visible field just to demonstrate normal layout around the hidden input */} |
| 43 | + <div className="space-y-4"> |
| 44 | + <TextField name="note" label="Note" placeholder="Optional note" /> |
| 45 | + <Button type="submit">Submit</Button> |
| 46 | + {fetcher.data?.message && ( |
| 47 | + <p className="mt-2 text-green-600"> |
| 48 | + {fetcher.data.message} – submittedId: {fetcher.data.submittedId} |
| 49 | + </p> |
| 50 | + )} |
| 51 | + </div> |
| 52 | + </fetcher.Form> |
| 53 | + </RemixFormProvider> |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +const handleFormSubmission = async (request: Request) => { |
| 58 | + const { data, errors } = await getValidatedFormData<FormData>(request, zodResolver(formSchema)); |
| 59 | + |
| 60 | + if (errors) { |
| 61 | + return { errors }; |
| 62 | + } |
| 63 | + |
| 64 | + return { message: 'Form submitted successfully', submittedId: data.lineItemId }; |
| 65 | +}; |
| 66 | + |
| 67 | +const meta: Meta<typeof HiddenField> = { |
| 68 | + title: 'RemixHookForm/HiddenField', |
| 69 | + component: HiddenField, |
| 70 | + parameters: { layout: 'centered' }, |
| 71 | + tags: ['autodocs'], |
| 72 | + decorators: [ |
| 73 | + withReactRouterStubDecorator({ |
| 74 | + routes: [ |
| 75 | + { |
| 76 | + path: '/', |
| 77 | + Component: HiddenFieldExample, |
| 78 | + action: async ({ request }: ActionFunctionArgs) => handleFormSubmission(request), |
| 79 | + }, |
| 80 | + ], |
| 81 | + }), |
| 82 | + ], |
| 83 | +} satisfies Meta<typeof HiddenField>; |
| 84 | + |
| 85 | +export default meta; |
| 86 | + |
| 87 | +type Story = StoryObj<typeof meta>; |
| 88 | + |
| 89 | +export const Default: Story = { |
| 90 | + parameters: { |
| 91 | + docs: { |
| 92 | + description: { |
| 93 | + story: |
| 94 | + 'HiddenField renders a plain <input type="hidden" /> registered with remix-hook-form. Use it to submit values that should not impact layout or be visible to users.', |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | +}; |
0 commit comments