mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-5143] Fix Checkbox widget value parsing (#5706)
This commit is contained in:
@@ -5,8 +5,8 @@
|
|||||||
color="primary"
|
color="primary"
|
||||||
[required]="field.required"
|
[required]="field.required"
|
||||||
[disabled]="field.readOnly || readOnly"
|
[disabled]="field.readOnly || readOnly"
|
||||||
[(ngModel)]="checkboxValue"
|
[(ngModel)]="field.value"
|
||||||
(ngModelChange)="onCheckboxToggled()">
|
(ngModelChange)="onFieldChanged(field)">
|
||||||
{{field.name | translate }}
|
{{field.name | translate }}
|
||||||
<span *ngIf="field.required">*</span>
|
<span *ngIf="field.required">*</span>
|
||||||
</mat-checkbox>
|
</mat-checkbox>
|
||||||
|
@@ -80,51 +80,16 @@ describe('CheckboxWidgetComponent', () => {
|
|||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
const checkbox = fixture.debugElement.nativeElement.querySelector('mat-checkbox input');
|
const checkbox = fixture.debugElement.nativeElement.querySelector('mat-checkbox input');
|
||||||
expect(checkbox.getAttribute('aria-checked')).toBe('true');
|
expect(checkbox.getAttribute('aria-checked')).toBe('true');
|
||||||
expect(widget.checkboxValue).toBe(true);
|
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should be checked if string "true" is passed', async(() => {
|
it('should not be checked if false is passed', async(() => {
|
||||||
widget.field.value = 'true';
|
|
||||||
fixture.detectChanges();
|
|
||||||
fixture.whenStable().then(() => {
|
|
||||||
fixture.detectChanges();
|
|
||||||
const checkbox = fixture.debugElement.nativeElement.querySelector('mat-checkbox input');
|
|
||||||
expect(checkbox.getAttribute('aria-checked')).toBe('true');
|
|
||||||
expect(widget.checkboxValue).toBe(true);
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should not be checked if boolean false is passed', async(() => {
|
|
||||||
widget.field.value = false;
|
widget.field.value = false;
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
fixture.whenStable().then(() => {
|
fixture.whenStable().then(() => {
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
const checkbox = fixture.debugElement.nativeElement.querySelector('mat-checkbox input');
|
const checkbox = fixture.debugElement.nativeElement.querySelector('mat-checkbox input');
|
||||||
expect(checkbox.getAttribute('aria-checked')).toBe('false');
|
expect(checkbox.getAttribute('aria-checked')).toBe('false');
|
||||||
expect(widget.checkboxValue).toBe(false);
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should not be checked if string "false" is passed', async(() => {
|
|
||||||
widget.field.value = 'false';
|
|
||||||
fixture.detectChanges();
|
|
||||||
fixture.whenStable().then(() => {
|
|
||||||
fixture.detectChanges();
|
|
||||||
const checkbox = fixture.debugElement.nativeElement.querySelector('mat-checkbox input');
|
|
||||||
expect(checkbox.getAttribute('aria-checked')).toBe('false');
|
|
||||||
expect(widget.checkboxValue).toBe(false);
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should not be checked if null is passed', async(() => {
|
|
||||||
widget.field.value = null;
|
|
||||||
fixture.detectChanges();
|
|
||||||
fixture.whenStable().then(() => {
|
|
||||||
fixture.detectChanges();
|
|
||||||
const checkbox = fixture.debugElement.nativeElement.querySelector('mat-checkbox input');
|
|
||||||
expect(checkbox.getAttribute('aria-checked')).toBe('false');
|
|
||||||
expect(widget.checkboxValue).toBe(false);
|
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
/* tslint:disable:component-selector no-input-rename */
|
/* tslint:disable:component-selector no-input-rename */
|
||||||
|
|
||||||
import { Component, ViewEncapsulation, OnInit } from '@angular/core';
|
import { Component, ViewEncapsulation } from '@angular/core';
|
||||||
import { FormService } from './../../../services/form.service';
|
import { FormService } from './../../../services/form.service';
|
||||||
import { baseHost, WidgetComponent } from './../widget.component';
|
import { baseHost, WidgetComponent } from './../widget.component';
|
||||||
|
|
||||||
@@ -27,23 +27,10 @@ import { baseHost, WidgetComponent } from './../widget.component';
|
|||||||
host: baseHost,
|
host: baseHost,
|
||||||
encapsulation: ViewEncapsulation.None
|
encapsulation: ViewEncapsulation.None
|
||||||
})
|
})
|
||||||
export class CheckboxWidgetComponent extends WidgetComponent implements OnInit {
|
export class CheckboxWidgetComponent extends WidgetComponent {
|
||||||
checkboxValue: boolean;
|
checkboxValue: boolean;
|
||||||
|
|
||||||
constructor(public formService: FormService) {
|
constructor(public formService: FormService) {
|
||||||
super(formService);
|
super(formService);
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
|
||||||
this.checkboxValue = this.parseValue(this.field.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
parseValue(value: any) {
|
|
||||||
return value === true || value === 'true';
|
|
||||||
}
|
|
||||||
|
|
||||||
onCheckboxToggled() {
|
|
||||||
this.field.value = this.checkboxValue;
|
|
||||||
this.onFieldChanged(this.field);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -284,6 +284,30 @@ describe('FormFieldModel', () => {
|
|||||||
expect(field.value).toBe('deferred-radio');
|
expect(field.value).toBe('deferred-radio');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should parse boolean value when set to "true"', () => {
|
||||||
|
const field = new FormFieldModel(new FormModel(), {
|
||||||
|
type: FormFieldTypes.BOOLEAN,
|
||||||
|
value: 'true'
|
||||||
|
});
|
||||||
|
expect(field.value).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse boolean value when set to "false"', () => {
|
||||||
|
const field = new FormFieldModel(new FormModel(), {
|
||||||
|
type: FormFieldTypes.BOOLEAN,
|
||||||
|
value: 'false'
|
||||||
|
});
|
||||||
|
expect(field.value).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse boolean value to false when set to null', () => {
|
||||||
|
const field = new FormFieldModel(new FormModel(), {
|
||||||
|
type: FormFieldTypes.BOOLEAN,
|
||||||
|
value: null
|
||||||
|
});
|
||||||
|
expect(field.value).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it('should update form with empty dropdown value', () => {
|
it('should update form with empty dropdown value', () => {
|
||||||
const form = new FormModel();
|
const form = new FormModel();
|
||||||
const field = new FormFieldModel(form, {
|
const field = new FormFieldModel(form, {
|
||||||
|
@@ -318,6 +318,10 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (json.type === FormFieldTypes.BOOLEAN) {
|
||||||
|
value = json.value === 'true' || json.value === true ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user