ragflow-form.stories.tsx


Overview

This file defines Storybook stories for demonstrating the usage of the RAGFlowFormItem component within a form context. RAGFlowFormItem is a form item wrapper component designed to provide consistent styling, layout, and integration with react-hook-form and shadcn/ui form primitives. The file showcases how to use this component in both vertical and horizontal layouts, with form validation powered by zod schemas.

The file serves primarily as documentation and visual test cases for UI developers and designers to see and interact with the RAGFlowFormItem in isolation via Storybook.


Key Components and Functions

FormSchema

const FormSchema = z.object({
  username: z.string().min(2, {
    message: 'Username must be at least 2 characters.',
  }),
  email: z.string().email({
    message: 'Please enter a valid email address.',
  }),
  description: z.string().optional(),
});

FormExample Component

function FormExample({
  horizontal = false,
  fieldName = 'username',
  label = 'Username',
  tooltip = 'Please enter your username',
  placeholder = 'Enter username',
}: {
  horizontal?: boolean;
  fieldName?: string;
  label?: string;
  tooltip?: string;
  placeholder?: string;
}) { ... }

Storybook Meta Configuration (meta)

const meta = { ... } satisfies Meta<typeof FormExample>;
export default meta;

Story Exports

The file exports two main stories demonstrating the form item layout variations:

VerticalLayout

export const VerticalLayout: Story = {
  args: {
    horizontal: false,
    fieldName: 'username',
    label: 'Username',
    tooltip: 'Please enter your username',
    placeholder: 'Enter username',
  },
  parameters: { ... }
};

HorizontalLayout

export const HorizontalLayout: Story = {
  args: {
    horizontal: true,
    fieldName: 'email',
    label: 'Email Address',
    tooltip: 'Please enter a valid email address',
    placeholder: 'Enter email',
  },
  parameters: { ... }
};

Important Implementation Details


Interaction with Other Parts of the System


Usage Summary

To use the form item component demonstrated here:

  1. Define a Zod schema for your form data.

  2. Create a React Hook Form instance with useForm and zodResolver.

  3. Wrap your input controls with RAGFlowFormItem, passing props like name, label, tooltip, and horizontal.

  4. Use the Form component to provide context and styling.

  5. Use this story file in Storybook to view and interact with the component or adapt it as a template for your forms.


Visual Diagram: Component Interaction Overview

componentDiagram
    direction TB
    FormExample --> Form
    Form --> form
    form --> RAGFlowFormItem
    RAGFlowFormItem --> Input
    RAGFlowFormItem ..> FormField : wraps
    RAGFlowFormItem ..> FormItem : wraps
    RAGFlowFormItem ..> FormLabel : wraps
    RAGFlowFormItem ..> FormControl : wraps
    RAGFlowFormItem ..> FormMessage : wraps
    FormExample .. uses react-hook-form ..> useForm
    useForm .. uses zodResolver ..> FormSchema

Summary

This file is a Storybook story definition showcasing the RAGFlowFormItem UI component integrated with react-hook-form and zod validation. It illustrates both vertical and horizontal layouts with customizable labels, tooltips, and placeholders. The example demonstrates best practices for form handling and UI composition in a React application using modern libraries. It also enriches Storybook documentation with usage examples and API controls to facilitate UI development.


End of Documentation for ragflow-form.stories.tsx