4.0.0 • Published 3 years ago

@banzaicloud/uniform v4.0.0

Weekly downloads
363
License
-
Repository
-
Last release
3 years ago

Uniform npm version

This library contains building blocks for generating dynamic Angular forms.

Usage

Install dependency:

npm i @banzaicloud/uniform

Include the module import for your application:

import { UniformModule } from '@banzaicloud/uniform';

@NgModule({
  imports: [
    // ...
    UniformModule,
  ],
})
export class AppModule {}

Use the UniformService to generate groups with form field types from raw input:

import { Component, OnInit } from '@angular/core';
import { UniformService, IFormFieldGroup } from '@banzaicloud/uniform';

@Component({
  selector: 'app-root',
  template: `
    <bcu-form
      [groups]="groups"
      (valueChanges)="onValueChanges($event)"
      (save)="onSave($event)">
    </bcu-form>
  `,
})
export class AppComponent implements OnInit {
  // can be fetched from an API
  rawGroups = [
    {
      name: 'Uniform demo',
      fields: [
        {
          controlType: 'textarea',
          key: 'wishlist',
          label: 'Wish list',
          placeholder: 'What are your desires?',
        },
      ],
    },
  ];
  groups: IFormFieldGroup[];
  values: { [key: string]: any };

  ngOnInit() {
    this.groups = UniformService.factory(this.rawGroups);
  }

  onValueChanges(values) {
    this.values = values;
  }

  onSave(rawValues) {
    console.log('Raw form values:', rawValues);
  }
}

Alternatively, use the field classes to build a form:

import { Component } from '@angular/core';
import { UniformService, FormFieldTextarea, IFormFieldGroup } from '@banzaicloud/uniform';

@Component({
  selector: 'app-root',
  template: `
    <bcu-form
      [groups]="groups"
      (valueChanges)="onValueChanges($event)"
      (save)="onSave($event)">
    </bcu-form>
  `,
})
export class AppComponent {
  groups: IFormFieldGroup[] = [
    {
      name: 'Uniform demo',
      fields: [
        new FormFieldTextarea({
          key: 'wishlist',
          label: 'Wish list',
          placeholder: 'What are your desires?',
        }),
      ],
    },
  ];
  values: { [key: string]: any };

  onValueChanges(values) {
    this.values = values;
  }

  onSave(rawValues) {
    console.log('Raw form values:', rawValues);
  }
}

Control types

Each control can be specified with the following options:

FIELDTYPEDEFAULTDESCRIPTION
controlTypestring (required)Control type
keystring (required)Unique key of the control
labelstring (required)Label of the control (user friendly name)
defaultanyDefault value
requiredboolfalseRequired control
hiddenboolfalseAlways hidden control
disabledboolfalseAlways disabled control (eg. use with default)
placeholderstringPlaceholder (for example or description)
descriptionstringDescription or hint
minLengthnumberMinimum length of value
maxLengthnumberMaximum length of value
patternstringRegular expression to validate value (eg. fqdn)
showIfobjectJSON schema to conditionally show or hide the control

checkbox | FormFieldCheckbox

select | FormFieldSelect

Additional options:

FIELDTYPEDEFAULTDESCRIPTION
options{ label: string, value: any }Select options
multiplebooleanWhether the user can select multiple options

number | FormFieldNumber

text | FormFieldText

password | FormFieldPassword

Additional options:

FIELDTYPEDEFAULTDESCRIPTION
passwordRevealButtonbooleanfalseEnables a show/hide functionality on the password field

textarea | FormFieldTextarea

code | FormFieldCode

Additional options:

FIELDTYPEDEFAULTDESCRIPTION
optionsobjectConfig options passed to the CodeMirror instance

file | FormFieldFile

Additional options:

FIELDTYPEDEFAULTDESCRIPTION
fillFormbooltrueParse the file as JSON and set the form values
acceptstringComma-separated list of accepted, unique file type specifiers

For example, when the field has a key of google.json_key and fillForm is set to true, the key-value pairs in the JSON file will be parsed and applied to the form values under the key prefix google.* (google.service_account, google.project_id, ...).

4.0.0

3 years ago

3.0.0

4 years ago

2.2.1

4 years ago

2.2.1-0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.12.4

4 years ago

1.12.3

4 years ago

1.12.2

4 years ago

1.12.1

4 years ago

1.12.0

4 years ago

1.12.0-6

4 years ago

1.12.0-7

4 years ago

1.12.0-4

4 years ago

1.12.0-5

4 years ago

1.12.0-3

4 years ago

1.12.0-2

4 years ago

1.12.0-1

4 years ago

1.12.0-0

4 years ago

1.11.7

5 years ago

1.11.6

5 years ago

1.11.5

5 years ago

1.11.4

5 years ago

1.11.3

5 years ago

1.11.2

5 years ago

1.11.1

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.3

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.5

5 years ago

1.7.4

5 years ago

1.7.3

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.2.0-beta.8

5 years ago

1.2.0-beta.7

5 years ago

1.2.0-beta.6

5 years ago

1.2.0-beta.5

5 years ago

1.2.0-beta.4

5 years ago

1.2.0-beta.3

5 years ago

1.2.0-beta.2

5 years ago

1.2.0-beta.1

5 years ago

1.2.0-beta.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.0.24

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago