mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-09-17 14:21:29 +00:00
dropdown form field return object
This commit is contained in:
@@ -146,7 +146,7 @@ describe('FormFieldModel', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(field.options).toEqual([{ id: 'id_one', name: 'One' }]);
|
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)', () => {
|
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.hasEmptyValue).toBe(true);
|
||||||
expect(field.emptyOption).toEqual({ id: 'empty', name: 'Chose one...' });
|
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', () => {
|
it('should set hasEmptyValue to true if "empty" option is present in options', () => {
|
||||||
@@ -272,7 +287,7 @@ describe('FormFieldModel', () => {
|
|||||||
options: [],
|
options: [],
|
||||||
value: { id: 'delayed-option-id', name: 'Delayed option' }
|
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' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -331,13 +331,13 @@ export class FormFieldModel extends FormWidgetModel {
|
|||||||
|
|
||||||
const isEmptyValue = !value || [this.emptyOption.id, this.emptyOption.name].includes(value);
|
const isEmptyValue = !value || [this.emptyOption.id, this.emptyOption.name].includes(value);
|
||||||
if (isEmptyValue) {
|
if (isEmptyValue) {
|
||||||
return this.emptyOption.id;
|
return this.emptyOption;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isValidOption(value)) {
|
if (this.isValidOption(value)) {
|
||||||
this.addOption({ id: value.id, name: value.name });
|
this.addOption({ id: value.id, name: value.name });
|
||||||
return value.id;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.hasMultipleValues) {
|
if (this.hasMultipleValues) {
|
||||||
|
@@ -983,7 +983,7 @@ describe('DropdownCloudWidgetComponent', () => {
|
|||||||
expect(widget.field.options.length).toEqual(0);
|
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 = {
|
widget.field = {
|
||||||
value: 'testValue',
|
value: 'testValue',
|
||||||
options: [],
|
options: [],
|
||||||
@@ -997,6 +997,20 @@ describe('DropdownCloudWidgetComponent', () => {
|
|||||||
expect(widget.dropdownControl.value).toEqual({ id: 'testValue', name: '' });
|
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 () => {
|
it('should display options persisted from process variable', async () => {
|
||||||
widget.field = getVariableDropdownWidget(
|
widget.field = getVariableDropdownWidget(
|
||||||
'variables.json-variable',
|
'variables.json-variable',
|
||||||
|
@@ -195,8 +195,12 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
|||||||
}
|
}
|
||||||
|
|
||||||
private setFormControlValue(): void {
|
private setFormControlValue(): void {
|
||||||
|
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 });
|
this.dropdownControl.setValue({ id: this.field?.value, name: '' }, { emitEvent: false });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private updateFormControlState(): void {
|
private updateFormControlState(): void {
|
||||||
this.updateDropdownValidationRules();
|
this.updateDropdownValidationRules();
|
||||||
|
@@ -29,7 +29,8 @@ 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;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user