📋

Typesafe Form Inputs with React Hook Forms

May 17, 2025

Lecture on TypeSafe Form Inputs in React Hook Forms

Introduction

  • Presenter: Dave
  • Focus: Creating typesafe form inputs with client-side validation and immediate feedback in React Hook Forms.
  • Resources & Community:
    • Links to resources and Discord server in the description.
    • Acknowledgment to Sentry for supporting the video series.
  • Tech Stack: Same as used in Dave's professional project; includes Next.js and React.

Setting Up Typesafe Form Inputs

  1. Previous Lesson Recap: Schemas and Default Values
  2. Current Lesson Goals:
    • Create form inputs with validation and feedback.
    • Utilize Shad CN components for input fields.

Tools & Libraries

  • Sentry:
    • Debugging, error reporting, logging, and performance tracking.
    • Offers free trials with promo code 'Dave gray'.
  • Development Environment:
    • VS Code, Next.js 15, React 19.

Project Walkthrough: Repair Shop App

  • User Stories:
    • Story 14 & 15: Users can create/view tickets and customers.
  • Form Inputs Setup:
    • Utilizing Shad CN components (input, select, textarea, checkbox).

Installation Steps

  1. Install Shad CN Components:
    • Use npx shad cn@latest add input select textarea checkbox.
  2. Run Project:
    • Use npm run dev to start the Next.js project.

React Hook Forms with Shad CN

  • Form Components Creation:
    • Use form context from React Hook Form.
    • Import necessary components from Shad CN.
    • Create reusable form input components to avoid clutter.

Example: Input Component

  • Creation of inputWithLabel.tsx:
    • Use useFormContext to reference the form.
    • Import from Shad CN (form control, field, item, label, message, and input).
    • Define props including typesafe schema matching.

Form Layout with Flexbox

  • Columns Setup:
    • Use Flexbox for responsive design.
    • Form divided into multiple columns.

TextArea Component

  • Creation of textareaWithLabel.tsx:
    • Similar to input, with textarea-specific adjustments.

Select Input

  • Manage States and Territories:
    • Create a constants file to manage state data.
    • Use select input for state selection in forms.

Checkbox Input

  • Creation of checkboxWithLabel.tsx:
    • Incorporate checkbox component from Shad CN.
    • Use for Boolean fields like 'Completed'.

Putting It All Together

  • Customer and Ticket Forms:
    • Integrate all created components into forms.
    • Validation provides immediate feedback for required fields.

Conclusion

  • Summary: Successfully created a typesafe and interactive form with client-side validation.
  • Next Steps: Implement permissions for assigning techs based on user roles.
  • Community Support:
    • Special thanks to patrons.
    • Encourage progress over perfection in coding.
    • Engage with the community for shared learning.