AAE-34675 Fix default selection required (#10860)

* fix default selection required

* rename test

* dropdown form field return object

* dropdown form field return object

* dropdown form field return object

* fix test

* Update lib/core/src/lib/form/components/widgets/core/form-field.model.spec.ts

Co-authored-by: Ehsan Rezaei <ehsan.rezaei@hyland.com>

* Update lib/core/src/lib/form/components/widgets/core/form-field.model.ts

Co-authored-by: Ehsan Rezaei <ehsan.rezaei@hyland.com>

---------

Co-authored-by: Ehsan Rezaei <ehsan.rezaei@hyland.com>
This commit is contained in:
Eugenio Romano
2025-05-15 21:26:17 +02:00
committed by GitHub
parent 9c6a1901c6
commit b4eee9d631
7 changed files with 138 additions and 11 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,7 @@ 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 set hasEmptyValue to true if "empty" option is present in options', () => {
@@ -272,7 +272,8 @@ describe('FormFieldModel', () => {
options: [],
value: { id: 'delayed-option-id', name: 'Delayed option' }
});
expect(field.value).toBe('delayed-option-id');
expect(field.value).toEqual({ id: 'delayed-option-id', name: 'Delayed option' });
});
});
});
@@ -770,7 +771,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 string', () => {
field.value = 'restOpt2';
field.updateForm();
@@ -1023,7 +1024,7 @@ describe('FormFieldModel', () => {
expect(field.options).toEqual(staticOptions);
});
it('should selected option appear in form values', () => {
it('should selected option appear in form values string', () => {
const field = getFieldConfig('manual', staticOptions, 'opt2');
field.updateForm();
@@ -1031,6 +1032,15 @@ describe('FormFieldModel', () => {
expect(field.value).toEqual('opt2');
expect(field.form.values['dropdown_field']).toEqual({ id: 'opt2', name: 'Option 2' });
});
it('should selected option appear in form values obj', () => {
const field = getFieldConfig('manual', staticOptions, { id: 'opt3', name: 'opt3' });
field.updateForm();
expect(field.value).toEqual({ id: 'opt3', name: 'opt3' });
expect(field.form.values['dropdown_field']).toEqual({ id: 'opt3', name: 'opt3' });
});
});
describe('radio buttons field', () => {

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) {
@@ -436,6 +436,17 @@ export class FormFieldModel extends FormWidgetModel {
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;
}
break;
}
case FormFieldTypes.RADIO_BUTTONS: {