Skip to content

About

Build forms from JSON configuration for your React project!

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Simple Form Generator

Simple Form Generator

Simple Form Generator is an app for you to generate forms for your apps, based on a specified JSON configuration.

The application consists of three tabs: Config – to add the form configuration in JSON format Result – to display the form Code – to copy the form as a React + TypeScript component or as plain HTML

The Config tab validates the configuration on every change in its editor, CodeMirror from the @uiw/react-codemirror package. It checks both the JSON syntax and the configuration values, and displays any errors below the editor.

The Config tab can show two buttons that save the current configuration to the browser's localStorage or remove it from there. This way, you can easily control which configuration the browser keeps. When you open the app for the first time, neither button is displayed. After a page refresh, the Config tab displays the saved configuration, or the default config example if nothing is saved. Each button appears only when its action is possible:

  • Save to browser - appears when the editor content differs from the saved configuration, or from the default config example if nothing is saved. It saves the content even when it is invalid, so you can come back later and fix the JSON.
  • Remove from browser - appears while a configuration is saved. Clicking it deletes the saved configuration but keeps the editor content. The default config example appears after the next page refresh.

If no error occurs after specifying the configuration, you can click on Result tab to see the result. You can test your forms by entering the values into appropriate fields.

The Result tab validates the form data with the React Hook Form library.

The Code tab has three subtabs:

  • Unstyled - shows the component without the classes of the Result tab.
  • Tailwind CSS - displays the component with the same Tailwind CSS classes as the Result tab. This code requires Tailwind CSS v4.1 or later in your project.
  • HTML - represents the form as plain HTML, without the classes of the Result tab and without event handlers. The browser validates this form on submit.

The generator uses Tailwind CSS and is quite good adapted also for your mobile phone screen.

This is a Next.js project bootstrapped with create-next-app.

JSON format

The JSON configuration object consists of three main keys:

title - represents the main title of the form

items - the actual form fields of various types, such as:

  • number - number field
  • text - text field
  • textarea - text area
  • checkbox - checkbox field
  • date - date field
  • radio - radio button

buttons - form buttons of type: button, submit, reset

The JSON configuration entered in the Config tab is validated by using Zod. You can find the specific schemas defined in the src/schemas directory. Most of the common html attributes for input, textarea and button HTML elements are accepted. Keys are passed to React as they are: value and checked pin the control, while defaultValue and defaultChecked preset it and leave it editable.

Radio groups

A radio item renders one <fieldset>, and its label becomes the legend. Each entry of options is one <input type="radio"> and accepts the same attributes as in HTML, plus a label for its visible text:

{
  "id": "plan",
  "type": "radio",
  "label": "Plan",
  "options": [
    { "value": "basic", "label": "Basic" },
    { "value": "pro", "label": "Pro", "defaultChecked": true },
    { "value": "team", "label": "Team", "disabled": true }
  ]
}

Keys set on the radio item apply to every option, and an option's own key wins. These item keys are exceptions:

  • aria-label and aria-labelledby - are set on the <fieldset>, not on the options.
  • autoFocus - is set only on the first option.
  • "disabled": true - disables every option, even an option with "disabled": false.

The id of an option without id defaults to <id>-<index>. As in HTML, required on any option makes the whole group required.

Event handlers

Fields and buttons accept the onClick, onChange, onFocus, onBlur, onMouseDown, onMouseUp, onKeyDown and onKeyUp keys. Since JSON cannot hold a function, the value is the name of a handler:

{ "text": "Clear", "onClick": "clear" }

The Result tab implements two handlers:

  • reset - asks for a confirmation, then restores the form to the defaults from your config. Every button with "type": "reset" calls it by default, without an onClick key.
  • clear - restores the form right away, with no confirmation. It runs only where a handler key names it.

Both also clear the submitted Form Data output.

Any other handler passes validation but has no implementation in the Result tab, so it does nothing while you are testing the form. Two exceptions: onChange and onBlur on fields are validated, but they never run in the Result tab, because React Hook Form handles those two events.

The Unstyled and Tailwind CSS subtabs of the Code tab generate a handlers object with a stub for every handler name in your config, ready for your own code. reset and clear are already implemented. Field onChange and onBlur handlers are passed in the register options of React Hook Form.

Getting Started

npm install

Run the development server:

npm run dev

Open http://localhost:3000 with your browser to see the result.

This project uses next/font to automatically optimize and load Inter, a Google font.

Unit tests

To run the unit tests, run:

npm test

Further improvements/coming soon:

This project is fresh, hence lots of improvements can be done, for example:

  • extend the Code tab with more output formats to choose from: a React component in plain JavaScript, or a matching CSS stylesheet for the HTML subtab, possibly also using styled components with React
  • allow other input types to be accepted, like type of button, submit, reset, email, password etc.
  • make the data entered in the generated forms persistent while switching between the tabs, even before clicking on Submit button (can be useful when playing with the various data and setting the proper JSON attributes to make the inputs work as expected)
  • improve state management, consider getting rid of the Context eventually
  • enable the React Compiler (reactCompiler: true in next.config.ts + babel-plugin-react-compiler) for automatic memoization
  • simplify CSS, maybe add custom utility classes and use them
  • add detailed documentation and examples for advanced usage

Later:

  • add internationalization (i18n) support for multiple languages
  • implement a drag-and-drop interface for building forms
  • enhance the UI/UX with more themes and customization options
  • provide a library of pre-built form templates

I appreciate your interest and support as I continue to enhance and expand this project. Enjoy using the Simple Form Generator!

About

Build forms from JSON configuration for your React project!

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages