|
30 | 30 | To use the library in your project, follow these steps: |
31 | 31 |
|
32 | 32 | ```shell |
33 | | -npm i @angular/material |
34 | | -npm i @angular/flex-layout |
35 | | -npm i @angular/cdk |
36 | 33 | npm i @dashjoin/json-schema-form |
37 | 34 | ``` |
38 | 35 |
|
@@ -78,35 +75,43 @@ A small sample component: |
78 | 75 |
|
79 | 76 | ```typescript |
80 | 77 | import { Component } from '@angular/core'; |
81 | | -import { Schema } from '@dashjoin/json-schema-form/lib/schema'; |
| 78 | +import { State } from '@dashjoin/json-schema-form'; |
| 79 | +import { FormArray } from '@angular/forms'; |
82 | 80 |
|
83 | 81 | @Component({ |
84 | 82 | selector: 'app-root', |
85 | 83 | template: ` |
86 | | - <lib-json-schema-form [(value)]="value" [schema]="schema" [label]="schema.title"></lib-json-schema-form> |
87 | | - <pre>{{print()}}<pre> |
| 84 | + <lib-json-schema-form [state]="state"></lib-json-schema-form> |
88 | 85 | ` |
89 | 86 | }) |
90 | 87 | export class AppComponent { |
91 | 88 |
|
92 | | - schema: Schema = { |
93 | | - type: 'array', |
94 | | - items: { |
95 | | - type: 'object', |
96 | | - properties: { |
97 | | - name: { type: 'string' }, |
98 | | - bday: { type: 'string', widget: 'date' } |
| 89 | + state: State = { |
| 90 | + schema: { |
| 91 | + type: 'array', |
| 92 | + items: { |
| 93 | + type: 'object', |
| 94 | + properties: { |
| 95 | + name: { type: 'string' }, |
| 96 | + bday: { type: 'string', widget: 'date' } |
| 97 | + } |
99 | 98 | } |
100 | | - } |
| 99 | + }, |
| 100 | + value: any = [{ |
| 101 | + name: 'Joe', |
| 102 | + bday: '2018-09-09T22:00:00.000Z' |
| 103 | + }]; |
| 104 | + name: 'myform', |
| 105 | +
|
| 106 | + // pick FormArray, FormGroup or FormControl for arrays, objects, or single values respectively |
| 107 | + control: new FormArray([]) |
101 | 108 | }; |
102 | | - |
103 | | - value: any = [{ |
104 | | - name: 'Joe', |
105 | | - bday: '2018-09-09T22:00:00.000Z' |
106 | | - }]; |
107 | | -
|
108 | | - print(): string { |
109 | | - return JSON.stringify(this.value, null, 2); |
| 109 | +
|
| 110 | + foo() { |
| 111 | + // subscribe to form value change / validation or state events |
| 112 | + this.state.control.valueChanges.subscribe(res => { |
| 113 | + console.log(res); |
| 114 | + }) |
110 | 115 | } |
111 | 116 | } |
112 | 117 | ``` |
@@ -145,7 +150,7 @@ This option specifies a specific input widget to be used. The default is a simpl |
145 | 150 |
|
146 | 151 | It is possible to create custom widgets using the following steps: |
147 | 152 |
|
148 | | -* Create a component that implements [WidgetComponent](https://github.com/dashjoin/json-schema-form/blob/master/projects/dashjoin/json-schema-form/src/lib/widget.component.ts). All relevant data such as the applicable subschema and the current value are passed to the component. Make sure to emit value changes. An example can be found [here](https://github.com/dashjoin/json-schema-form/tree/master/src/app/custom.component.ts) |
| 153 | +* Create a component that extends [BaseComponent](https://github.com/dashjoin/json-schema-form/blob/master/projects/dashjoin/json-schema-form/src/lib/base/base.component.ts). All relevant data such as the applicable subschema and the current value are passed to the component. Make sure to emit value changes via state.control. An example can be found [here](https://github.com/dashjoin/json-schema-form/blob/master/src/app/custom/custom.component.ts) |
149 | 154 | * Include the component in your @NgModule declarations |
150 | 155 | * In the parent component, add this service to your constructor: private service: JsonSchemaFormService |
151 | 156 | * Register your widget in ngOnInit() using this service: this.service.registerComponent('rich-text-editor', CustomComponent); |
|
0 commit comments