Skip to main content

Form Components

Tienda ETCA includes three main form components for user interaction and product management. These forms handle contact submissions, product creation, and product editing.

Overview

Formulario

Contact form for user inquiries

FormularioProducto

Add new products to catalog

FormularioEdicion

Edit existing product details

Formulario (Contact Form)

A simple contact form for user inquiries and messages. File: src/components/Formulario.jsx

Overview

The contact form collects basic user information including name, email, and message. It uses Bootstrap styling for a clean, responsive design.

Component Structure

Formulario.jsx

Features

Three input fields arranged in a responsive grid:
  • Nombre - User’s name (text input, required)
  • Correo - Email address (email input, required)
  • Mensaje - User’s message (text input, required)
All fields use Bootstrap’s form-control class for consistent styling.
Uses React useState for controlled form inputs:
Each field’s value is controlled by state and updates on change.
Handles form submission with basic alert:
Currently displays an alert. In production, this should send data to a backend API.
Uses HTML5 validation:
  • All fields are required
  • Email field uses type="email" for format validation
  • Browser-native validation messages

Layout

The form uses Bootstrap’s grid system:
  • Three columns on medium screens and above (col-md-4)
  • Stacks vertically on smaller screens
  • Submit button aligned to the right

Usage Example

In Contacto Layout

FormularioProducto (Add Product Form)

Modal form for adding new products to the catalog with validation. File: src/components/FormularioProducto.jsx

Overview

A modal-based form that allows administrators to add new products. Includes comprehensive validation and error handling.

Component Structure

FormularioProducto.jsx

Props

function
required
Callback function to handle product creation. Receives the product object as a parameter.
function
required
Callback function to close the modal.

Features

Three fields for new product data:
Custom validation with error messages:
Validation Rules:
  • Name cannot be empty
  • Price must be greater than 0
  • Description must be at least 10 characters
Dynamic error feedback:
  • Error messages appear below invalid fields
  • Uses Bootstrap’s is-invalid class for styling
  • Real-time validation on submit
  • Clear error messages in Spanish

State Management

Usage Example

In Admin Layout

FormularioEdicion (Edit Product Form)

Modal form for editing existing product information. File: src/components/FormularioEdicion.jsx

Overview

A comprehensive form for editing product details, including all product attributes. Displayed in a modal overlay.

Component Structure

FormularioEdicion.jsx (excerpt)

Props

object
required
Product object to edit. Contains all product properties:
function
required
Callback function to handle product update. Receives updated product object.
function
required
Callback function to close the modal.

Form Fields

Basic Product Information
  • ID - Read-only product identifier
  • Nombre - Product name (text, required)
  • Precio - Product price (number, required, min: 0)
  • Stock - Available stock (number, required)

Complete Field List

Features

Uses useEffect to update form when selected product changes:
This ensures the form always displays the latest product data.
All form fields are controlled components:
Default empty strings prevent uncontrolled input warnings.
Handles update and closes modal:

Usage Example

In Admin Layout

Styling

All form components use Bootstrap for base styling, with custom CSS for modal behavior.

CSS Files

Form Validation Summary

Formulario

HTML5 Validation
  • Required fields
  • Email format
  • Browser-native messages

FormularioProducto

Custom Validation
  • Name: non-empty
  • Price: greater than 0
  • Description: min 10 chars
  • Spanish error messages

FormularioEdicion

Required Fields
  • All fields required except descripción
  • Number validation for precio/stock
  • No custom validation messages

Best Practices

Form SubmissionAll forms use e.preventDefault() to prevent default browser form submission. This allows for custom handling of form data.
Controlled ComponentsAll form inputs are controlled components, meaning their values are managed by React state. This provides better control and validation capabilities.
Production Considerations
  • Formulario currently uses alert() for feedback - implement proper backend integration
  • Add loading states during form submission
  • Implement proper error handling for API failures
  • Consider adding CSRF protection for production

Components Overview

See all available components

Admin Context

Learn about product management state