6.0.7-alpha • Published 1 year ago

@cisco-msx/forms v6.0.7-alpha

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

@cisco-msx/forms

Form builder classes, renderer, and Angular upgrades for MSX form field components.

Usage

import { NgModule } from '@angular/core';
import { MsxFormsModule } from '@cisco-msx/forms';

@NgModule({
  imports: [
    MsxFormsModule
  ]
})
export class MyModule {}

Testing

If you are testing your component using Angular's TestBed, import the MsxFormsTestingModule in your testing module instead of MsxFormsModule to have @cisco-msx/forms's components and directives mocked.

import {
  async,
  ComponentFixture,
  TestBed
} from '@angular/core/testing';
import { MsxFormsTestingModule } from '@cisco-msx/forms/testing';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        MsxFormsTestingModule
      ],
      declarations: [MyComponent]
    });
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
  }));
});

Form Fields

The upgraded form inputs that can be used are also included in the MsxFormModule. You can find the upgraded components here.

Form Builder/Renderer

This package contains the Form and FormGroup classes that are the basis for being a structure for your form. It also contains the DynamicFormComponent that takes instances of the Form and FormGroup to automatically render and handle the change lifecycle of the fields within them.

Note: If fields are added to a Form and a FormGroup you must render 2 dynamic form components as fields are encapsulated into the instance of where they are added even if all values are available in the top level form

The following is an example of how to use the dynamic forms using a Form and a FormGroup. This example uses Angular 8 but you can build forms (without the dynamic rendering) with any (or no) framework.

@Component({
    selector: 'my-component',
    template: `
    <div>
        <div>
            <h1>Name Information</h1>
            <msx-form [form]="nameForm"></msx-form>
        </div>
        <div>
            <h1>Location Information</h1>
            <!--Without passing the locationFormGroup, the address and postal code fields would never show  -->
            <msx-form [form]="locationFormGroup"></msx-form>
        </div>
    </div>
`
})
class MyComponent {

    nameForm = new Form();
    locationFormGroup = new FormGroup('locationGroup');

    ngOnInit() {
        this.nameForm.setFields([
            {
                name: "firstName",
                initialValue: "",
                properties: {
                    label: "First Name",
                    required: true,
                    placeholder: "Johny"
                }
            },
            {
                name: "lastName",
                initialValue: "",
                properties: {
                    label: "Last Name",
                    required: true,
                    placeholder: "Bravo"
                }
            }
        ]);

        this.locationGroup.setFields([
            {
                name: "address",
                initialValue: "",
                properties: {
                    required: true,
                    label: "Address",
                    placeholder: "123 Somewhere Lane"
                }
            },
            {
                name: "postalCode",
                initialValue: "",
                properties: {
                    label: "Postal Code",
                    required: true,
                    placeholder: "K0F K8A"
                }
            }
        ]);


        this.nameForm.setFormGroup(this.locationGroup);
    }
}

The example above would render a section header Name Information with 2 fields (firstName, lastName) and another section header Location Information with the 2 fields (address and postal code). Note that 2 msx-form components where used, one for the Form and one for the FormGroup. This is because fields are encapsulated where they are added. However, the Form will still know when a field from a child FormGroup changes.

Other Resources

6.0.5-alpha

1 year ago

6.0.3-alpha

1 year ago

6.0.4-alpha

1 year ago

6.0.7-alpha

1 year ago

6.0.6-alpha

1 year ago

6.0.0-alpha

1 year ago

6.0.1-alpha

1 year ago

6.0.2-alpha

1 year ago

2.10.0

1 year ago

2.9.11

1 year ago

2.9.9

2 years ago

2.9.4

2 years ago

2.9.3

2 years ago

2.9.6

2 years ago

2.9.5

2 years ago

2.9.8

2 years ago

2.9.7

2 years ago

2.9.10

2 years ago

2.9.2

2 years ago

2.9.1

2 years ago

2.8.2

2 years ago

2.7.3

2 years ago

2.9.0

2 years ago

2.8.1

2 years ago

2.7.2

2 years ago

2.8.0

2 years ago

2.7.0

3 years ago

2.7.1

3 years ago

2.6.2

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.25

3 years ago