Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
fix(filter-field): Fixes freeTexts set as defaultSearch and unique.
Browse files Browse the repository at this point in the history
  • Loading branch information
rowa-audil authored and tomheller committed Mar 5, 2021
1 parent 73bdeea commit 00532db
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions apps/dev/src/filter-field/testdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const TEST_DATA = {
name: 'DE',
defaultSearch: true,
suggestions: [{ name: 'Berlin' }, { name: 'Bremen' }, { name: 'Munich' }],
unique: true,
validators: [
{
validatorFn: Validators.minLength(2),
Expand Down
33 changes: 33 additions & 0 deletions libs/barista-components/filter-field/src/filter-field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
TEST_DATA_RANGE,
TEST_DATA_PLACEHOLDER,
TEST_DATA_KEYBOARD_NAVIGATION,
TEST_DEFAULT_SEARCH_UNIQUE,
} from './testing/filter-field-test-data';
import {
TestApp,
Expand Down Expand Up @@ -1213,6 +1214,38 @@ describe('DtFilterField', () => {
});
});

describe('default search', () => {
it('should not show the default search option when it is unique and used', fakeAsync(() => {
fixture.componentInstance.dataSource.data = TEST_DEFAULT_SEARCH_UNIQUE;
fixture.detectChanges();
advanceFilterfieldCycle();

filterField.focus();
advanceFilterfieldCycle();

let options = getOptions(overlayContainerElement);
expect(options[0].innerHTML.includes('DE')).toBeTruthy();

options[0].click();
advanceFilterfieldCycle();

options = getOptions(overlayContainerElement);

zone.simulateZoneExit();

options[0].click();
advanceFilterfieldCycle();

expect(filterField.filters.length).toBe(1);

options = getOptions(overlayContainerElement);

zone.simulateZoneExit();

expect(options[0].innerHTML.includes('DE')).toBeFalsy();
}));
});

describe('programmatic setting', () => {
beforeEach(() => {
fixture.componentInstance.dataSource.data = TEST_DATA_EDITMODE;
Expand Down
19 changes: 13 additions & 6 deletions libs/barista-components/filter-field/src/filter-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import { DtFilterFieldRangeTrigger } from './filter-field-range/filter-field-ran
import { DtFilterFieldTag } from './filter-field-tag/filter-field-tag';
import {
applyDtOptionIds,
defUniquePredicate,
filterAutocompleteDef,
filterFreeTextDef,
filterMultiSelectDef,
Expand Down Expand Up @@ -1727,12 +1728,18 @@ export class DtFilterField<T = any>
!isAsyncDtAutocompleteDef(currentDef) &&
this._inputValue.length
) {
if (this._defaultSearchDef === null) {
this._defaultSearchDef = findDefaultSearch(currentDef);
this._updateControl();
this._writeControlValue(this._inputValue);
} else {
this._defaultSearchDef = findDefaultSearch(currentDef);
const defaultSearchDef = findDefaultSearch(currentDef);
if (
defaultSearchDef &&
defUniquePredicate(defaultSearchDef, this._getSelectedOptionIds())
) {
if (this._defaultSearchDef === null) {
this._defaultSearchDef = defaultSearchDef;
this._updateControl();
this._writeControlValue(this._inputValue);
} else {
this._defaultSearchDef = findDefaultSearch(currentDef);
}
}
if (this._defaultSearchDef)
this._defaultSearchDef = filterFreeTextDef(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ export const TEST_DATA_EDITMODE = {
],
};

export const TEST_DEFAULT_SEARCH_UNIQUE = {
autocomplete: [
{
name: 'DE',
defaultSearch: true,
autocomplete: [{ name: 'Berlin' }],
unique: true,
},
{
name: 'AUT',
autocomplete: [{ name: 'Vienna' }],
},
],
};

export const TEST_DATA_KEYBOARD_NAVIGATION = {
autocomplete: [
{
Expand Down

0 comments on commit 00532db

Please sign in to comment.