0.1.2 • Published 7 years ago

uniform-ng2 v0.1.2

Weekly downloads
1
License
MIT
Repository
-
Last release
7 years ago

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.

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.1.0-beta.14

7 years ago

0.1.0-beta.13

7 years ago

0.1.0-beta.12

7 years ago

0.1.0-beta.11

7 years ago

0.1.0-beta.10

7 years ago

0.1.0-beta.9

7 years ago

0.1.0-beta.8

7 years ago

0.0.97

7 years ago

0.1.0-beta.7

7 years ago

0.1.0-beta.6

7 years ago

0.1.0-beta.5

7 years ago

0.1.0-beta.4

7 years ago

0.0.96

7 years ago

0.1.0-beta.3

7 years ago

0.1.0-beta.2

7 years ago

0.1.0-beta.1

7 years ago

0.0.95

7 years ago

0.1.0-beta.0

7 years ago

0.0.95-beta.1

7 years ago

0.0.95-beta.0

7 years ago

0.0.94

7 years ago

0.0.94-beta.1

7 years ago

0.0.94-beta.0

7 years ago

0.0.93

7 years ago

0.0.93-beta.19

7 years ago

0.0.93-beta.18

7 years ago

0.0.93-beta.17

7 years ago

0.0.93-beta.16

7 years ago

0.0.93-beta.15

7 years ago

0.0.93-beta.14

7 years ago

0.0.93-beta.13

7 years ago

0.0.93-beta.12

7 years ago

0.0.93-beta.11

7 years ago

0.0.93-beta.10

7 years ago

0.0.93-beta.9

7 years ago

0.0.93-beta.8

7 years ago

0.0.93-beta.7

7 years ago

0.0.93-beta.6

7 years ago

0.0.93-beta.5

7 years ago

0.0.93-beta.4

7 years ago

0.0.93-beta.3

7 years ago

0.0.93-beta.2

7 years ago

0.0.93-beta.1

7 years ago

0.0.93-beta.0

7 years ago

0.0.92

7 years ago

0.0.91

7 years ago

0.0.90

7 years ago

0.0.90-rc.8

7 years ago

0.0.90-rc.7

7 years ago

0.0.90-rc.6

7 years ago

0.0.90-rc.5

7 years ago

0.0.90-rc.4

7 years ago

0.0.90-rc.3

7 years ago

0.0.90-rc.2

7 years ago

0.0.90-rc.1

7 years ago

0.0.89

7 years ago

0.0.88

7 years ago

0.0.87

7 years ago

0.0.86

7 years ago

0.0.85

7 years ago

0.0.84

7 years ago

0.0.83-rc.1

7 years ago

0.0.83

7 years ago

0.0.82

7 years ago

0.0.81

7 years ago

0.0.80

7 years ago

0.0.79

7 years ago

0.0.78

7 years ago

0.0.77

7 years ago

0.0.76

7 years ago

0.0.75

7 years ago

0.0.74

7 years ago

0.0.73

7 years ago

0.0.72

7 years ago

0.0.71

7 years ago

0.0.70

7 years ago

0.0.69

7 years ago

0.0.68

7 years ago

0.0.67

7 years ago

0.0.66

7 years ago

0.0.65

7 years ago

0.0.64

7 years ago

0.0.63

7 years ago

0.0.62

7 years ago

0.0.60

7 years ago

0.0.59

7 years ago

0.0.58

7 years ago

0.0.57

7 years ago

0.0.56

7 years ago

0.0.55

7 years ago

0.0.54

7 years ago

0.0.53

7 years ago

0.0.52

7 years ago

0.0.51

7 years ago

0.0.50

7 years ago

0.0.49

7 years ago

0.0.48

7 years ago

0.0.48-0

7 years ago

0.0.47

7 years ago

0.0.46

7 years ago

0.0.45

7 years ago

0.0.44

7 years ago

0.0.42

7 years ago

0.0.41

7 years ago

0.0.40

7 years ago

0.0.39

7 years ago

0.0.38

7 years ago

0.0.37

7 years ago

0.0.36

7 years ago

0.0.35

7 years ago

0.0.34

7 years ago

0.0.33

7 years ago

0.0.32

7 years ago

0.0.31

7 years ago

0.0.30

7 years ago

0.0.29

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.26

7 years ago

0.0.25

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago