Skip to content

Commit 0a80d6d

Browse files
committed
Improve typings
* Split own & stated-based props (no optionals anymore) * Change type of 1st parameter in isVisible/isEnabled * Provide default renderer props * Remove parentPath prop * Add type to errorAt * Replace resolvedRootData with data in mui-tree * Differentiate between array control and control in angular package
1 parent 570b941 commit 0a80d6d

File tree

25 files changed

+431
-294
lines changed

25 files changed

+431
-294
lines changed

packages/angular-material/src/other/label.renderer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Component } from '@angular/core';
44
import { JsonFormsBaseRenderer } from '@jsonforms/angular';
55
import { Subscription } from 'rxjs/Subscription';
66
import {
7+
getData,
78
isVisible,
89
JsonFormsState,
910
LabelElement,
@@ -58,7 +59,7 @@ const mapStateToProps = (
5859
) => {
5960
const visible = has(ownProps, 'visible')
6061
? ownProps.visible
61-
: isVisible(ownProps, state);
62+
: isVisible(ownProps.uischema, getData(state));
6263

6364
return {
6465
visible

packages/angular-material/src/other/master-detail/master.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import some from 'lodash/some';
22
import get from 'lodash/get';
33
import { Component } from '@angular/core';
4-
import { JsonFormsControl } from '@jsonforms/angular';
4+
import { JsonFormsArrayControl } from '@jsonforms/angular';
55
import { NgRedux } from '@angular-redux/store';
66
import {
77
ArrayControlProps,
@@ -104,7 +104,7 @@ export const removeSchemaKeywords = (path: string) => {
104104
`
105105
]
106106
})
107-
export class MasterListComponent extends JsonFormsControl {
107+
export class MasterListComponent extends JsonFormsArrayControl {
108108
masterItems: any[];
109109
selectedItem: any;
110110
selectedItemIdx: number;
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2019 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import {
26+
Actions,
27+
composeWithUi,
28+
computeLabel,
29+
ControlElement,
30+
isPlainLabel,
31+
JsonFormsState,
32+
JsonSchema,
33+
OwnPropsOfControl,
34+
StatePropsOfControl
35+
} from '@jsonforms/core';
36+
import { Input, OnDestroy, OnInit } from '@angular/core';
37+
import { NgRedux } from '@angular-redux/store';
38+
import {
39+
AbstractControl,
40+
FormControl,
41+
ValidationErrors,
42+
ValidatorFn
43+
} from '@angular/forms';
44+
import { Subscription } from 'rxjs';
45+
46+
import { JsonFormsBaseRenderer } from './base.renderer';
47+
48+
export abstract class JsonFormsAbstractControl<
49+
Props extends StatePropsOfControl
50+
> extends JsonFormsBaseRenderer<ControlElement> implements OnInit, OnDestroy {
51+
@Input() id: string;
52+
@Input() disabled: boolean;
53+
@Input() visible: boolean;
54+
55+
form: FormControl;
56+
subscription: Subscription;
57+
data: any;
58+
label: string;
59+
description: string;
60+
error: string | null;
61+
scopedSchema: JsonSchema;
62+
rootSchema: JsonSchema;
63+
enabled: boolean;
64+
hidden: boolean;
65+
66+
constructor(protected ngRedux: NgRedux<JsonFormsState>) {
67+
super();
68+
this.form = new FormControl(
69+
{
70+
value: '',
71+
disabled: true
72+
},
73+
{
74+
updateOn: 'change',
75+
validators: this.validator.bind(this)
76+
}
77+
);
78+
}
79+
80+
getEventValue = (event: any) => event.value;
81+
82+
onChange(ev: any) {
83+
const path = composeWithUi(this.uischema, this.path);
84+
this.ngRedux.dispatch(Actions.update(path, () => this.getEventValue(ev)));
85+
this.triggerValidation();
86+
}
87+
88+
ngOnInit() {
89+
this.subscription = this.ngRedux
90+
.select()
91+
.subscribe((state: JsonFormsState) => {
92+
const props = this.mapToProps(state);
93+
const {
94+
data,
95+
enabled,
96+
errors,
97+
label,
98+
required,
99+
schema,
100+
rootSchema,
101+
visible
102+
} = props;
103+
this.label = computeLabel(
104+
isPlainLabel(label) ? label : label.default,
105+
required
106+
);
107+
this.data = data;
108+
this.error = errors ? errors.join('\n') : null;
109+
this.enabled = enabled;
110+
this.enabled ? this.form.enable() : this.form.disable();
111+
this.hidden = !visible;
112+
this.scopedSchema = schema;
113+
this.rootSchema = rootSchema;
114+
this.description =
115+
this.scopedSchema !== undefined ? this.scopedSchema.description : '';
116+
this.id = props.id;
117+
this.form.setValue(data);
118+
this.mapAdditionalProps(props);
119+
});
120+
this.triggerValidation();
121+
}
122+
123+
validator: ValidatorFn = (_c: AbstractControl): ValidationErrors | null => {
124+
return this.error ? { error: this.error } : null;
125+
};
126+
127+
// @ts-ignore
128+
mapAdditionalProps(props: Props) {
129+
// do nothing by default
130+
}
131+
132+
ngOnDestroy() {
133+
if (this.subscription) {
134+
this.subscription.unsubscribe();
135+
}
136+
}
137+
138+
protected getOwnProps(): OwnPropsOfControl {
139+
const props: OwnPropsOfControl = {
140+
uischema: this.uischema,
141+
schema: this.schema,
142+
path: this.path,
143+
id: this.id
144+
};
145+
if (this.disabled !== undefined) {
146+
props.enabled = !this.disabled;
147+
}
148+
if (this.visible !== undefined) {
149+
props.visible = this.visible;
150+
}
151+
return props;
152+
}
153+
154+
protected abstract mapToProps(state: JsonFormsState): Props;
155+
156+
protected triggerValidation() {
157+
// these cause the correct update of the error underline, seems to be
158+
// related to ionic-team/ionic#11640
159+
this.form.markAsTouched();
160+
this.form.updateValueAndValidity();
161+
}
162+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2019 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import {
26+
ArrayControlProps,
27+
JsonFormsState,
28+
mapDispatchToArrayControlProps,
29+
mapStateToArrayControlProps
30+
} from '@jsonforms/core';
31+
import { OnDestroy, OnInit } from '@angular/core';
32+
import { JsonFormsAbstractControl } from './abstract-control';
33+
34+
export class JsonFormsArrayControl
35+
extends JsonFormsAbstractControl<ArrayControlProps>
36+
implements OnInit, OnDestroy {
37+
protected mapToProps(state: JsonFormsState): ArrayControlProps {
38+
const props = mapStateToArrayControlProps(state, this.getOwnProps());
39+
const dispatch = mapDispatchToArrayControlProps(this.ngRedux.dispatch);
40+
return { ...props, ...dispatch };
41+
}
42+
}

0 commit comments

Comments
 (0)