Skip to content

Commit cdec5b8

Browse files
author
Plamena Radneva
committed
removed SearchResult interface, fixed the typeahead behvior
1 parent 86b6f22 commit cdec5b8

File tree

8 files changed

+62
-66
lines changed

8 files changed

+62
-66
lines changed

projects/ng-sq-ui/src/lib/form-elements/typeahead/typeahead.component.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
1111
(focus)="turnClickOutsideListenerOn()" placeholder="{{controlPlaceholder}}">
1212

1313
<div class="entered-items" *ngIf="selectedItems.size > 0">
14-
<div class="entered-item chosen-item" *ngFor="let selectedItem of selectedItems, index as itemIndex;">
15-
<div class="content">{{displayProp ? selectedItem[displayProp] : selectedItem}}</div>
14+
<div class="entered-item chosen-item"
15+
*ngFor="let selectedItem of selectedItems, index as itemIndex;">
16+
<div class="content">{{ selectedItem.label }}</div>
1617
<div class="remove" (click)="removeSearchResult(itemIndex)" *ngIf="!disabled">
1718
<i class="fa fa-times" aria-hidden="true"></i>
1819
</div>
1920
</div>
2021
</div>
2122

2223
<div class="options" [ngClass]="{'display-none': hideResults}">
23-
<div class="option" *ngFor="let result of searchResults" (click)="selectSearchResult(result)">
24-
{{displayProp ? result[displayProp] : result}}
24+
<div class="option" *ngFor="let result of options" (click)="selectSearchResult(result)">
25+
{{ result.label }}
2526
</div>
2627
</div>
2728
</div>

projects/ng-sq-ui/src/lib/form-elements/typeahead/typeahead.component.spec.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ describe('TypeaheadComponent', () => {
1010

1111
const testSearchResults = [
1212
{
13-
displayName: 'option1',
13+
label: 'option1',
1414
value: { key: 1 },
1515
},
1616
{
17-
displayName: 'option2',
17+
label: 'option2',
1818
value: { key: 2 },
1919
},
2020
{
21-
displayName: 'option3',
21+
label: 'option3',
2222
value: { id: '1234', key: 3 },
2323
},
2424
];
@@ -33,7 +33,7 @@ describe('TypeaheadComponent', () => {
3333
beforeEach(() => {
3434
fixture = TestBed.createComponent(TypeaheadComponent);
3535
component = fixture.componentInstance;
36-
component.displayProp = 'displayName';
36+
component.displayProp = '';
3737
fixture.detectChanges();
3838
});
3939

@@ -60,18 +60,11 @@ describe('TypeaheadComponent', () => {
6060
component.selectSearchResult(component.searchResults[0]);
6161
component.selectSearchResult(component.searchResults[1]);
6262

63-
console.log(
64-
!Object.is(
65-
component.selectedItems.get(0).value,
66-
component.value[0].value,
67-
),
68-
);
69-
63+
console.log(Object.is(component.selectedItems.get(0), component.value[0]));
7064
// the typeahead should return a new array with the copied search items
7165
expect(
7266
component.value.length === 1 &&
73-
!Object.is(component.selectedItems.get(0), component.value[0]) &&
74-
component.value[0].value.id === component.selectedItems.get(0).value.id,
67+
Object.is(component.selectedItems.get(0), component.value[0]),
7568
).toBe(true, 'single choice is correctly populated');
7669
});
7770

@@ -103,7 +96,7 @@ describe('TypeaheadComponent', () => {
10396
);
10497
});
10598

106-
it('#should be able to populate correctly with a pre-defined SearchResult[] when [multiple] = true', () => {
99+
it('#should be able to populate correctly with a pre-defined LabelValuePair[] when [multiple] = true', () => {
107100
component.multiple = true;
108101
component.value = testSearchResults;
109102
expect(component.selectedItems.toArray()).toEqual(
@@ -112,7 +105,7 @@ describe('TypeaheadComponent', () => {
112105
);
113106
});
114107

115-
it('#should be able to populate correctly with a pre-defined SearchResult[] when [multiple] = false', () => {
108+
it('#should be able to populate correctly with a pre-defined LabelValuePair[] when [multiple] = false', () => {
116109
component.multiple = false;
117110
component.value = testSearchResults;
118111
const itemsToArray = component.selectedItems.toArray();

projects/ng-sq-ui/src/lib/form-elements/typeahead/typeahead.component.ts

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import {
2-
Component,
3-
OnInit,
4-
Input,
5-
OnDestroy,
6-
forwardRef,
7-
ViewEncapsulation,
8-
OnChanges,
9-
Output,
10-
EventEmitter,
1+
import { Component, OnInit, Input, OnDestroy,
2+
forwardRef, ViewEncapsulation, OnChanges, Output,
3+
EventEmitter
114
} from '@angular/core';
125

136
import { NG_VALUE_ACCESSOR, FormControl } from '@angular/forms';
14-
import { SearchResult } from '../../shared/interfaces/search-result';
7+
import { LabelValuePair } from '../../shared/shared.module';
158
import { InputCoreComponent } from '../../shared/entities/input-core-component';
169

1710
import { Subject, Subscription } from 'rxjs';
@@ -34,23 +27,18 @@ const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR = {
3427
})
3528
export class TypeaheadComponent extends InputCoreComponent
3629
implements OnInit, OnDestroy, OnChanges {
37-
@Input()
38-
searchResults: SearchResult[] = [];
39-
@Input()
40-
multiple = false;
41-
@Input()
42-
delay = 500;
43-
@Input()
44-
displayProp = '';
45-
@Output()
46-
onUserInputEnd = new EventEmitter<string>();
30+
@Input() searchResults: any[] = [];
31+
@Input() multiple = false;
32+
@Input() delay = 500;
33+
@Input() displayProp = '';
34+
@Output() onUserInputEnd = new EventEmitter<string>();
4735

4836
private onInputValueChangeSubscription: Subscription;
4937
private onQueryInputControlSubscription: Subscription;
5038
private valueChangedSubscription: Subscription;
51-
private innerSelectedItemsListCopy: List<SearchResult>;
5239

53-
selectedItems: List<SearchResult> = List<SearchResult>();
40+
selectedItems: List<LabelValuePair> = List<LabelValuePair>();
41+
options: List<LabelValuePair> = List<LabelValuePair>();
5442

5543
constructor() {
5644
super();
@@ -104,6 +92,9 @@ export class TypeaheadComponent extends InputCoreComponent
10492

10593
ngOnChanges(changesObj) {
10694
if (changesObj.searchResults && changesObj.searchResults.currentValue) {
95+
const parsedResults= this.transformToLabelValuePairList(this.searchResults);
96+
this.options = List(parsedResults);
97+
10798
this.isLoading = false;
10899
this.hideResults = false;
109100
}
@@ -123,7 +114,7 @@ export class TypeaheadComponent extends InputCoreComponent
123114
}
124115
}
125116

126-
selectSearchResult(result: SearchResult) {
117+
selectSearchResult(result: LabelValuePair) {
127118
this.selectItem(result);
128119
}
129120

@@ -152,7 +143,7 @@ export class TypeaheadComponent extends InputCoreComponent
152143
this.value = [];
153144
}
154145

155-
private selectItem(result: SearchResult, copyResults: boolean = true) {
146+
private selectItem(result: LabelValuePair, copyResults: boolean = true) {
156147
this.queryInputControl.setValue(null);
157148

158149
if (!this.multiple && this.selectedItems.size === 1) {
@@ -173,23 +164,35 @@ export class TypeaheadComponent extends InputCoreComponent
173164
}
174165

175166
private copyResults() {
176-
this.innerSelectedItemsListCopy = this.selectedItems;
177-
this.value = this.copyObjectsToNewIterable(this.innerSelectedItemsListCopy);
167+
this.value = this.selectedItems.toArray();
178168
}
179169

180-
private copyObjectsToNewIterable(objList: List<SearchResult>) {
181-
const newList = objList.map((obj) => {
182-
let copiedSearchResult = {};
183-
184-
if (typeof obj === 'object') {
185-
copiedSearchResult = Object.assign({}, obj);
170+
private transformToLabelValuePairList(resultsList: any): Array<LabelValuePair> {
171+
const newList = resultsList.map(item => {
172+
let searchResult: LabelValuePair | any;
173+
174+
if (typeof item === 'object') {
175+
// if displayProp is an empty string,
176+
// it assumes that the author passes LabelValuePair items
177+
if (this.displayProp === '') {
178+
searchResult = Object.assign({}, item);
179+
} else {
180+
// in case the author wants a specific display property
181+
searchResult = {
182+
label: item[this.displayProp],
183+
value: Object.assign({}, item),
184+
};
185+
}
186186
} else {
187-
copiedSearchResult[this.displayProp] = obj;
187+
searchResult = {
188+
label: item,
189+
value: item,
190+
};
188191
}
189192

190-
return copiedSearchResult;
193+
return searchResult;
191194
});
192195

193-
return newList.toArray();
196+
return newList;
194197
}
195198
}

projects/ng-sq-ui/src/lib/shared/interfaces/search-result.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

projects/ng-sq-ui/src/lib/shared/shared.module.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { OutsideClickListenerDirective } from './directives/outside-click-listen
44
import { CustomEventBroadcasterService } from './services/custom-event-broadcaster.service';
55
import { OSDetectorService } from './services/os-detector.service';
66
import { LabelValuePair } from './interfaces/label-value-pair';
7-
import { SearchResult } from './interfaces/search-result';
87

9-
export { LabelValuePair, SearchResult };
8+
export { LabelValuePair };
109
export { OutsideClickListenerDirective };
1110
export { OSDetectorService };
1211

projects/ng-sq-ui/src/public_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
*/
44

55
export * from './lib/ng-sq-ui.module';
6-
export { LabelValuePair, SearchResult, OSDetectorService } from './lib/shared/shared.module';
6+
export { LabelValuePair, OSDetectorService } from './lib/shared/shared.module';

src/app/app.component.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ <h2>Introduction</h2>
2424
The font used in this preview is "Quicksand" and is applied using the Google Fonts API. The SQ-UI theme does not come with
2525
it and does not have a default font set.
2626
</p>
27+
28+
<p>
29+
<b>Note:</b> Clicking the Submit button will produce a console.log() with all the form data.
30+
</p>
2731
</section>
2832

2933
<section id="formElementsModule" class="module">
@@ -66,7 +70,7 @@ <h3>sq-tags-input</h3>
6670

6771
<section id="sqTypeahead1">
6872
<header>
69-
<h3>sq-typeahead with array of object</h3>
73+
<h3>sq-typeahead with array of objects - multiple choice</h3>
7074
</header>
7175

7276
<sq-typeahead name="typeahead1" displayProp="label" multiple="true" formControlName="typeahead1" [searchResults]="searchResults"
@@ -76,10 +80,10 @@ <h3>sq-typeahead with array of object</h3>
7680

7781
<section id="sqTypeahead2">
7882
<header>
79-
<h3>sq-typeahead with array of strings</h3>
83+
<h3>sq-typeahead with array of strings - single choice</h3>
8084
</header>
8185

82-
<sq-typeahead name="typeahead2" multiple="true" formControlName="typeahead2" [searchResults]="searchResultsStrings" (onUserInputEnd)="searchMethodString($event)"
86+
<sq-typeahead name="typeahead2" formControlName="typeahead2" [searchResults]="searchResultsStrings" (onUserInputEnd)="searchMethodString($event)"
8387
controlLabel="Typeahead*" controlPlaceholder="Type something in">
8488
</sq-typeahead>
8589
</section>

src/app/app.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component } from '@angular/core';
22
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
3-
import { LabelValuePair, SearchResult } from 'ng-sq-ui';
3+
import { LabelValuePair } from 'ng-sq-ui';
44

55
@Component({
66
selector: 'app-root',

0 commit comments

Comments
 (0)