mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
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:
@@ -983,6 +983,34 @@ describe('DropdownCloudWidgetComponent', () => {
|
||||
expect(widget.field.options.length).toEqual(0);
|
||||
};
|
||||
|
||||
it('should set dropdownControl value without emitting events if the mapping is a string', () => {
|
||||
widget.field = {
|
||||
value: 'testValue',
|
||||
options: [],
|
||||
isVisible: true
|
||||
} as any; // Mock field
|
||||
spyOn(widget.dropdownControl, 'setValue').and.callThrough();
|
||||
|
||||
widget['setFormControlValue']();
|
||||
|
||||
expect(widget.dropdownControl.setValue).toHaveBeenCalledWith({ id: 'testValue', name: '' }, { emitEvent: false });
|
||||
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: 'testValueObj', name: 'testValueObjName' });
|
||||
});
|
||||
|
||||
it('should display options persisted from process variable', async () => {
|
||||
widget.field = getVariableDropdownWidget(
|
||||
'variables.json-variable',
|
||||
|
@@ -195,7 +195,15 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
}
|
||||
|
||||
private setFormControlValue(): void {
|
||||
this.dropdownControl.setValue(this.field?.value, { emitEvent: false });
|
||||
if (Array.isArray(this.field.value)) {
|
||||
this.dropdownControl.setValue(this.field?.value, { emitEvent: false });
|
||||
} else if (this.field?.value && typeof this.field?.value === 'object') {
|
||||
this.dropdownControl.setValue({ id: this.field?.value.id, name: this.field?.value.name }, { emitEvent: false });
|
||||
} else if (this.field.value === null) {
|
||||
this.dropdownControl.setValue(this.field?.value, { emitEvent: false });
|
||||
} else {
|
||||
this.dropdownControl.setValue({ id: this.field?.value, name: '' }, { emitEvent: false });
|
||||
}
|
||||
}
|
||||
|
||||
private updateFormControlState(): void {
|
||||
@@ -469,7 +477,11 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
const fieldValueIds = this.field.value.map((valueOption) => valueOption.id);
|
||||
return fieldValueIds.every((valueOptionId) => optionIdList.includes(valueOptionId));
|
||||
} else {
|
||||
return [...this.field.options].map((option) => option.id).includes(this.field.value);
|
||||
if (this.field?.value && typeof this.field?.value === 'object') {
|
||||
return [...this.field.options].map((option) => option.id).includes(this.field.value.id);
|
||||
} else {
|
||||
return [...this.field.options].map((option) => option.id).includes(this.field.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { FormFieldModel } from '@alfresco/adf-core';
|
||||
import { FormControl } from '@angular/forms';
|
||||
import { defaultValueValidator } from './validators';
|
||||
import { DEFAULT_OPTION } from './dropdown-cloud.widget';
|
||||
|
||||
describe('defaultValueValidator', () => {
|
||||
let mockField: FormFieldModel;
|
||||
|
||||
beforeEach(() => {
|
||||
mockField = new FormFieldModel(null, {
|
||||
options: [
|
||||
{ id: DEFAULT_OPTION.id, name: DEFAULT_OPTION.name },
|
||||
{ id: 'opt_1', name: 'Option 1' },
|
||||
{ id: 'opt_2', name: 'Option 2' }
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
it('should return null when a valid option is selected', () => {
|
||||
const validator = defaultValueValidator(mockField);
|
||||
const control = new FormControl({ id: 'opt_1' });
|
||||
|
||||
const result = validator(control);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return a required error when no valid option is selected', () => {
|
||||
const validator = defaultValueValidator(mockField);
|
||||
const control = new FormControl(null);
|
||||
|
||||
const result = validator(control);
|
||||
|
||||
expect(result).toEqual({ required: true });
|
||||
});
|
||||
|
||||
it('should return a required error when the default "choose one" option is selected', () => {
|
||||
const validator = defaultValueValidator(mockField);
|
||||
const control = new FormControl(DEFAULT_OPTION.id);
|
||||
|
||||
const result = validator(control);
|
||||
|
||||
expect(result).toEqual({ required: true });
|
||||
});
|
||||
});
|
@@ -30,6 +30,7 @@ export const defaultValueValidator =
|
||||
|
||||
const isSomeOptionSelected = optionsWithNoDefaultValue.some((dropdownOption) => {
|
||||
const isOptionSelected = dropdownOption.id === control.value?.id;
|
||||
|
||||
return isOptionSelected;
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user