0.1.80 • Published 1 year ago

formello v0.1.80

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

Formello

Angular Form Generation based on Models

Angular Angular Lending Solution

Formello is a form generation system based on models for Angular 8+

Features

  • Custom model creation
  • Automatic form creation component based on configuration
  • Data persistence on configuration changes
  • Injectable model
  • Validation and errors management
  • Custom Preset Field
  • Service injection in model
  • Supported field: Text, Select, Radio, Checkbox, Date, Autocomplete

Installation

Formello requires Angular v8+ and Angular Material/CDK to run.

npm install formello

Import formello module in your main/shared module.

@NgModule ({....
  imports: [...,
  FormelloModule
…]
})

How to create a Model

In your favorite folder create a .form.ts file.

Field Creation

new FormelloField(_name: string, _value: string | number | boolean | null, _type: FormelloFieldTypes, _validators?: ValidatorFn[], _options?: Array<IFormelloFieldOption>)

Where IFormelloFieldOption is a object {value: string | number | boolean, viewValue: string};

FormelloFieldTypes supported now are:

Type
TEXTBasic Text Field
SELECTSelect Field - declare options in options param
RADIORadio Field - declare options in options param
CHECKBOXCheckbox field - accept one option only!
SEARCH_SELECTAutocomplete select field
DATEDatepicker field

Example model

@Injectable()
export class CustomerFormModel {
    type = new FormelloField('type', 'Typology', null, FormelloFieldTypes.SELECT, [Validators.required], [{ value: 0, viewValue: 'Type 1' }, { value: 1, viewValue: 'Type 2' }]);
    surname = new FormelloField('surname', 'Surname', null, FormelloFieldTypes.TEXT, [Validators.required, Validators.pattern(myCustomPattern)]);
    name = new FormelloField('name', 'Name', null, FormelloFieldTypes.TEXT, [Validators.required, Validators.pattern(myCustomPattern)]);
    email = new FormelloField('email', 'Email', null, FormelloFieldTypes.TEXT, [Validators.email]);
    birthdate = new FormelloField('birthdate', 'Birth Date', null, FormelloFieldTypes.DATE);
    gender = new FormelloField('gender', 'Gender', null, FormelloFieldTypes.RADIO, [], [{ value: "M", viewValue: "Male" }, { value: "F", viewValue: "Female" }]);
    taxCode = new TaxCodeField('taxCode', 'Tax Code', null, [Validators.required]);
}

Custom Field Preset

TaxCodeField is a custom preset field with integrated pattern validation and error.

const PATTERN_ITALIAN_TAX_CODE = "^[A-Za-z]{6}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{3}[A-Za-z]{1}$";

export class TaxCodeField extends FormelloField {

    constructor(name: string, label: string, value: string, validators?: Array<ValidatorFn>) {
        super(name, label, value, FormelloFieldTypes.TEXT, [Validators.pattern(PATTERN_ITALIAN_TAX_CODE)].concat(validators ? validators : []));
        this.control.valueChanges.pipe(distinctUntilChanged()).subscribe((value: string) => this.control.patchValue(value.toUpperCase()));

        this.setError(GENERIC_ERROR_CODES.PATTERN, GENERIC_ERROR_MESSAGES.TAX_CODE)
    }
}

Create a model configuration (you need it to show form in different way)

To generate your form using formello component you must create a configuration. Configuration are composed of a model and some rows. A row is an object like {fields: FormelloField[]};

export class CustomerFormConfiguration1 implements IFormelloConfig<CustomerFormModel>{
    model: CustomerFormModel;
    rows: Array<IFormelloRow>;

    constructor(model: CustomerFormModel) {
        this.model = model;
        this.rows = [
            { fields: [this.model.type] },
            { fields: [this.model.name, this.model.surname] },
            { fields: [this.model.gender,  this.model.birthdate] },
            { fields: [this.model.taxCode, this.model.email] }
        ];
    }
}

export class CustomerFormConfiguration2 implements IFormelloConfig<CustomerFormModel>{
    model: CustomerFormModel;
    rows: Array<IFormelloRow>;

    constructor(model: CustomerFormModel) {
        this.model = model;
        this.rows = [
            { fields: [this.model.type] },
            { fields: [this.model.name, this.model.surname, this.model.taxCode] },
            { fields: [this.model.email] }
        ];
    }
}

So with this CustomerFormConfiguration1 configuration I want to show a form with 4 rows in the order I have setted with the field in the same order I have setted.

In the second configuration I have a different form view and I hide some fields.

Use Component

To use your model, you must inject it in your component constructor and to manipulate your formello you need to save his reference.

my-component.component.ts

@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})
export class MyComponent implements OnInit {
    myFormello: Formello<CustomerFormModel>;
    
    constructor(public customerFormModel: CustomerFormModel ){
    //Here you create a new Formello passing a configuration to it, with the model injected.
        this.myFormello = new Formello(new CustomerFormConfiguration1(this.customerFormModel));
    }
    
     ngOnInit() {
     // Here I am listening on model type control to change my form configuration based on selected value
        this.customerFormModel.type.control.valueChanges.subscribe(value => {
            switch (value) {
                case 0:
                    this.myFormello.changeConfiguration(new CustomerFormConfiguration1(this.customerFormModel));
                break;
                case 1:
                    this.myFormello.changeConfiguration(new CustomerFormConfiguration2(this.customerFormModel));
                break;
            }
        });
    }
    
}

my-component.component.html

    <formello [formello]="myFormello"></formello>

License

MIT

0.1.59

2 years ago

0.1.80

1 year ago

0.1.74

2 years ago

0.1.75

2 years ago

0.1.76

2 years ago

0.1.77

2 years ago

0.1.78

2 years ago

0.1.79

2 years ago

0.1.70

2 years ago

0.1.71

2 years ago

0.1.72

2 years ago

0.1.73

2 years ago

0.1.63

2 years ago

0.1.64

2 years ago

0.1.65

2 years ago

0.1.66

2 years ago

0.1.67

2 years ago

0.1.68

2 years ago

0.1.69

2 years ago

0.1.60

2 years ago

0.1.61

2 years ago

0.1.62

2 years ago

0.1.56

2 years ago

0.1.57

2 years ago

0.1.58

2 years ago

0.1.52

2 years ago

0.1.53

2 years ago

0.1.54

2 years ago

0.1.55

2 years ago

0.1.50

2 years ago

0.1.51

2 years ago

0.1.49

2 years ago

0.1.43

2 years ago

0.1.44

2 years ago

0.1.45

2 years ago

0.1.46

2 years ago

0.1.47

2 years ago

0.1.48

2 years ago

0.1.42

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.1.41

2 years ago

0.1.35-beta

2 years ago

0.1.36-beta

2 years ago

0.1.40

2 years ago

0.1.38

2 years ago

0.1.39

2 years ago

0.1.34-beta

2 years ago

0.1.33-beta

2 years ago

0.1.32-beta

2 years ago

0.1.31-beta

3 years ago

0.1.29-beta

3 years ago

0.1.26-beta

3 years ago

0.1.30-beta

3 years ago

0.1.27-beta

3 years ago

0.1.28-beta

3 years ago

0.1.24-beta

3 years ago

0.1.25-beta

3 years ago

0.1.23-beta

3 years ago

0.1.22-beta

3 years ago

0.1.0-beta

3 years ago

0.1.1-beta

3 years ago

0.1.2-beta

3 years ago

0.1.21-beta

3 years ago

0.0.9-beta

3 years ago

0.0.8-beta

3 years ago

0.0.7-beta

3 years ago

0.0.7-alpha

3 years ago

0.0.6-beta

3 years ago

0.0.6-alpha

3 years ago

0.0.5-beta

3 years ago

0.0.4-beta

3 years ago

0.0.3-beta

3 years ago

0.0.2-beta

3 years ago

0.0.1-beta

3 years ago