Gathering detailed insights and metrics for @react-formgen/core
Gathering detailed insights and metrics for @react-formgen/core
Gathering detailed insights and metrics for @react-formgen/core
Gathering detailed insights and metrics for @react-formgen/core
Headless, unopinionated, and flexible form generation.
npm install @react-formgen/core
Typescript
Module System
Node Version
NPM Version
TypeScript (98.46%)
JavaScript (0.83%)
Shell (0.38%)
HTML (0.22%)
CSS (0.11%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
52 Stars
83 Commits
1 Forks
3 Watchers
4 Branches
1 Contributors
Updated on Jul 09, 2025
Latest Version
0.0.0-alpha.27
Package Id
@react-formgen/core@0.0.0-alpha.27
Unpacked Size
67.94 kB
Size
16.94 kB
File Count
6
NPM Version
10.2.4
Node Version
21.6.0
Published on
Jul 03, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
20
A foundational library designed to facilitate the creation of schema-based form generators. It provides core functionality for state management, form data manipulation, and error handling, allowing developers to build form generators that can be extended for various schema types.
Install the package:
1npm install @react-formgen/core 2# or 3yarn add @react-formgen/core 4# or 5pnpm add @react-formgen/core
To create a schema-specific form provider and hooks, use the createFormProviderAndHooks
factory function. This function requires a generateInitialData
function for initializing form data and a getErrorsAtPath
function for retrieving errors at specific paths.
1import { createFormProviderAndHooks } from "@react-formgen/core"; 2 3// Example: Initial data generator function 4const generateInitialData = (schema) => { 5 // Logic to generate initial data based on the schema 6}; 7 8// Example: Function to get errors at a specific path 9const getErrorsAtPath = (errors, path) => { 10 // Logic to get errors at the specified path 11}; 12 13// Create schema-specific form provider and hooks 14const { 15 FormProvider, 16 useFormContext, 17 useFormDataAtPath, 18 useErrorsAtPath, 19 useArrayTemplate, 20 useTemplates, 21 useRenderTemplate, 22 Form, 23} = createFormProviderAndHooks<SomeSchemaType, SomeErrorType>( 24 createInitialData, 25 getErrorsAtPath, 26 DefaultRenderTemplate, 27 BaseFormRoot, 28 BaseTemplates 29);
Wrap your form components with the Form
or FormProvider
to provide the form state context.
1import React from "react"; 2import { Form, FormProvider } from "./your-custom-formgen-library"; 3import MyFormRoot from "./MyFormRoot"; 4 5const MyForm = () => { 6 const schema = {}; // Define your schema 7 const initialData = {}; // Define initial data if any 8 const handleSubmit = (data) => { 9 console.log("Form submitted with data:", data); 10 }; 11 const handleErrors = (errors) => { 12 console.error("Form submission errors:", errors); 13 }; 14 15 return ( 16 <Form 17 schema={schema} 18 initialData={initialData} 19 onSubmit={handleSubmit} 20 onError={handleErrors} 21 /> 22 23 // or 24 25 <FormProvider schema={schema} initialData={initialData}> 26 <MyFormRoot 27 onSubmit={handleSubmit} 28 onError={handleErrors} 29 /> 30 </FormProvider> 31 ); 32}; 33 34export default MyForm;
Use the provided hooks to manage form state and handle errors.
1import React from "react"; 2import { 3 useFormContext, 4 useFormDataAtPath, 5 useErrorsAtPath, 6} from "./your-custom-schema-form-hooks"; 7 8const MyField = () => { 9 const formData = useFormContext((state) => state.formData); 10 const [value, setValue] = useFormDataAtPath(["path", "to", "field"]); 11 const errors = useErrorsAtPath(["path", "to", "field"]); 12 13 return ( 14 <div> 15 <input value={value} onChange={(e) => setValue(e.target.value)} /> 16 {errors && 17 errors.map((error, index) => ( 18 <div key={index} style={{ color: "red" }}> 19 {error.message} 20 </div> 21 ))} 22 </div> 23 ); 24}; 25 26export default MyField;
createFormProviderAndHooks(generateInitialData, getErrorsAtPath, BaseRenderTemplate, BaseFormRoot?, BaseTemplates?)
Creates schema-specific form provider and hooks.
generateInitialData(schema)
: Function to generate initial form data from the schema.getErrorsAtPath(errors, path)
: Function to get errors at a specific path.BaseRenderTemplate
: Component for rendering form fields.BaseFormRoot
: (Optional) Root form component for managing submission.BaseTemplates
: (Optional) Object mapping template names to React components.Returns an object containing:
FormProvider
: A React component to provide the form state context.useFormContext
: Hook to access the form context.useFormDataAtPath
: Hook to get and set form data at a specific path.useErrorsAtPath
: Hook to get errors at a specific path.useArrayTemplate
: Hook for array manipulation (add, remove, move items).useTemplates
: Hook to access the templates.useRenderTemplate
: Hook to access the render template component.Form
: A generalized Form component for rendering forms.FormState<S, E>
Represents the state of the form.
schema
: The schema of the form.formData
: The current form data.errors
: The current form errors.readonly
: Whether the form is in read-only mode.setFormData(path: string[], value: any)
: Function to set form data at a specific path.setErrors(errors: E[] | null)
: Function to set form errors.setReadonly(readonly: boolean)
: Function to set the read-only state.FormProviderProps<S>
Props for the FormProvider
component.
initialData?
: Initial form data.schema
: The schema of the form.children
: The child components.createInitialData(schema: S)
: Function to create initial data from the schema.templates?
: An object mapping template names to React components.readonly?
: Whether the form should be initially read-only.renderTemplate?
: A React component for rendering form fields.RenderTemplateProps<S>
Props for the render template component.
schema
: The schema of the form.path
: The path to the current form data.useFormDataAtPath(path: string[], defaultOnNull: unknown)
Hook to get and set form data at a specific path.
path
: Path to the form data.defaultOnNull
: Value to return if the form data at the path is null.useErrorsAtPath(path: string[])
Hook to retrieve validation errors at a specific path.
useArrayTemplate(path: string[], zeroState: () => any, defaultOnNull: any)
Hook to manage array properties within the form state.
useTemplates()
Hook to access the templates from the context.
useRenderTemplate()
Hook to access the render template component from the context.
Contributions are welcome. Please open an issue or submit a pull request on the GitHub repository.
This project is licensed under the MIT License. See the LICENSE file for details.
No vulnerabilities found.
No security vulnerabilities found.