0.0.3 • Published 3 years ago

ng-json-form-bootstrap v0.0.3

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

Bootstrap Json Form


Instal

Install the package

npm:

npm i ngx-json-form-bootstrap

Import it in your module file

import { NgJsonFormModule} from 'ng-json-form';

@NgModule({
  declarations: [ ... ],
  imports: [ 
    NgJsonFormModule, /* <--inside of imports */
    ...
  ]
})

Usage

All you need to do is provide an object for each Field in your form, following the interface below:

In your component, declare the questions Object:

interface Question {
  [field:string]: Object<ControlType>
  
}

Example 1: Single text field

In your component, declare the questions Object:

/* Minimum setup */
public questions = {
  "favorite-color": { /* Question Key */
    "label": "Favorite color?", /* <param-name> :  <param-value> */
  }
}

constructor(){}

// Handle the form value
onFormValueChanged(formValue) {
    console.log(formValue)
}

In your template.html add the component and bind the questions and the output to an method

<ng-json-form 
  [questions]="questions" 
  (valueChanged)="handleChanges($event)" >
</ng-json-form>

And you've got:

Example 2: Complex form

In your component, declare the questions Object:

public questions = {
  textboxExample: {
    key: 'textboxExample',
    controlType: 'textbox',
    label: 'Textbox Question',
    helpText: 'Please provide all information',
    required: false,
    order: 1,
    disabled: true,
    size: 12,
    type: 'text',
    value: 'The house of sunshine',
    
  },
  rangeExample: {
    key: 'rangeExample',
    controlType: 'range',
    label: 'Range question',
    step: 1,
    min: 50,
    max: 100,
    value: 25,
    order: 2,
    required: true,
    helpText: 'Please provide all information',
    readonly: false,
    disabled: false,
    size: 12,
    placeholder: ''
  },
  DropdownExample: {
    key: 'DropdownExample',
    controlType: 'dropdown',
    label: 'Dropdown question',
    required: true,
    order: 4,
    trackKey: 'id',
    viewValue: 'label',
    helpText: 'Please provide all information',
    value: {
      label: 'Apartament',
      id: 1
    },
    options: [
      {
        label: 'Apartament',
        id: 1
      },
      {
        label: 'House',
        id: 2
      },
      {
        label: 'Flat',
        id: 3
      }
    ]
  },
  RadioExample: {
    key: 'RadioExample',
    controlType: 'radio',
    label: 'Radio Question',
    required: true,
    order: 4,
    trackKey: 'id',
    viewValue: 'label',
    value: 2,
    options: [
      {
        label: 'radio option 01',
        id: 1
      },
      {
        label: 'radio option 02',
        id: 2
      },
      {
        label: 'radio option 03',
        id: 3
      }
    ],
    readonly: false,
    disabled: false,
    size: null,
    helpText: '',
    placeholder: ''
  },
  CheckBoxExampleA: {
    key: 'CheckBoxExampleA',
    controlType: 'checkbox',
    label: 'Checkbox item A',
    required: true,
    order: '8',
    value: null,
    readonly: false,
    disabled: false,
    size: null,
    helpText: '',
    placeholder: ''
  },
  CheckBoxExampleB: {
    key: 'CheckBoxExampleB',
    controlType: 'checkbox',
    label: 'Checkbox item B',
    required: true,
    order: '10',
    value: null,
    readonly: false,
    disabled: false,
    size: null,
    helpText: '',
    placeholder: ''
  },
  TextAreaExample: {
    key: 'TextAreaExample',
    controlType: 'textarea',
    label: 'Textarea question',
    rows: 3,
    order: 8,
    required: true,
    minLength: 2,
    maxLength: 100,
    readonly: false,
    disabled: false,
    size: 12,
    helpText: 'Hello, leave a cool message',
    placeholder: 'My Placeholder'
  }
}

constructor(){}

// Handle the form value
onFormValueChanged(formValue) {
    console.log(formValue)
}

In your template.html add the component and bind the questions and the output to an method

<ng-json-form 
  [questions]="questions" 
  (valueChanged)="handleChanges($event)" >
</ng-json-form>

And you've got:

IMPORTANT: The Questions Object is Immutable, the component it'self doesn't change the object, instead it emmits a new value from the output binding everytime the form changes

Input Types

All inputs extends Base Configuration

Textbox

PropertyTypeDescription
typestring | TypeDefine the input type. Default: textbox
minLengthnumberMin length of the field. Default null
maxLengthnumberMax length of the field. Default null
patternRegExp | stringPattern of the field. Default null

Textarea

PropertyTypeDescription
minLengthnumberMin length of the field. Default null
maxLengthnumberMax length of the field. Default null
rowsnumberNumber of rows. Default null

Range

PropertyTypeDescription
stepnumberDistance between each step. Default 1
minnumberMin value of the scale. Default 0
maxnumberMaximum value of the scale. Default 10
iconStartstringIcon class of the icon in the beggining of the scale. Default: fa fa-plus
iconEndstringIcon class of the icon in the ending of the scale. Default: fa fa-minus

Radio

PropertyTypeDescription
optionsArray\<Object>Options available to be chosen. Default []
trackKeystringWhick key of option list should be used as value. Default id
viewValuestringWhick key of option list should be used as label. Default label
valuestring | numberValue. Default: ''

Dropdown

PropertyTypeDescription
optionsArray\<Object>Options available to be chosen. Default []
trackKeystringWhick key of option list should be used as value. Default id
viewValuestringWhick key of option list should be used as label. Default label
valueObjectValue. Default: ''

Checkbox

PropertyTypeDescription
trackKeystringWhick key of option list should be used as value. Default id
viewValuestringWhick key of option list should be used as label. Default label
valuebooleanValue. Default: false

Base input default configuration

PropertyTypeDescription
controlTypeControlTypeDefine which input will be rendered: Options available: "checkbox", "radio", "dropdown", "range", "textarea", "textbox", "list" Default: textbox
disabledbooleanDisabled status of the field Default: false
helpTextstringShort text describing the field, appears below the field and below above the errors messages Default: ''
keystringUnique ID, Default: Object key or xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
labelstringLabel of the input, shown on top of the field Default: ''
ordernumberDefine which position in the order the field should be displayed Default: 1
placeholderstringValue of the field placeholder, shown when there is no value in the field. Default: label value
readonlybooleanDefine if the field is text only or not; Default: false;
requiredbooleanDefine if the field is mandatory or not; Default: false;
sizenumberValue of bootstrap grid, from 1(8.5%) to 12 (100%) Default: null
valuestring | numberValue of the field Default: null

Enums

ControlType
Values
text
password
email
number
search
tel
file
image
url
Type
Values
checkbox
radio
dropdown
range
textarea
textbox
list

NgDynamicForm

This project was generated with Angular CLI version 10.1.1.