A small utility for viewing react-hook-form values in a quick and easy way. This library provides a React component that displays form values, errors, touched fields, dirty fields, and form state in a clean, developer-friendly interface.
- Real-time Form Monitoring: Displays form values, errors, and state as they change.
- React Hook Form Integration: Seamlessly integrates with
react-hook-formfor easy setup. - Portal-based UI: Renders a floating preview panel using React portals for flexible positioning.
- Customizable Styling: Comes with CSS classes for easy styling customization.
- TypeScript Support: Fully typed with TypeScript for a robust development experience.
Install the package using npm or yarn:
npm install rhf-previewor
yarn add rhf-preview- Ensure you have
react,react-dom, andreact-hook-forminstalled in your project. - Wrap your form with the
FormProviderfromreact-hook-form. - Add the
RHFPreviewcomponent to your form to display the preview panel.
import { FormProvider, useForm } from "react-hook-form";
import RHFPreview from "rhf-preview";
function MyForm() {
const methods = useForm({
defaultValues: {
name: "",
email: "",
},
});
return (
<FormProvider {...methods}>
<form>
<input {...methods.register("name")} placeholder="Name" />
<input {...methods.register("email")} placeholder="Email" />
<button type="submit">Submit</button>
</form>
<RHFPreview />
</FormProvider>
);
}
export default MyForm;- The
RHFPreviewcomponent usesuseFormContextfromreact-hook-formto access form state and values. - A button (rendered via a React portal) toggles the visibility of the preview panel.
- The preview panel displays:
- Form Values: All current form values.
- Form State: Key form state properties (excluding certain fields like errors and touched fields).
- Errors: Any validation errors for form fields.
- Dirty Fields: Fields that have been modified.
- Touched Fields: Fields that have been interacted with.
The RHFPreview component does not accept any props directly, as it relies on the FormProvider context from react-hook-form.
The preview panel uses the following CSS classes for styling:
mainContainerStyles: The main container for the preview panel.heading: The title of the preview panel.typeContainer: Containers for each section (e.g., Form Values, Errors).typeContainer__title: Section titles.typeContainer__row: Rows within each section.typeContainer__label: Labels for field names.typeContainer__value: Values for fields.
You can override these styles by providing your own CSS. The styles are defined in global.css within the package.
To contribute or modify the library:
- Clone the repository:
git clone https://github.com/JatinThakur-12/rhf-preview.git- Install dependencies:
npm install- Build the project:
npm run rollupnpm run rollup: Builds the library using Rollup, generating CommonJS, ES modules, and TypeScript declaration files.npm test: Placeholder for tests (not yet implemented).
src/index.tsx: Entry point for theRHFPreviewcomponent.src/preview.tsx: Main preview panel component.src/components/: Contains components for displaying form values, errors, state, etc.src/global.css: Default styles for the preview panel.rollup.config.js: Rollup configuration for building the library.tsconfig.json: TypeScript configuration.
react: ^18.0.0 || ^19.0.0react-dom: ^18.0.0 || ^19.0.0react-hook-form: ^7.0.0
- Rollup plugins for bundling and TypeScript support.
- TypeScript for type checking.
- PostCSS for CSS processing.
This project is licensed under the ISC License. See the package.json for more details.
Jatin Thakur