1.1.1 • Published 1 year ago

forms-creator v1.1.1

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

Forms Creator

A React component for creating dynamic forms with validation using Zod schemas.

Installation

You can install the forms-creator package via npm:
npm install forms-creator
or yarn
yarn add forms-creator

Usage

Importing the Component

import FormCreator from 'forms-creator'

Creating Form Fields

Consider organizing your form field definitions in a forms folder. Each form can have its own file containing the field definitions.

For example, you can create a LoginForm.ts file inside the forms folder:

// forms/LoginForm.ts
import { z } from 'zod'
import { FieldObject } from 'forms-creator/types'

export const loginFormFields: FieldObject[] = [
{
name: "username",
value: "",
type: "text",
label: "Username",
validationSchema: z.string().min(3, "Username must be at least 3 characters long"),
isRequired: true,
errorClassName: "error",
},
{
name: "password",
value: "",
type: "password",
label: "Password",
validationSchema: z.string().min(6, "Password must be at least 6 characters long"),
isRequired: true,
errorClassName: "error",
},
]

Using the FormCreator Component

import { loginFormFields } from './forms/LoginForm'

// Inside your component
<FormCreator className='form' fields={loginFormFields} onSubmit={handleSubmit} submitText='Login' />

Handling Form Submission

import {Typeofy} from "forms-creator"

const handleSubmit = (formData:Typeofy(loginFormFields)) => {
// Handle form submission here
console.log('Form data:', formData)
}

Props

PropTypeDescription
classNameStringCSS class name for styling the form itself.
fieldsArrayAn array of FieldObject instances representing form fields.
onSubmitFunctionA function to handle form submission. It receives the form data as an argument.
submitTextString (optional)Text to display on the submit button (default is "Submit").
submitClassNameString (optional)CSS class name for styling the submit button.

Field Options

OptionTypeDescription
nameStringThe name of the form field.
valueString | Number | BooleanThe initial value of the form field.
typeStringThe type of the form field (text - number - textarea - checkbox - file).
classNameString (optional)CSS class name for styling the input field.
parentClassNameString (optional)CSS class name for styling the parent div of the input field.
childReact.ReactNode (optional)React node for rendering additional content inside the parent div of the input field.
customInputCustom React Component (optional)Custom React component for rendering the input field.
validationSchemaZod Schema (optional)Zod schema for validating the input field value.
labelString (optional)Label text for the input field.
isRequiredBoolean (optional)Boolean indicating whether the input field is required.
errorClassNameString (optional)CSS class name for styling the error message.
1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago