react-redux-form-errors v1.1.0
Hereβs a README.md
for your package:
π¦ react-redux-form-errors
A lightweight, easy-to-use form error handling and navigation library using React and Redux Toolkit. This package helps manage form validation errors efficiently while allowing users to navigate through the errors in a sequential manner.
π Features
β
Centralized error state management with Redux Toolkit
β
Automatic ref management for error fields
β
Smooth scrolling to each error field
β
Error summary display with navigation
β
TypeScript support
β
No external dependencies beyond Redux Toolkit and React
π Installation
Install via npm:
npm install react-redux-form-errors
or using yarn:
yarn add react-redux-form-errors
π Setup
1οΈβ£ Add the reducer to your Redux store:
import { configureStore } from "@reduxjs/toolkit";
import { errorReducer } from "react-redux-form-errors";
export const store = configureStore({
reducer: {
formErrors: errorReducer,
},
});
2οΈβ£ Wrap your app with Redux Provider
:
import React from "react";
import { Provider } from "react-redux";
import { store } from "./store";
import App from "./App";
const Root = () => (
<Provider store={store}>
<App />
</Provider>
);
export default Root;
π Usage
Handling Errors in Input Fields
Use the useErrorHandler
hook to manage errors for individual form fields.
import React from "react";
import { useErrorHandler } from "react-redux-form-errors";
interface InputFieldProps {
name: string;
error?: string;
}
const InputField: React.FC<InputFieldProps> = ({ name, error }) => {
const ref = useErrorHandler(name, !!error, error || "");
return (
<div>
<input ref={ref} name={name} />
{error && <div className="error-text">{error}</div>}
</div>
);
};
export default InputField;
Displaying Error Summary & Navigation
Use the useErrorNavigation
hook to navigate through errors.
import React from "react";
import { useErrorNavigation } from "react-redux-form-errors";
const ErrorSummary = () => {
const { errorCount, scrollToFirst, scrollToNext, currentErrorMessage } =
useErrorNavigation();
if (errorCount === 0) return null;
return (
<div className="error-summary">
<p>Errors: {errorCount}</p>
<p>Current Error Message: {currentErrorMessage}</p>
<button onClick={scrollToFirst}>Jump to first error</button>
<button onClick={scrollToNext}>Next error</button>
</div>
);
};
export default ErrorSummary;
π§ͺ Running Tests
This package includes tests for its hooks. To run the tests, use:
npm run test
or with yarn:
yarn test
π API Reference
useErrorHandler(field: string, hasError: boolean, errorMessage: string): React.RefObject<HTMLInputElement>
Handles error management for an individual form field.
Parameter | Type | Description |
---|---|---|
field | string | The name of the form field |
hasError | boolean | Whether the field has an error |
errorMessage | string | The error message to display |
Example:
const ref = useErrorHandler("email", true, "Email is required");
useErrorNavigation(): { errorCount: number, scrollToFirst: () => void, scrollToNext: () => void }
Manages error navigation within the form.
Property | Type | Description |
---|---|---|
errorCount | number | The total number of errors in the form |
scrollToFirst | () => void | Scrolls to the first error field |
scrollToNext | () => void | Scrolls to the next error field |
currentErrorMessage | string | Current index error message |
Example:
const { errorCount, scrollToFirst, scrollToNext } = useErrorNavigation();
π Peer Dependencies
Ensure the following peer dependencies are installed in your project:
{
"peerDependencies": {
"react": "^18.0.0",
"@reduxjs/toolkit": "^2.0.0"
}
}
π License
This package is licensed under the MIT License.
π‘ Contributing
We welcome contributions! Feel free to submit issues or open a pull request.
π© Support
If you have any issues or questions, feel free to open an issue on GitHub.
This README.md
provides clear installation steps, usage examples, API references, and testing instructions. Let me know if youβd like any modifications! π