create-folder-form.tsx


Overview

create-folder-form.tsx defines a React functional component CreateFolderForm which renders a form interface for creating a new folder by specifying its name. The form uses robust validation powered by the zod schema validation library integrated with react-hook-form for form state management and validation handling.

The primary purpose of this component is to collect and validate user input for a folder name, then pass the validated name back to a parent component via a callback, optionally closing the modal dialog containing the form.


Component: CreateFolderForm

Description

CreateFolderForm is a controlled form component used to create a new folder by entering its name. It validates the folder name to ensure it is not empty and trims whitespace. Upon successful submission, it calls a callback function with the folder name and closes the modal if requested.

Props

interface IModalProps<T> {
  hideModal?: () => void;
  onOk?: (data: T) => Promise<boolean | void> | boolean | void;
}

Validation Schema

The form uses a zod schema to validate the folder name:

const FormSchema = z.object({
  name: z
    .string()
    .min(1, { message: t('common.namePlaceholder') })
    .trim(),
});

Internal State and Hooks

Methods

onSubmit

async function onSubmit(data: z.infer<typeof FormSchema>)

Render Output

The component renders a form UI composed of custom UI components:

The form uses localized placeholders and labels, and disables browser autocomplete on the input field.


Implementation Details and Algorithms


Interaction with Other System Components


Usage Example

import { CreateFolderForm } from './create-folder-form';

function FolderModal({ close, createFolder }) {
  return (
    <Modal isOpen onClose={close}>
      <CreateFolderForm
        hideModal={close}
        onOk={async (name) => {
          const success = await createFolder(name);
          return success;
        }}
      />
    </Modal>
  );
}

Mermaid Component Diagram

componentDiagram
    component CreateFolderForm {
        +hideModal?: () => void
        +onOk?: (name: string) => Promise<boolean | void> | boolean | void
        -FormSchema: ZodSchema
        -form: UseFormReturn
        -onSubmit(data): Promise<void>
    }
    component Form {
        +Provides form context
    }
    component FormField {
        +Connects form control to input
    }
    component FormItem {
        +Layout wrapper
    }
    component FormLabel {
        +Displays label text
    }
    component FormControl {
        +Wraps input element
    }
    component Input {
        +Text input for folder name
    }
    component FormMessage {
        +Shows validation error message
    }
    CreateFolderForm --> Form
    Form --> FormField
    FormField --> FormItem
    FormItem --> FormLabel
    FormItem --> FormControl
    FormControl --> Input
    FormItem --> FormMessage

Summary

create-folder-form.tsx is a well-structured React component providing a localized, validated folder creation form. It integrates modern React form libraries and project UI components to deliver a clean user experience. Its design allows easy integration into modal dialogs and asynchronous workflows, making it a reusable and flexible part of the UI for managing folder creation.