Hey folks, I started using JSONforms recently and got an interesting question.
I have an Angular app and use “@jsonforms/angular”: “3.2.0”, “@jsonforms/angular-material”: “3.2.0”, “@jsonforms/core”: “3.2.0”.
I need to have such functionality: two checkboxes - Enable and Required, when the Required is checked, Enable should be checked as well, and the same behaviour when Required is unchecked. The important thing is that I have to send a string (required or enabled), not a boolean.
I couldn’t find a normal way to do it, so I created a custom renderer where I added the two checkboxes like this
<input
type=“checkbox”
name=“checkbox_{{id}}”
#enabledCheckbox
(change)=“onCheckboxChanged(FORM_FIELD_STATE.ENABLED)”
[id]=“id”
[disabled]=“!isEnabled()”/>
export class FormFieldsComponent extends ToggleControlRenderer {
@ViewChild(‘enabledCheckbox’, {static: true}) enabledCheckbox: ElementRef;
@ViewChild(‘requiredCheckbox’, {static: true}) requiredCheckbox: ElementRef;
readonly FORM_FIELD_STATE = FORM_FIELD_STATE;
constructor(
private jsonformsService: JsonFormsAngularService,
changeDetectorRef: ChangeDetectorRef
) {
super(jsonformsService, changeDetectorRef);
}
ngOnInit() {
super.ngOnInit();
if (this.data === FORM_FIELD_STATE.ENABLED) {
this.enabledCheckbox.nativeElement.checked = true;
this.form.setValue(FORM_FIELD_STATE.ENABLED);
}
if (this.data === FORM_FIELD_STATE.REQUIRED) {
this.enabledCheckbox.nativeElement.checked = true;
this.requiredCheckbox.nativeElement.checked = true;
this.form.setValue(FORM_FIELD_STATE.REQUIRED);
}
}
onCheckboxChanged(state: FORM_FIELD_STATE) {
if (state === FORM_FIELD_STATE.REQUIRED) {
this.enabledCheckbox.nativeElement.checked = this.requiredCheckbox.nativeElement.checked;
}
this.data = FORM_FIELD_STATE.ENABLED;
this.form.setValue(FORM_FIELD_STATE.ENABLED);
this.jsonformsService.updateCore(
Actions.update(this.propsPath, () => FORM_FIELD_STATE.ENABLED)
);
this.jsonformsService.setData(FORM_FIELD_STATE.ENABLED);
}
}
I just hardcoded ‘enabled’ to check if the data will be updated, but it doesn’t update.
How can I update the data from the Custom Renderer?
Thanks in advance