1.0.2 • Published 4 years ago

formik-dynamic-forms v1.0.2

Weekly downloads
11
License
Ollaek
Repository
github
Last release
4 years ago

formik-dynamic-forms

A React Library for creating dynamic forms using Formik, Yup validation, Bootstrap

NPM JavaScript Style Guide

Install

npm install --save formik-dynamic-forms

Usage

import  React,  {  Component  }  from  'react'

  

import  {  DynamicForm  }  from  'formik-dynamic-forms'

import  'formik-dynamic-forms/dist/index.css'

  

const  Example  =  ()  =>  {

return  <DynamicForm />

}

Porps

nametypedescription
schemaYup schema
feildsList of ObjectsObjects properties in the section below
submitObjectall inputs default values
initialValuesObjectall inputs default values
buttonTextStringthe submittion button value

Field Object

nametypedescription
tagString (optional)the field tag (ex, input, select.., etc). by default if not provided it is Input Tag
labelString
nameStringfield name which is the same as yup property, needed for validation
typeStringtext, email , password ...., etc
placeholderString
optionsList Of Objectsdropdown list options, object props (value: string, text: string)

Supported Fields Types

Input select datePicker

Example (Signup Form)

import React from 'react'

import { DynamicForm } from 'formik-dynamic-forms'
import * as Yup from 'yup'
import 'formik-dynamic-forms/dist/index.css'

const App = () => {
  const signupValidationSchema = Yup.object().shape({
    name: Yup.string()
      .max(40, 'Please enter no more than 40 characters')
      .required('Please enter your first name'),
    email: Yup.string()
      .email('Please enter a valid email')
      .required('Please enter an email'),
    password: Yup.string()
      .required('Please enter a password')
      .matches(
        /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/,
        'Please enter password that consist of at least 8 characters, number, special character, capital and small letters'
      ),
    passwordConfirm: Yup.string()
      .oneOf([Yup.ref('password'), null], 'Passwords must match')
      .required('Password confirm is required'),
    birthDate: Yup.date().required('Birthday is required')
  })

  const fields = [
    {
      label: 'Username',
      name: 'name',
      type: 'test',
      placeholder: 'please enter your name'
    },
    {
      label: 'E-mail',
      name: 'email',
      type: 'email',
      placeholder: 'please enter your email'
    },
    {
      label: 'Password',
      name: 'password',
      type: 'password',
      placeholder: '********'
    },
    {
      label: 'Confirm Password',
      name: 'passwordConfirm',
      type: 'password',
      placeholder: '********'
    },
    {
      tag: 'dropdown',
      label: 'Gender',
      name: 'gender',
      options: [
        {
          value: 'M',
          text: 'Male'
        },
        {
          value: 'F',
          text: 'Female'
        }
      ]
    },
     {
       tag: 'datepicker',
       label: 'Birth Date',
       name: 'birthDate',
       placeholder: 'pick a date'
     }
  ]

  const onSubmit = (values) => {
    console.log(values)
  }

  const initial = {
    name: '',
    email: '',
    password: '',
    passwordConfirm: '',
    birthDate: ''
  }

  return (
    <DynamicForm
      schema={signupValidationSchema}
      submit={onSubmit}
      feilds={fields}
      initialValues={initial}
      buttonText='SignUp'
    />
  )
}

export default App

License

Ollaek © ollaek

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago