Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,32 @@ describe('DsDynamicAutocompleteComponent test suite', () => {
autFixture.detectChanges();
flush();

expect(autComp.model.value).toEqual(modelValue.display);
expect(autComp.model.value).toEqual(modelValue.value);
expect(autComp.currentValue).toEqual(modelValue.value);
expect(autComp.change.emit).toHaveBeenCalled();
}));

it('should normalize object value to string on blur', () => {
spyOn(autComp.change, 'emit');
autComp.currentValue = { value: 'aig', display: 'Alumu-Tesu' };

autComp.onBlur(new Event('blur'));

expect(autComp.model.value).toEqual('aig');
expect(autComp.change.emit).toHaveBeenCalledWith('aig');
});

it('should format string values as-is', () => {
expect(autComp.formatter('aig')).toEqual('aig');
});

it('should format object values using value first', () => {
expect(autComp.formatter({ value: 'aig', display: 'Alumu-Tesu' })).toEqual('aig');
});

it('should format object values using display fallback', () => {
expect(autComp.formatter({ display: 'Alumu-Tesu' })).toEqual('Alumu-Tesu');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class DsDynamicAutocompleteComponent extends DsDynamicTagComponent implem
* @param event
*/
onBlur(event: Event) {
this.dispatchUpdate(this.currentValue);
this.dispatchUpdate(this.normalizeValue(this.currentValue));
this.cdr.detectChanges();
}

Expand All @@ -148,7 +148,9 @@ export class DsDynamicAutocompleteComponent extends DsDynamicTagComponent implem
updateValue.value = this.handlePrefix.value + handle_title[0];
}

this.dispatchUpdate(updateValue.display);
const selectedValue = this.normalizeValue(updateValue);
this.currentValue = selectedValue;
this.dispatchUpdate(selectedValue);
}

/**
Expand All @@ -170,29 +172,37 @@ export class DsDynamicAutocompleteComponent extends DsDynamicTagComponent implem
if (init) {
this.getInitValueFromModel()
.subscribe((formValue: FormFieldMetadataValueObject) => {
this.currentValue = formValue;
this.currentValue = this.normalizeValue(formValue);
this.cdr.detectChanges();
});
} else {
if (isEmpty(value)) {
result = '';
} else {
result = value.value;
}
result = isEmpty(value) ? '' : this.normalizeValue(value);

this.currentValue = result;
this.cdr.detectChanges();
}
}

/**
* Do not show whole suggestion object but just display value.
* @param x
* Formats input values for the typeahead.
* If x is a string (persisted ISO), return it as-is.
* If x is an object, return value first and fallback to display.
* @param x raw string/object value from model/typeahead
*/
formatter = (x: { display: string }) => {
return x.display;
formatter = (x: string | { value?: string; display?: string }) => {
return this.normalizeValue(x);
};

/**
* Normalize autocomplete values to string.
*/
private normalizeValue(value: string | { value?: string; display?: string }): string {
if (typeof value === 'string') {
return value;
}
return value?.value || value?.display || '';
}

/**
* Pretify suggestion.
* @param suggestion
Expand Down