2.7.9 • Published 3 months ago

sh-dynamic-form-builder v2.7.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

✔️ Version 2.7.0

  • Customizing form array title by setting titleClass property
  • Ability to define as many form group with as many fields you want for each
  • Setting full with buttons for mobile and another device size by setting fullWidthButtons property

✔️ Version 2.6.0

  • Sizing for control and label by values: small,medium and large.Default is medium
  • Red color added to color of buttons
  • Simple validation error style has added that shows red text without background color.You can reached it by setting simpleValidationError:true

Some Features

  • Ability to define form fields by type
  • Ability to define as many form arrays with as many fields you want for each
  • Ability to define as many form group with as many fields you want for each
  • Has a config for Add button of form array
  • Ability to define the as many validation rules and error message of the field and the form array fields as desired
  • Has a config for form array divider
  • Ability to define Send, Reset and Cancel buttons with the desired title, color, and order
  • Binding form by object in edit mode
  • Bootstrap style

Field types

  • Text
  • Range
  • Number
  • Password
  • Color
  • Dropdown
  • Radio
  • Checkbox
  • File
  • Date
  • Datetime local
  • Time
  • Week
  • Month
  • Form array
  • Form group

Table of contents

Install

npm i sh-dynamic-form-builder

Install dependencies

npm install bootstrap --save
npm install bootstrap-icons --save
npm install sh-ordinal-numbers --save

Quick start

import { DynamicFormBuilderModule } from "sh-dynamic-form-builder";

@NgModule({
  imports: [DynamicFormBuilderModule],
})
export class AppModule {}

Import style and icon

In angular.json

"styles": [
    "src/styles.scss",
    "./node_modules/bootstrap/dist/css/bootstrap.min.css"
    "./node_modules/bootstrap-icons/font/bootstrap-icons.scss"
]

If Bootstrap icons don't work, you should import them in global style.

@import "~bootstrap-icons/font/bootstrap-icons";

How to use?

In component

formGroup = new FormGroup({});
isSubmit: boolean = false;
config: IFormConfig = {
    title: 'Profile',
    fields: [
      {
        type: 'text', name: 'fullName', label: 'Full name', id: 'fullName',
        validators: [
          { rule: 'required', msg: 'Full name is required' },
          { rule: 'minlength', value: 6, msg: 'Full name must be at least 6 characters long' },

        ],
      },
      {
        type: 'form-array',
        label: 'Tasks',
        name: 'tasks',
        formArray: {
          simpleAddButton: true,
          addButtonBGColor: 'green',
          fields: [
            {
              type: 'text', name: 'title', label: 'title', validators: [
                { rule: 'required', msg: 'title is required!' },
                { rule: 'maxlength', value: 50, msg: 'Maximum length should be 50 characters' }
              ]
            },
            {
              type: 'date', name: 'dueDate', label: 'due date', validators: [
                { rule: 'required', msg: 'date is required!' }
              ]
            }
          ]
        }
      },
      {
        type: 'text', name: 'email1', label: 'E-mail',
        validators: [
          { rule: 'email', msg: 'E-mail is wrong' }
        ],
      },
      {
        type: 'file', name: 'avatar', label: 'Avatar',
        onUpload: this.onUpload.bind(this), multipleFile: true
      }
      ...
        buttonSetting: {
      fullWidthButtons: 'only-mobile',
      buttons:
        [
          { type: 'submit', caption: 'Submit', bgColor: 'blue' },
          { type: 'reset', caption: 'Reset', bgColor: 'orange' },
          { type: 'cancel', caption: 'Cancel', bgColor: 'light' }
        ]
    }
}
private onUpload(event: any): void {
    const files = event.files;
    ...
}
 public submit(event: FormGroup): void {
    this.isSubmit = true;
    this.formGroup = event;
    /**Sending data ... */
    this.isSubmit = false;
 }
 public cancel(): void {
    /** */
 }

In template

<div class="container">
  <sh-dynamic-form-builder
    [config]="config"
    [isSubmit]="isSubmit"
    (submit)="submit($event)"
  >
  </sh-dynamic-form-builder>
</div>

Input

NameTypeDefaultRequired?Description
configIFormConfig-yesForm config
formValueobject{}-Adjust form values
isSubmitbooleanfalsenoSet in to your Submit Function. If it sets "true", Submit button will be disabled.When submit is clicked it must be set "true" value, when sending data will be ended it must be set "false" value
  • Config

NameTypeDefaultRequired?Description
fieldsIMainFieldItem-yesForm fields
titlestring-noForm title
titleClassstring-noSet style for form title, you can add one or more class name split by space. for example:'class1 class2 class3'
buttonSettingIButtonSetting{}yesButton setting
formControlConfigIFormControlConfig{}yesForm controls configuration
  • Form control config

NameTypeDefaultRequired?Description
elementSizestringmediumnoControl size and label size
simpleValidationErrorbooleanfalsenoRed text without background color as the validation error message

Output

NameTypeDescription
(submit)FormGroupOutput formGroup, when Submit button clicked
(cancel)booleanWhen the Cancel button has been clicked

Field properties and methods

NameTypeDefaultRequired?Description
typestring-yesAllowed values: text, range, number, password, color, dropdown, radio, checkbox, file, date, datetime-local, time, week, month, form-group and form-array
namestring-yesForm control name
defaultValueany-noForm control default value
placeholderstring-noUsable for text, number, password, dropdown and email
[validators]IValidationRules-noSet validator rules and error messages
idstring-noElement id
widthnumber100%noWidth of Control
disablebooleanfalsenoDisable control
  • File

    NameTypeDefaultRequired?Description
    onUploadvoid-yesFile selection event.
    multipleFilebooleanfalsenoAllow choice multiple file
  • Text

    NameTypeDefaultRequired?Description
    multilinebooleanfalsenoMultiline handling
    rowCountnumber5noSpecifies the visible height of a text area, in lines
  • Radio

    NameTypeDefaultRequired?Description
    optionsIOption-yesDefine options: {key: string;label: string;}
    inlinebooleanfalsenoShow on the same horizontal row
  • dropdown

    NameTypeDefaultRequired?Description
    optionsIOption-yesDefine options: {key: string;label: string;}
  • Range

    NameTypeDefaultRequired?Description
    rangeMinnumber0novalue of the min
    rangeMaxnumber100novalue of the max
    rangeStepnumber1novalue of the step
    • Form-group

    NameTypeDefaultRequired?Description
    formGroupIFormGroup-noSet form group fields and validations
    • FormGroup

    NameTypeDefaultRequired?Description
    fieldsIFormGroupFieldItem-yesForm group fields
    titleClassstring-noSet style for form group title, you can add one or more class name split by space. for example:'class1 class2 class3'
    showInCardbooleanfalsenoShow in bootstrap card
  • Form-array

    NameTypeDefaultRequired?Description
    formArrayIFormArray-noSet form array fields and validations
    • FormArray

      NameTypeDefaultRequired?Description
      fieldsIFormArrayFieldItem-yesForm array fields
      simpleAddButtonbooleanfalsenoOnly icon on button, without text
      addButtonBGColorstringgraynoAllowed values: gray, green, blue, red, orange, light and dark
      dividerIDivider-nodoesn't show an ordinal number in the center of the divider line
      • Divider

        NameTypeDefaultRequired?Description
        suppressbooleanfalsenoDoesn't show divider
        lineClassstring-noSet style for divider line, you can add one or more class name split by space. for example:'class1 class2 class3'
        ordinalNumberClassstring-noSet style for ordinal number between divider line, you can add one or more class name split by space. for example:'class1 class2 class3'
        suppressOrdinalNumberbooleanfalsenoDoesn't show an ordinal number in the center of the divider line
        heightnumber3noThe height of divider

Button setting

NameTypeDefaultRequired?Description
[buttons]IButton[]yesThe array of buttons, order by definition
buttonsAlignstringleftnoUsable when fullWidthButtons is false
fullWidthButtonsstringnonenoAllowed values: none, all, only-mobile
  • Button properties

NameTypeRequired?Description
typestringyesAllowed values: submit, cancel and reset
captionstringyesButton caption
bgColorstringyesAllowed values: gray, green, blue, red, orange, light and dark

Validators

nametypeRequiredDescription
rulestringyesDefine rules
msgstringyesError message
valueanynoUsable for min, max,pattern,minlength and maxlength rules
  • Validator rules

NameTypeDescription
minnumberValidator that requires the control's value to be greater than or equal to the provided number.
maxnumberValidator that requires the control's value to be less than or equal to the provided number.
emailbooleanValidator that requires the control's value pass an email validation test.
patternstringValidator that requires the control's value to match a regex pattern.
requiredbooleanValidator that requires the control have a non-empty value.
requiredTruebooleanValidator that requires the control's value be true. This validator is commonly used for required checkboxes.
minLengthnumberValidator that requires the length of the control's value to be greater than or equal to the provided minimum length
maxLengthnumberValidator that requires the length of the control's value to be less than or equal to the provided maximum length.

Creator

Hakime sheikhalishahi

License

MIT