1.1.13-rc.2 • Published 1 year ago

@trufla/ngx-tru-forms v1.1.13-rc.2

Weekly downloads
107
License
MIT
Repository
github
Last release
1 year ago

Tru Forms

Angular 8+ module for generating forms from JSON schema. Refer to documentation for structure of JSON Schema PDF/HTML. This component utilizes Reactive Forms for most of its functionality.

Table of Contents

Installation

npm install @trufla/ngx-tru-forms --save

Usage

Module can be used with Angular Material or Bootstrap 4. Import either JsonFormBootstrap4Module or JsonFormMaterialModule or TruUiModule and use it with JsonFormModule. For example:

Example uses Angular Material

app.module.ts

import {
  JsonFormModule,
  JsonFormMaterialModule,
  JsonFormMaterila
} from '@trufla/ngx-tru-forms';
// for using tru ui
// import { JsonFormModule, TruUiModule, JsonFormFieldsService, TruUi } from '@trufla/ngx-tru-forms';

@NgModule({
  imports: [
    JsonFormMaterialModule,
    // for using tru ui
    // TruUiModule
    // for using Bootstrap
    // JsonFormBootstrap4Module
    {
      ngModule: JsonFormModule,
      providers: [
        {
          provide: JsonFormFieldsService,
          useClass: JsonFormMaterial,
          multi: true
        }
        // for using tru ui
        // {
        //   provide: JsonFormFieldsService,
        //   useClass: TruUi,
        //   multi: true
        // }
        // for using bootstrap
        // {
        //   provide: JsonFormBootstrap4,
        //   useClass: TruUi,
        //   multi: true
        // }
      ]
    }
  ]
})

app.component.ts

export class AppComponent {
  schema = {
    "type": "object",
    "properties": {
      "username": {
        "type": "string"
      },
      "password": {
        "type": "string",
        "format": "password"
      }
    },
    "required": ["username", "password"]
  };

  handleSubmit(e) {
    // e = { username: "john_doe", password: "123456789" }
    this.loginService.login(e);
  }
}

app.component.html

<jf-form
  [schema]="schema"
  [submit]="'Login'"
  (handleSubmit)="handleSubmit($event)"
></jf-form>

Options

KeyDescriptionRequired
idUniquely identify forms if there are multiples on the page
schemaJSON Schema objectYes
dataJSON Schema default values
styleExtra classes and style overrides
submitText label for submit button
cancelText label for cancel button
outerClassWrapper class for the form component
submitClassClass for submit button
cancelClassClass for cancel button
isWorkingToggle form state if using async data process
isMultiStepTreat schema as multi step. See example.
viewOnlyRender only the labels and form data. Useful for reports.
disabledDisable all the fields in the form. Set to true for disabled and null otherwise
languageText for translation default is 'en'
(handleSubmit)Watch for form submission. Return JSON Schema response data
(handleChange)Watch for form changes
(handleCancel)Watch for cancel click (emitting value even if form invalid)

For additional ways to modify and access the form see External Methods.

Example

const schema = {
	"type": "object",
	"properties": {
		"first_name": {
			"type": "string"
		},
		"last_name": {
    	"type": "string"
    }
	},
	"required": ["first_name"]
};

const data = {
  "first_name": "Test",
  "last_name": "Me"
}

const onFormSubmit = (form) => console.log(form);
<jf-form 
  [schema]="schema" 
  [data]="data"
  (handleChange)="onFormSubmit($event)"
></jf-form> 

Quick Reference

type: string, number, object, array, boolean
format (optional): date, photo, textarea
description (optional): hover text to describe purpose of field
title: string | array of objects each contains language and value for label or header or array title or object title

"title": [{"language": "en", "value": "first name"}, {"language": "fr", "value": "le prénom"}]

enumNames: array of string or array of arrays contains translation array of objects if not provided enum used as default

// with translation
"enumNames": [[{"language": "en", "value":  "Yes"}, {"language": "fr", "value":  "Qui"}],
              [{"language": "en", "value":  "No"}, {"language": "fr", "value": "Non"}]]
// without translation
"enumNames": ["Yes", "No"]

verify: Boolean default false duplicate string type for verification
maxSize: for photo format to verify size, unit are in MB

Extending

This module allows for extension via injectors.

constructor(
  jfDefaultsService: JsonFormDefaultsService,
  jfValidatorsService: JsonFormValidatorsService
)

Defaults

Extend values in default tag.

this.jfDefaultsService.register('now', () => new Date());

Validations

Add JSON validator.

const ValidatorJSON = (control: AbstractControl) => {
  try {
    JSON.parse(control.value);
    return null;
  } catch (err) {
    return { customError: err.message };
  }
};
this.jfValidatorsService.register('field_name', ValidatorJSON);

field_name has to be valid field including any nesting (as it is display on data and without properties)

const schema = {
	"type": "object",
	"properties": {
		"first_name": {
			"type": "string"
		},
		"last_name": {
    	"type": "string",
    	"prefix": {
    	  "type": "object",
        "properties": {
          "custom": {
            "type": "string"
          }
        }
    	}
    }
	},
	"required": ["make"]
};

Fields would be first_name, last_name, last_name.prefix.custom.

Fields

Add new field type. Create a component that extends CommonComponent. Add the following as a starting template (or copy from string field).

import { CommonComponent } from '@trufla/ngx-tru-forms';

@Component({
  template: `
    <label [ngClass]="['jf-label', schema.key, (isRequired() ? 'required' : '')]">
      {{title()}}  
    </label>
    <input
      class="form-control"
      [name]="schema.key"
      [attr.type]="type()"
      [formControl]="control"
      [(colorPicker)]="color"
      [style.background]="color"
      [style.color]="color"
      [style.width]="'40px'"
      (colorPickerChange)="handleColorPickerChange($event)"
    />
  `
})
export class CustomComponent extends CommonComponent {
  color: '#0000ff';

  handleColorPickerChange(val) {
    this.control.setValue(val);
  }
}

Add it to your module:

entryComponents: [
  ColourPickerComponent
]

Add it via the component:

[fields]="{'colour': ColourPickerComponent}"

Also you can disable Submit button by passing

<jf-form [btnDisabled]="true"></jf-form>

Now objects of format new_format will show the CustomComponent.

External Methods

When referencing the form as a dom element @ViewChild(myForm) myForm, there are certain external functions that can be useful, for example, handling submit of multiple forms manually.

isValid

Check if the form is valid.

submitForm

Trigger submission of the form. Useful for trigger validation.

setHeader

Set header of the form in ngAfterViewInit or after events.

setFooter

Set footer of the form in ngAfterViewInit or after events.

getRequiredFieldCount

Get number of required fields.

Styling

We prefer csswizardry-grids to align and order fields. Form, groups and labels are assigned classes which can be utilized globally or per form. Certain forms fields can be assigned classes on top of current defaults.

{
  first_name: {
    default: 'one-half grid__item'
  },
  last_name: {
    default: 'one-half grid__item'
  }
}
1.1.15-rc.5

1 year ago

1.1.15-rc.6

1 year ago

1.1.15-rc.4

2 years ago

1.1.14

2 years ago

1.1.15-rc.2

2 years ago

1.1.15-rc.3

2 years ago

1.1.15-rc.1

2 years ago

1.1.13-rc.0

2 years ago

1.1.13-rc.1

2 years ago

1.1.13-rc.2

2 years ago

1.1.11-rc.4

2 years ago

1.1.11-rc.0

2 years ago

1.1.10

2 years ago

1.1.9-rc.10

3 years ago

1.1.9-rc.8

3 years ago

1.1.9-rc.9

3 years ago

1.1.9-rc.7

3 years ago

1.1.9-rc.6

3 years ago

1.1.9-rc.4

3 years ago

1.1.9-rc.5

3 years ago

1.1.9-rc.3

3 years ago

1.1.9-rc.2

3 years ago

1.1.9-rc.1

3 years ago

1.1.8

3 years ago

1.1.9-rc.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3-beta.10

3 years ago

1.1.3-beta.11

3 years ago

1.1.3-beta.9

3 years ago

1.1.3-beta.8

3 years ago

1.1.3-beta.7

3 years ago

1.1.3-beta.6

3 years ago

1.1.3-beta.5s

3 years ago

1.1.3-beta.4

3 years ago

1.1.3-beta.1

3 years ago

1.1.3-beta.0

3 years ago

1.1.3-beta.3

3 years ago

1.1.3-beta.2

3 years ago

1.1.2

3 years ago

1.1.2-rc-5

3 years ago

1.1.2-rc-4

3 years ago

1.1.2-rc-2

3 years ago

1.1.2-rc-3

3 years ago

1.1.2-rc-1

3 years ago

1.1.2-rc-0

3 years ago

1.1.1

3 years ago

1.1.1-rc-3

3 years ago

1.1.1-rc-2

3 years ago

1.1.1-rc-1.0

3 years ago

1.1.1-rc.0

3 years ago

1.0.9

3 years ago

1.0.9-rc.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.6.47

4 years ago

0.6.46

4 years ago

0.6.45

4 years ago

0.6.44

5 years ago

0.6.43

5 years ago

0.6.42

5 years ago

0.6.41

5 years ago

0.6.40

5 years ago

0.6.39

5 years ago

0.6.38

5 years ago

0.6.37

5 years ago

0.6.35

5 years ago

0.6.34

5 years ago

0.6.33

5 years ago

0.6.32

5 years ago

0.6.31

5 years ago

0.6.30

5 years ago

0.6.29

5 years ago

0.6.28

5 years ago

0.6.27

5 years ago

0.6.26

5 years ago

0.6.25

5 years ago

0.3.24

5 years ago

0.3.23

5 years ago

0.3.22

5 years ago

0.3.20

5 years ago

0.3.18

5 years ago

0.3.17

5 years ago

0.3.16

5 years ago

0.3.15

6 years ago

0.3.14

6 years ago

0.3.12

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.1.13

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago