# react-form-generator **Repository Path**: wangrupeng/react-form-generator ## Basic Information - **Project Name**: react-form-generator - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-10-17 - **Last Updated**: 2021-10-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [npm-stats]: https://nodei.co/npm/form-generator-react.png?compact=true [npm-url]: https://www.npmjs.org/package/form-generator-react # react-form-generator [![NPM version][npm-stats]][npm-url] Generate, validate, and parse React forms based on arbitrary JSON schemas. ### Installation `npm install --save form-generator-react` Or download from this repo and use globally (FormGenerator) or with RequireJS ### Demo To run the demo app, navigate to `/demo` and run: ``` npm install npm start open http://localhost:3000 ``` ### Usage First, instantiate a FormGenerator instance as follows: ```js const formGenerator = new FormGenerator(); ``` - `inputsMap` is an object mapping input component types to input component implementations. E.g. `inputsMap.TextInput` could equal some component that implements the `TextInput` interface (see `src/input-components` for examples of this). Next, to instantiate an actual FormGeneratorForm component, use `formGenerator.create`: ```js const formComponent = formGenerator.create(, , ); ``` - `formSchema` is a JSON schema for the form - `ref` is the ref-string for the FormGeneratorForm - `onSubmit` is the submission callback for the form, whose first parameter is an object in the same form as the initial schema, but with the form data filled in where the form field definitions were previously. ### Form Schema Options A valid form schema is just an object of the form ```js var schema = { fieldName1: , fieldName2: , ... }; ``` Where `FormField` values are objects with metadata describing a field's type, along with additional metadata for things like default values, validation, labels, and the like. #### `FormField` Options A valid `FormField` object must have a `type` attribute (with the exception of hidden fields) and can contain additional metadata. ##### `FormField` `type` Options - `String` - `Number` - `Boolean` - `[String]` - `[Number]` - `[Boolean]` - `[]` - `{ objectField: , ... }` - `[{ objectField: , ... }]` Note how in the last three type examples, we define `FormField`'s recursively as objects or arrays of arbitrarily-nested `FormField`'s, each of which can have all of the same metadata as top-level fields (like default values, validators, etc.). #### Additional `FormField` Options - `enum`: for `Number` and `String` fields, you can add an `enum` attribute that equals an array of allowed values. Adding an `enum` attribute will render the field as a `