|
| 1 | +import { FormError } from '@lambdacurry/forms'; |
| 2 | +import type { Meta, StoryObj } from '@storybook/react-vite'; |
| 3 | +import { RemixFormProvider, useRemixForm } from 'remix-hook-form'; |
| 4 | +import { withReactRouterStubDecorator } from '../lib/storybook/react-router-stub'; |
| 5 | + |
| 6 | +const UncontrolledFormErrorExample = ({ message }: { message?: string }) => { |
| 7 | + const methods = useRemixForm({ |
| 8 | + defaultValues: { |
| 9 | + email: '', |
| 10 | + }, |
| 11 | + }); |
| 12 | + |
| 13 | + return ( |
| 14 | + <RemixFormProvider {...methods}> |
| 15 | + <div className="max-w-md mx-auto p-6 space-y-4 border rounded-lg shadow-sm"> |
| 16 | + <h2 className="text-xl font-semibold text-gray-900">Uncontrolled Error</h2> |
| 17 | + <p className="text-sm text-gray-500 mb-4"> |
| 18 | + This FormError is rendered with a manual message prop, bypassing the form state. |
| 19 | + </p> |
| 20 | + |
| 21 | + <FormError message={message} /> |
| 22 | + |
| 23 | + <div className="p-4 bg-gray-50 rounded-md border border-gray-200"> |
| 24 | + <p className="text-sm text-gray-600"> |
| 25 | + The error above is not coming from <code>errors._form</code>. It's passed directly as a prop. |
| 26 | + </p> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | + </RemixFormProvider> |
| 30 | + ); |
| 31 | +}; |
| 32 | + |
| 33 | +const meta: Meta<typeof FormError> = { |
| 34 | + title: 'RemixHookForm/FormError/Uncontrolled', |
| 35 | + component: FormError, |
| 36 | + parameters: { |
| 37 | + layout: 'centered', |
| 38 | + docs: { |
| 39 | + description: { |
| 40 | + component: ` |
| 41 | +The FormError component can be used in an "uncontrolled" mode by passing a \`message\` prop directly. |
| 42 | +This is useful for displaying errors that aren't managed by the form's validation state, such as generic network errors or manual UI feedback. |
| 43 | +
|
| 44 | +**Key Features of Uncontrolled Mode:** |
| 45 | +- Takes precedence over form-state errors |
| 46 | +- Does not require a \`name\` prop |
| 47 | +- Maintains consistent styling and accessibility |
| 48 | + `, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + tags: ['autodocs'], |
| 53 | + decorators: [ |
| 54 | + withReactRouterStubDecorator({ |
| 55 | + routes: [ |
| 56 | + { |
| 57 | + path: '/', |
| 58 | + Component: () => <UncontrolledFormErrorExample message="This is a manual, uncontrolled error message." />, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }), |
| 62 | + ], |
| 63 | +} satisfies Meta<typeof FormError>; |
| 64 | + |
| 65 | +export default meta; |
| 66 | +type Story = StoryObj<typeof meta>; |
| 67 | + |
| 68 | +export const Default: Story = { |
| 69 | + args: { |
| 70 | + message: 'A manual error message', |
| 71 | + }, |
| 72 | +}; |
| 73 | + |
| 74 | +export const CustomStyling: Story = { |
| 75 | + args: { |
| 76 | + message: 'A manual error message with custom styling', |
| 77 | + className: 'bg-red-50 p-4 border border-red-200 rounded-md', |
| 78 | + }, |
| 79 | +}; |
0 commit comments