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

Bind onChange against input event, fix layout trackBy #1187

Merged
merged 4 commits into from
Dec 12, 2018
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
2 changes: 1 addition & 1 deletion packages/angular-material/src/controls/number.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { LocaleValidation } from 'angular-l10n';
<mat-label>{{ label }}</mat-label>
<input
matInput
(change)="onChange($event)"
(input)="onChange($event)"
placeholder="{{ description }}"
[value]="data !== undefined && data !== null && locale ?
(data | l10nDecimal:locale:digits) :
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-material/src/controls/text.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { isControl, JsonFormsState, RankedTester, rankWith } from '@jsonforms/co
<input
matInput
[type]="getType()"
(change)="onChange($event)"
(input)="onChange($event)"
placeholder="{{ description }}"
[id]="id"
[formControl]="form"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { isMultiLineControl, JsonFormsState, RankedTester, rankWith } from '@jso
<mat-label>{{ label }}</mat-label>
<textarea
matInput
(change)="onChange($event)"
(input)="onChange($event)"
placeholder="{{ description }}"
[id]="id"
[formControl]="form"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { NgRedux } from '@angular-redux/store';
template: `
<mat-card fxLayout='column' [fxHide]="hidden">
<mat-card-title class='mat-title'>{{uischema.label}}</mat-card-title>
<div *ngFor="let props of renderProps" fxFlex>
<div *ngFor="let props of renderProps; trackBy: trackElement" fxFlex>
<jsonforms-outlet [renderProps]="props"></jsonforms-outlet>
</div>
</mat-card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { NgRedux } from '@angular-redux/store';
selector: 'HorizontalLayoutRenderer',
template: `
<div fxLayout='row' fxLayoutGap='16px' [fxHide]="hidden">
<div *ngFor="let props of renderProps" fxFlex>
<div *ngFor="let props of renderProps; trackBy: trackElement" fxFlex>
<jsonforms-outlet [renderProps]="props"></jsonforms-outlet>
</div>
</div>
Expand Down
28 changes: 10 additions & 18 deletions packages/angular-material/src/layouts/layout.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,18 @@ import { OnDestroy, OnInit } from '@angular/core';
import { JsonFormsBaseRenderer } from '@jsonforms/angular';
import {
JsonFormsState,
JsonSchema,
Layout,
mapStateToLayoutProps,
OwnPropsOfRenderer,
UISchemaElement } from '@jsonforms/core';
UISchemaElement
} from '@jsonforms/core';
import { Subscription } from 'rxjs';

export class LayoutRenderer<T extends Layout>
extends JsonFormsBaseRenderer implements OnInit, OnDestroy {

hidden: boolean;
private subscription: Subscription;
private elements: OwnPropsOfRenderer[];
private oldSchema: JsonSchema;
private oldUISchema: T;
private oldPath: string;

constructor(private ngRedux: NgRedux<JsonFormsState>) {
super();
Expand All @@ -64,19 +60,15 @@ export class LayoutRenderer<T extends Layout>
}

get renderProps(): OwnPropsOfRenderer[] {
if (this.uischema === this.oldUISchema &&
this.schema === this.oldSchema &&
this.path === this.oldPath) {
return this.elements;
}
this.oldUISchema = this.uischema as T;
this.oldSchema = this.schema;
this.oldPath = this.path;
this.elements = (this.oldUISchema.elements || []).map((el: UISchemaElement) => ({
const elements = ((this.uischema as T).elements || []).map((el: UISchemaElement) => ({
uischema: el,
schema: this.oldSchema,
path: this.oldPath
schema: this.schema,
path: this.path
}));
return this.elements;
return elements;
}

trackElement(_index: number, renderProp: OwnPropsOfRenderer): string {
return renderProp ? renderProp.path + JSON.stringify(renderProp.uischema) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { NgRedux } from '@angular-redux/store';
selector: 'VerticalLayoutRenderer',
template: `
<div fxLayout='column' fxLayoutGap='16px' [fxHide]="hidden">
<div *ngFor="let props of renderProps" fxFlex>
<div *ngFor="let props of renderProps; trackBy: trackElement" fxFlex>
<jsonforms-outlet [renderProps]="props"></jsonforms-outlet>
</div>
</div>
Expand Down
6 changes: 2 additions & 4 deletions packages/angular-material/test/object-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,10 @@ describe('Object Control', () => {
}
}
});
component.ngOnInit();
mockSubStore.complete();
fixture.detectChanges();

fixture.whenRenderingDone().then(() => {
fixture.detectChanges();
component.ngOnInit();
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelectorAll('input').length).toBe(2);
});
}));
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-test/src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export const numberInputEventTest = <C extends JsonFormsControl>(
const spy = spyOn(component, 'onChange');
numberNativeElement.value = 456.456;
if (numberNativeElement.dispatchEvent) {
numberNativeElement.dispatchEvent(new Event('change'));
numberNativeElement.dispatchEvent(new Event('input'));
}
// trigger change detection
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion packages/angular-test/src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export const textInputEventTest = <C extends JsonFormsControl>(
const spy = spyOn(component, 'onChange');
textNativeElement.value = 'bar';
if (textNativeElement.dispatchEvent) {
textNativeElement.dispatchEvent(new Event('change'));
textNativeElement.dispatchEvent(new Event('input'));
}
// trigger change detection
fixture.detectChanges();
Expand Down