uniform-ng2
Forms for angular2
Last updated 2 years ago by unimicro .
MIT · Original npm · Tarball · package.json
$ cnpm install uniform-ng2 
SYNC missed versions from official npm registry.

Uni Form

Uniform is a HTML5 form layout builder

Configuration

Uniform is based on a JSON configuration. This configuration is an array of fields. To build configurations you can use the layout builder.

// example
constructor(builder: LayoutBuilder) {
    // Definition in uniform/interfaces.ts
    let field: UniField = new FieldLayout(); 
    
    field.Property = 'Info.PersonalInfo.Name';
    field.Section = 1;
    field.SectionHeader = 'Title of the header';
    field.LineBreak = true; // next field will be moved to the next line
    field.ReadOnly = false;
    field.Placement = 1; // Order of the field;
    field.Placeholder = 'Insert your name in this input';
    field.Label = 'Name';
    field.FieldType = CONTROLS_ENUM.Multivalue
    
    //...
    
    this.builder.createNewLayout()
        .addField(field) // add a FieldLayout directly
        .addField( 
            // create a unifield with basic properties
            
            'property.of.the.model', //should be unique
            'Label to show in the form',
            
            // all types in uniform/controls/index.ts
            CONTROLS_ENUM.AUTOCOMPLETE 
            
            FieldSize.Normal, // Normal, Double, Quarter, Full.
            false, //Hide or show the label of the form
            0, // section of the field
            'Section Header'
            {}, // specific options for each field
        );
    
    this.layout = this.builder.getLayout();
}

Use uniform in a component

Import uniform and builder from the module

import {UniForm, LayoutBuilder} from 'uniform-ng2/main';

add builder to your component:

@Component({
    selector: 'my-component'
    templateUrl: './my-component.html'
})
export MyComponent {
    public model: Person; 
    public layout: UniFieldLayout;
    public config: UniFormConfig = {
        autofocus: true; //focus first element by default
        sections: { id: 1, isOpen: true} // section 1 is open on start
    }
    constructor(builder: LayoutBuilder) {
        this.layout = builder.createNewLayout()
            .addField(...)
            .addField(...)
            .getLayout();
    }
}

template:

<uni-form 
    [layout]="layout"
    [config]="config"
    [model]="model"
    (submitEvent)="onSubmit($event)"
    (changeEvent)="onChange($event)"
    (inputEvent)="oninput($event)"
    (readyEvent)="onReady($event)" 
    (focusEvent)="onFocus($event)"
>   
</uni-form>

access to the component:


// define a property to store the form
@ViewChild(UniForm)
public form: UniForm;

// at this stage (hook) html element is available
public ngAfterViewInit() {
    //Access to focused component
    this.form.currentComponent.then(field => this.doMyStuff(field));
    
    //how to access fields
    this.form.field('Property').then(field => this.doOtherStuff(field));
    this.form.section(index)
        .then(section => section.field('property'))
        .then(field => this.moreStuff(field))
}

Use Observables as inputs

Uniform support Observables as inputs

Events (Outputs)

submitEvent

It is triggered when the form is submited

changeEvent

It is triggered when the user emits a complete change to the form. A complete change is triggered when blur event is emited by an input.

inputEvent

It is triggered when there is a change in inputs when a key is pressed. In complex inputs like autocomplete or multivalue it is emited at the same time that changeEvent.

readyEvent

It is triggered when all input components have run ngAfterViewInit. That means all components have html elements in place and you can start form manipulation

focusEvent

It is triggered when a form element is focused

Refresh uniform data

Uniform uses OnPush strategies. That means that mutation of objects doesn't trigger OnChange events and refreshing of items will not be showed in the screen.

To perform a refresh you have 2 ways:

  • You are using plain objects:

    public ngOnInit() {
        getLayoutFromServer().then(layout => {
            layout.Fields[0].Options = createOptions();
            this.layout = layout; // first time you update layout
        });
    }
    public onChange(changes: SimpleChanges) {
        if (changes['Info.Personal.Name']) {
            let field = this.layout.find(x => x.Property === 'Info.Personal.Name');
            checkName(field).then(result => {
                field.ReadOnly = result;
                this.layout = _.cloneDeep(this.layout) 
                // creates a new reference that triggers ngOnChanges
                // inside uniform
            })
        }
    }
    
  • You are using observables

    public ngOnInit() {
        getLayoutFromServer().then(layout => {
            layout.Fields[0].Options = createOptions();
            this.layout.next(layout); // first time you update layout
        });
    }
    public onChange(changes: SimpleChanges) {
        if (changes['Info.Personal.Name']) {
            let field = this.layout.find(x => x.Property === 'Info.Personal.Name');
            checkName(field).then(result => {
                field.ReadOnly = result;
                this.layout.next(this.layout);
                // cloneDeep will run inside uniform and
                // creates a new reference inside uniform
            });
        }
    }
    

You have to do the same if you update the config or the model.

Current Tags

  • 0.1.0-beta.14                                ...           beta (2 years ago)
  • 0.1.2                                ...           latest (2 years ago)
  • 0.0.48-0                                ...           WEBPACK_TEST (3 years ago)

145 Versions

  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
  • 0.1.0-beta.14                                ...           2 years ago
  • 0.1.0-beta.13                                ...           2 years ago
  • 0.1.0-beta.12                                ...           2 years ago
  • 0.1.0-beta.11                                ...           2 years ago
  • 0.1.0-beta.10                                ...           2 years ago
  • 0.1.0-beta.9                                ...           2 years ago
  • 0.1.0-beta.8                                ...           2 years ago
  • 0.0.97                                ...           2 years ago
  • 0.1.0-beta.7                                ...           2 years ago
  • 0.1.0-beta.6                                ...           2 years ago
  • 0.1.0-beta.5                                ...           2 years ago
  • 0.1.0-beta.4                                ...           2 years ago
  • 0.0.96                                ...           2 years ago
  • 0.1.0-beta.3                                ...           2 years ago
  • 0.1.0-beta.2                                ...           2 years ago
  • 0.1.0-beta.1                                ...           2 years ago
  • 0.0.95                                ...           2 years ago
  • 0.1.0-beta.0                                ...           2 years ago
  • 0.0.95-beta.1                                ...           2 years ago
  • 0.0.95-beta.0                                ...           2 years ago
  • 0.0.94                                ...           2 years ago
  • 0.0.94-beta.1                                ...           2 years ago
  • 0.0.94-beta.0                                ...           2 years ago
  • 0.0.93                                ...           2 years ago
  • 0.0.93-beta.19                                ...           2 years ago
  • 0.0.93-beta.18                                ...           2 years ago
  • 0.0.93-beta.17                                ...           2 years ago
  • 0.0.93-beta.16                                ...           2 years ago
  • 0.0.93-beta.15                                ...           2 years ago
  • 0.0.93-beta.14                                ...           2 years ago
  • 0.0.93-beta.13                                ...           2 years ago
  • 0.0.93-beta.12                                ...           2 years ago
  • 0.0.93-beta.11                                ...           2 years ago
  • 0.0.93-beta.10                                ...           2 years ago
  • 0.0.93-beta.9                                ...           2 years ago
  • 0.0.93-beta.8                                ...           2 years ago
  • 0.0.93-beta.7                                ...           2 years ago
  • 0.0.93-beta.6                                ...           2 years ago
  • 0.0.93-beta.5                                ...           2 years ago
  • 0.0.93-beta.4                                ...           2 years ago
  • 0.0.93-beta.3                                ...           2 years ago
  • 0.0.93-beta.2                                ...           2 years ago
  • 0.0.93-beta.1                                ...           2 years ago
  • 0.0.93-beta.0                                ...           2 years ago
  • 0.0.92                                ...           2 years ago
  • 0.0.91                                ...           2 years ago
  • 0.0.90                                ...           2 years ago
  • 0.0.90-rc.8                                ...           2 years ago
  • 0.0.90-rc.7                                ...           2 years ago
  • 0.0.90-rc.6                                ...           2 years ago
  • 0.0.90-rc.5                                ...           3 years ago
  • 0.0.90-rc.4                                ...           3 years ago
  • 0.0.90-rc.3                                ...           3 years ago
  • 0.0.90-rc.2                                ...           3 years ago
  • 0.0.90-rc.1                                ...           3 years ago
  • 0.0.89                                ...           3 years ago
  • 0.0.88                                ...           3 years ago
  • 0.0.87                                ...           3 years ago
  • 0.0.86                                ...           3 years ago
  • 0.0.85                                ...           3 years ago
  • 0.0.84                                ...           3 years ago
  • 0.0.83-rc.1                                ...           3 years ago
  • 0.0.83                                ...           3 years ago
  • 0.0.82                                ...           3 years ago
  • 0.0.81                                ...           3 years ago
  • 0.0.80                                ...           3 years ago
  • 0.0.79                                ...           3 years ago
  • 0.0.78                                ...           3 years ago
  • 0.0.77                                ...           3 years ago
  • 0.0.76                                ...           3 years ago
  • 0.0.75                                ...           3 years ago
  • 0.0.74                                ...           3 years ago
  • 0.0.73                                ...           3 years ago
  • 0.0.72                                ...           3 years ago
  • 0.0.71                                ...           3 years ago
  • 0.0.70                                ...           3 years ago
  • 0.0.69                                ...           3 years ago
  • 0.0.68                                ...           3 years ago
  • 0.0.67                                ...           3 years ago
  • 0.0.66                                ...           3 years ago
  • 0.0.65                                ...           3 years ago
  • 0.0.64                                ...           3 years ago
  • 0.0.63                                ...           3 years ago
  • 0.0.62                                ...           3 years ago
  • 0.0.60                                ...           3 years ago
  • 0.0.59                                ...           3 years ago
  • 0.0.58                                ...           3 years ago
  • 0.0.57                                ...           3 years ago
  • 0.0.56                                ...           3 years ago
  • 0.0.55                                ...           3 years ago
  • 0.0.54                                ...           3 years ago
  • 0.0.53                                ...           3 years ago
  • 0.0.52                                ...           3 years ago
  • 0.0.51                                ...           3 years ago
  • 0.0.50                                ...           3 years ago
  • 0.0.49                                ...           3 years ago
  • 0.0.48                                ...           3 years ago
  • 0.0.48-0                                ...           3 years ago
  • 0.0.47                                ...           3 years ago
  • 0.0.46                                ...           3 years ago
  • 0.0.45                                ...           3 years ago
  • 0.0.44                                ...           3 years ago
  • 0.0.42                                ...           3 years ago
  • 0.0.41                                ...           3 years ago
  • 0.0.40                                ...           3 years ago
  • 0.0.39                                ...           3 years ago
  • 0.0.38                                ...           3 years ago
  • 0.0.37                                ...           3 years ago
  • 0.0.36                                ...           3 years ago
  • 0.0.35                                ...           3 years ago
  • 0.0.34                                ...           3 years ago
  • 0.0.33                                ...           3 years ago
  • 0.0.32                                ...           3 years ago
  • 0.0.31                                ...           3 years ago
  • 0.0.30                                ...           3 years ago
  • 0.0.29                                ...           3 years ago
  • 0.0.28                                ...           3 years ago
  • 0.0.27                                ...           3 years ago
  • 0.0.26                                ...           3 years ago
  • 0.0.25                                ...           3 years ago
  • 0.0.24                                ...           3 years ago
  • 0.0.23                                ...           3 years ago
  • 0.0.22                                ...           3 years ago
  • 0.0.21                                ...           3 years ago
  • 0.0.20                                ...           3 years ago
  • 0.0.19                                ...           3 years ago
  • 0.0.18                                ...           3 years ago
  • 0.0.17                                ...           3 years ago
  • 0.0.16                                ...           3 years ago
  • 0.0.14                                ...           3 years ago
  • 0.0.13                                ...           3 years ago
  • 0.0.12                                ...           3 years ago
  • 0.0.11                                ...           3 years ago
  • 0.0.10                                ...           3 years ago
  • 0.0.8                                ...           3 years ago
  • 0.0.7                                ...           3 years ago
  • 0.0.6                                ...           3 years ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 291
Dependencies (17)
Dev Dependencies (13)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |