dropdown form field return object

This commit is contained in:
eromano
2025-05-15 11:37:53 +02:00
parent cf9d296d0f
commit e3a1cd1202
5 changed files with 42 additions and 8 deletions

View File

@@ -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' });
});
});
});

View File

@@ -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) {

View File

@@ -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',

View File

@@ -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 {

View File

@@ -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;
});