mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
AAE-24007 Display radio buttons and dropdown options depending on optionType (#9975)
* AAE-24007 Update form-field.moel * AAE-24007 Update dropdown-cloud.widget * AAE-24007 Update radio-buttons-cloud.widget * AAE-24007 Dropdown aligment (undefined value should be reserved for empty option) * AAE-24007 Minor update * AAE-24007 Update tests * AAE-24007 Update * AAE-24007 Update * AAE-24007 Update * AAE-24007 Fix units * AAE-24007 Fix * AAE-24007 Align standard dropdown and radio
This commit is contained in:
committed by
GitHub
parent
3406341859
commit
cb0b072b1e
@@ -75,15 +75,21 @@ describe('DropdownCloudWidgetComponent', () => {
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should require field with restUrl', () => {
|
||||
spyOn(formCloudService, 'getRestWidgetData');
|
||||
widget.field = new FormFieldModel(new FormModel());
|
||||
it('should call rest api only when url is present and field type is rest', () => {
|
||||
const getFieldConfig = (optionType: string, restUrl: string) => new FormFieldModel(new FormModel(), { optionType, restUrl });
|
||||
spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of(fakeOptionList));
|
||||
|
||||
widget.field = getFieldConfig('manual', 'fake-rest-url');
|
||||
widget.ngOnInit();
|
||||
expect(formCloudService.getRestWidgetData).not.toHaveBeenCalled();
|
||||
|
||||
widget.field = new FormFieldModel(null, { restUrl: null });
|
||||
widget.field = getFieldConfig('rest', null);
|
||||
widget.ngOnInit();
|
||||
expect(formCloudService.getRestWidgetData).not.toHaveBeenCalled();
|
||||
|
||||
widget.field = getFieldConfig('rest', 'fake-rest-url');
|
||||
widget.ngOnInit();
|
||||
expect(formCloudService.getRestWidgetData).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should select the default value when an option is chosen as default', async () => {
|
||||
|
@@ -86,7 +86,8 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
switch (true) {
|
||||
case this.isReadOnlyForm():
|
||||
break;
|
||||
case this.hasRestUrl() && !this.isLinkedWidget():
|
||||
|
||||
case this.isValidRestConfig() && !this.isLinkedWidget():
|
||||
this.persistFieldOptionsFromRestApi();
|
||||
break;
|
||||
|
||||
@@ -192,8 +193,12 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
return this.field?.optionType === 'variable';
|
||||
}
|
||||
|
||||
private isRestOptionType(): boolean {
|
||||
return this.field?.optionType === 'rest';
|
||||
}
|
||||
|
||||
private persistFieldOptionsFromRestApi() {
|
||||
if (this.isValidRestType()) {
|
||||
if (this.isValidRestConfig()) {
|
||||
this.resetRestApiErrorMessage();
|
||||
const bodyParam = this.buildBodyParam();
|
||||
this.formCloudService
|
||||
@@ -248,7 +253,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
|
||||
private parentValueChanged(value: string) {
|
||||
if (value && !this.isNoneValueSelected(value)) {
|
||||
this.isValidRestType() ? this.persistFieldOptionsFromRestApi() : this.persistFieldOptionsFromManualList(value);
|
||||
this.isValidRestConfig() ? this.persistFieldOptionsFromRestApi() : this.persistFieldOptionsFromManualList(value);
|
||||
} else if (this.isNoneValueSelected(value)) {
|
||||
this.resetRestApiErrorMessage();
|
||||
this.resetOptions();
|
||||
@@ -378,8 +383,8 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
return optionValue;
|
||||
}
|
||||
|
||||
private isValidRestType(): boolean {
|
||||
return this.field.optionType === 'rest' && this.hasRestUrl();
|
||||
private isValidRestConfig(): boolean {
|
||||
return this.isRestOptionType() && this.hasRestUrl();
|
||||
}
|
||||
|
||||
private setPreviewState(): void {
|
||||
@@ -401,7 +406,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
}
|
||||
|
||||
private hasRestUrl(): boolean {
|
||||
return !!this.field.restUrl;
|
||||
return !!this.field?.restUrl;
|
||||
}
|
||||
|
||||
isReadOnlyType(): boolean {
|
||||
|
@@ -65,6 +65,7 @@ describe('RadioButtonsCloudWidgetComponent', () => {
|
||||
|
||||
widget.field = new FormFieldModel(form, {
|
||||
id: fieldId,
|
||||
optionType: 'rest',
|
||||
restUrl: '<url>'
|
||||
});
|
||||
const field = widget.field;
|
||||
@@ -74,6 +75,23 @@ describe('RadioButtonsCloudWidgetComponent', () => {
|
||||
expect(field.updateForm).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call rest api only when url is present and field type is rest', () => {
|
||||
const getFieldConfig = (optionType: string, restUrl: string) => new FormFieldModel(new FormModel(), { optionType, restUrl });
|
||||
spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of([]));
|
||||
|
||||
widget.field = getFieldConfig('manual', 'fake-rest-url');
|
||||
widget.ngOnInit();
|
||||
expect(formCloudService.getRestWidgetData).not.toHaveBeenCalled();
|
||||
|
||||
widget.field = getFieldConfig('rest', null);
|
||||
widget.ngOnInit();
|
||||
expect(formCloudService.getRestWidgetData).not.toHaveBeenCalled();
|
||||
|
||||
widget.field = getFieldConfig('rest', 'fake-rest-url');
|
||||
widget.ngOnInit();
|
||||
expect(formCloudService.getRestWidgetData).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should update the field value when an option is selected', () => {
|
||||
spyOn(widget, 'onFieldChanged').and.stub();
|
||||
widget.onOptionClick('fake-opt');
|
||||
|
@@ -52,7 +52,7 @@ export class RadioButtonsCloudWidgetComponent extends WidgetComponent implements
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
if (this.field?.restUrl && !this.field?.form?.readOnly) {
|
||||
if (this.isValidRestConfig() && !this.isReadOnlyForm()) {
|
||||
this.getValuesFromRestApi();
|
||||
}
|
||||
}
|
||||
@@ -103,4 +103,20 @@ export class RadioButtonsCloudWidgetComponent extends WidgetComponent implements
|
||||
hasError(): ErrorMessageModel {
|
||||
return this.restApiError || this.field.validationSummary;
|
||||
}
|
||||
|
||||
private isRestType(): boolean {
|
||||
return this.field?.optionType === 'rest';
|
||||
}
|
||||
|
||||
private isReadOnlyForm(): boolean {
|
||||
return !!this.field?.form?.readOnly;
|
||||
}
|
||||
|
||||
private hasRestUrl(): boolean {
|
||||
return !!this.field?.restUrl;
|
||||
}
|
||||
|
||||
private isValidRestConfig(): boolean {
|
||||
return this.isRestType() && this.hasRestUrl();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user