Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error output to root component #1663

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/angular/src/jsonforms-root.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges
} from '@angular/core';
import { Actions, JsonFormsRendererRegistryEntry, JsonSchema, UISchemaElement, UISchemaTester, ValidationMode } from '@jsonforms/core';
import { Ajv } from 'ajv';
import { Ajv, ErrorObject } from 'ajv';
import { JsonFormsAngularService, USE_STATE_VALUE } from './jsonforms.service';
@Component({
selector: 'jsonforms',
Expand All @@ -47,6 +47,10 @@ export class JsonForms implements OnChanges, OnInit {
@Input() validationMode: ValidationMode;
@Input() ajv: Ajv;
@Input() config: any;
@Output() errors = new EventEmitter<ErrorObject[]>();

private previousData:any;
private previousErrors:ErrorObject[];

private initialized = false;

Expand All @@ -70,9 +74,15 @@ export class JsonForms implements OnChanges, OnInit {
});
this.jsonformsService.$state.subscribe(state => {
const data = state?.jsonforms?.core?.data;
if (data !== this.data) {
const errors = state?.jsonforms?.core?.errors;
if(this.previousData !== data) {
this.previousData = data;
this.dataChange.emit(data);
}
if(this.previousErrors !== errors) {
this.previousErrors = errors;
this.errors.emit(errors);
}
});
this.initialized = true;
}
Expand Down
24 changes: 16 additions & 8 deletions packages/angular/src/jsonforms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ export class JsonFormsAngularService {

updateCore<T extends CoreActions>(coreAction: T): T {
const coreState = coreReducer(this._state.core, coreAction);
this._state.core = coreState;
this.updateSubject();
if(coreState !== this._state.core) {
this._state.core = coreState;
this.updateSubject();
}
return coreAction;
}

Expand Down Expand Up @@ -156,22 +158,28 @@ export class JsonFormsAngularService {
setUiSchema(uischema: UISchemaElement | undefined): void {
const newUiSchema = uischema ?? generateDefaultUISchema(this._state.core.schema);
const coreState = coreReducer(this._state.core, Actions.updateCore(this._state.core.data, this._state.core.schema, newUiSchema));
this._state.core = coreState;
this.updateSubject();
if(coreState !== this._state.core) {
this._state.core = coreState;
this.updateSubject();
}
}

setSchema(schema: JsonSchema | undefined): void {
const coreState = coreReducer(this._state.core,
Actions.updateCore(this._state.core.data, schema ?? generateJsonSchema(this._state.core.data), this._state.core.uischema)
);
this._state.core = coreState;
this.updateSubject();
if(coreState !== this._state.core) {
this._state.core = coreState;
this.updateSubject();
}
}

setData(data: any): void {
const coreState = coreReducer(this._state.core, Actions.updateCore(data, this._state.core.schema, this._state.core.uischema));
this._state.core = coreState;
this.updateSubject();
if(coreState !== this._state.core) {
this._state.core = coreState;
this.updateSubject();
}
}

setLocale(locale: string): void {
Expand Down