diff --git a/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts b/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts index 27e97de0dc..8fe1816c96 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts @@ -146,7 +146,7 @@ describe('FormFieldModel', () => { }); expect(field.options).toEqual([{ id: 'id_one', name: 'One' }]); - expect(field.value).toEqual('id_one'); + expect(field.value).toEqual([{ id: 'id_one', name: 'One' }]); }); it('should add value (selected options) to field options if NOT present (multiple selection)', () => { @@ -176,7 +176,22 @@ describe('FormFieldModel', () => { expect(field.hasEmptyValue).toBe(true); expect(field.emptyOption).toEqual({ id: 'empty', name: 'Chose one...' }); - expect(field.value).toEqual('empty'); + expect(field.value).toEqual({ id: 'empty', name: 'Chose one...' }); + }); + + it('should assign "empty" option as value if value is null and "empty" option is present in options', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.DROPDOWN, + options: [ + { id: '', name: 'Chose one...' }, + { id: 'one', name: 'One' } + ], + value: null + }); + + expect(field.hasEmptyValue).toBe(true); + expect(field.emptyOption).toEqual({ id: '', name: 'Chose one...' }); + expect(field.value).toEqual({ id: '', name: 'Chose one...' }); }); it('should set hasEmptyValue to true if "empty" option is present in options', () => { @@ -272,7 +287,7 @@ describe('FormFieldModel', () => { options: [], value: { id: 'delayed-option-id', name: 'Delayed option' } }); - expect(field.value).toBe('delayed-option-id'); + expect(field.value).toBe({ id: 'delayed-option-id', name: 'Delayed option' }); }); }); }); diff --git a/lib/core/src/lib/form/components/widgets/core/form-field.model.ts b/lib/core/src/lib/form/components/widgets/core/form-field.model.ts index 89d2fc6a06..3c60ebdb91 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field.model.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field.model.ts @@ -331,13 +331,13 @@ export class FormFieldModel extends FormWidgetModel { const isEmptyValue = !value || [this.emptyOption.id, this.emptyOption.name].includes(value); if (isEmptyValue) { - return this.emptyOption.id; + return this.emptyOption; } } if (this.isValidOption(value)) { this.addOption({ id: value.id, name: value.name }); - return value.id; + return value; } if (this.hasMultipleValues) { diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts index 4bb92b9483..12642729b1 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts @@ -983,7 +983,7 @@ describe('DropdownCloudWidgetComponent', () => { expect(widget.field.options.length).toEqual(0); }; - it('should set dropdownControl value without emitting events', () => { + it('should set dropdownControl value without emitting events if the mapping is a string', () => { widget.field = { value: 'testValue', options: [], @@ -997,6 +997,20 @@ describe('DropdownCloudWidgetComponent', () => { expect(widget.dropdownControl.value).toEqual({ id: 'testValue', name: '' }); }); + it('should set dropdownControl value without emitting events if is an object', () => { + widget.field = { + value: { id: 'testValueObj', name: 'testValueObjName' }, + options: [], + isVisible: true + } as any; // Mock field + spyOn(widget.dropdownControl, 'setValue').and.callThrough(); + + widget['setFormControlValue'](); + + expect(widget.dropdownControl.setValue).toHaveBeenCalledWith({ id: 'testValueObj', name: 'testValueObjName' }, { emitEvent: false }); + expect(widget.dropdownControl.value).toEqual({ id: 'testValue', name: '' }); + }); + it('should display options persisted from process variable', async () => { widget.field = getVariableDropdownWidget( 'variables.json-variable', diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts index 1ff67c8a4e..0b714193fa 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts @@ -195,7 +195,11 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI } private setFormControlValue(): void { - this.dropdownControl.setValue({ id: this.field?.value, name: '' }, { emitEvent: false }); + if (this.field?.value && typeof this.field?.value === 'object') { + this.dropdownControl.setValue(this.field?.value, { emitEvent: false }); + } else { + this.dropdownControl.setValue({ id: this.field?.value, name: '' }, { emitEvent: false }); + } } private updateFormControlState(): void { diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/validators.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/validators.ts index d3f153a08b..ed8b1fe52c 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/validators.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/validators.ts @@ -29,7 +29,8 @@ export const defaultValueValidator = }); const isSomeOptionSelected = optionsWithNoDefaultValue.some((dropdownOption) => { - const isOptionSelected = dropdownOption.id === control.value?.id; + const isOptionSelected = dropdownOption.id === control.value.id; + return isOptionSelected; });