dropdown form field return object

This commit is contained in:
eromano
2025-05-15 12:57:01 +02:00
parent e3a1cd1202
commit 8a17d7fcf6
4 changed files with 28 additions and 4 deletions

View File

@@ -785,7 +785,7 @@ describe('FormFieldModel', () => {
]; ];
}); });
it('should update form with selected option and options from which we chose', () => { it('should update form with selected option and options from which we chose when is a strign', () => {
field.value = 'restOpt2'; field.value = 'restOpt2';
field.updateForm(); field.updateForm();
@@ -1046,6 +1046,15 @@ describe('FormFieldModel', () => {
expect(field.value).toEqual('opt2'); expect(field.value).toEqual('opt2');
expect(field.form.values['dropdown_field']).toEqual({ id: 'opt2', name: 'Option 2' }); expect(field.form.values['dropdown_field']).toEqual({ id: 'opt2', name: 'Option 2' });
}); });
it('should selected option appear in form values', () => {
const field = getFieldConfig('manual', staticOptions, { id: 'opt3', name: 'opt3' });
field.updateForm();
expect(field.value).toEqual('opt2');
expect(field.form.values['dropdown_field']).toEqual({ id: 'opt2', name: 'Option 2' });
});
}); });
describe('radio buttons field', () => { describe('radio buttons field', () => {

View File

@@ -436,6 +436,17 @@ export class FormFieldModel extends FormWidgetModel {
this.form.values[this.id] = matchingOption || null; this.form.values[this.id] = matchingOption || null;
} }
if (typeof this.value === 'object') {
if (this.value.id === 'empty' || this.value.id === '') {
this.form.values[this.id] = null;
break;
}
const matchingOption: FormFieldOption = this.options.find((opt) => opt.id === this.value.id);
this.form.values[this.id] = matchingOption || null;
}
break; break;
} }
case FormFieldTypes.RADIO_BUTTONS: { case FormFieldTypes.RADIO_BUTTONS: {

View File

@@ -196,7 +196,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
private setFormControlValue(): void { private setFormControlValue(): void {
if (this.field?.value && typeof this.field?.value === 'object') { if (this.field?.value && typeof this.field?.value === 'object') {
this.dropdownControl.setValue(this.field?.value, { emitEvent: false }); this.dropdownControl.setValue({ id: this.field?.value.id, name: this.field?.value.name }, { emitEvent: false });
} else { } else {
this.dropdownControl.setValue({ id: this.field?.value, name: '' }, { emitEvent: false }); this.dropdownControl.setValue({ id: this.field?.value, name: '' }, { emitEvent: false });
} }
@@ -472,10 +472,14 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
const optionIdList = [...this.field.options].map((option) => option.id); const optionIdList = [...this.field.options].map((option) => option.id);
const fieldValueIds = this.field.value.map((valueOption) => valueOption.id); const fieldValueIds = this.field.value.map((valueOption) => valueOption.id);
return fieldValueIds.every((valueOptionId) => optionIdList.includes(valueOptionId)); return fieldValueIds.every((valueOptionId) => optionIdList.includes(valueOptionId));
} else {
if (this.field?.value && typeof this.field?.value === 'object') {
return [...this.field.options].map((option) => option.id).includes(this.field.value.id);
} else { } else {
return [...this.field.options].map((option) => option.id).includes(this.field.value); return [...this.field.options].map((option) => option.id).includes(this.field.value);
} }
} }
}
private hasRuleEntries(): boolean { private hasRuleEntries(): boolean {
return !!this.field.rule.entries.length; return !!this.field.rule.entries.length;

View File

@@ -29,7 +29,7 @@ export const defaultValueValidator =
}); });
const isSomeOptionSelected = optionsWithNoDefaultValue.some((dropdownOption) => { const isSomeOptionSelected = optionsWithNoDefaultValue.some((dropdownOption) => {
const isOptionSelected = dropdownOption.id === control.value.id; const isOptionSelected = dropdownOption.id === control.value?.id;
return isOptionSelected; return isOptionSelected;
}); });