Skip to content

Commit 21e7e7d

Browse files
authored
Merge pull request #447 from dannyhn/master
Add option for custom search function
2 parents 49166dd + 389a19f commit 21e7e7d

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

demo/src/app/dummy.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ <h3>Dropdown Demo</h3>
1010
With search and buttons:
1111
<ss-multiselect-dropdown [options]="data.options" [settings]="settings" [ngModel]="data.selectedItems"></ss-multiselect-dropdown>
1212
</p>
13+
14+
<p>
15+
With custom prefix search and buttons:
16+
<ss-multiselect-dropdown [options]="data.options" [settings]="settings" [ngModel]="data.selectedItems" [searchFunction]=prefixSearchFunction></ss-multiselect-dropdown>
17+
</p>
1318
</div>

demo/src/app/dummy.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ export class DummyComponent {
1717
showCheckAll: true,
1818
showUncheckAll: true
1919
};
20+
21+
prefixSearchFunction(str: string): RegExp {
22+
return new RegExp('^' + str, 'i');
23+
}
2024
}

src/dropdown/dropdown.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export class MultiselectDropdownComponent
6464
@Input() texts: IMultiSelectTexts;
6565
@Input() disabled: boolean = false;
6666
@Input() disabledSelection: boolean = false;
67+
@Input() searchFunction: (str: string) => RegExp = this._escapeRegExp;
6768

6869
@Output() selectionLimitReached = new EventEmitter();
6970
@Output() dropdownClosed = new EventEmitter();
@@ -277,7 +278,8 @@ export class MultiselectDropdownComponent
277278
options,
278279
value,
279280
this.settings.searchMaxLimit,
280-
this.settings.searchMaxRenderedItems
281+
this.settings.searchMaxRenderedItems,
282+
this.searchFunction
281283
);
282284
}
283285

@@ -709,4 +711,10 @@ export class MultiselectDropdownComponent
709711
e.stopPropagation();
710712
}
711713
}
714+
715+
private _escapeRegExp(str: string): RegExp {
716+
const regExpStr = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
717+
return new RegExp(regExpStr, 'i');
718+
}
719+
712720
}

src/dropdown/search-filter.pipe.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export class MultiSelectSearchFilter implements PipeTransform {
2020
options: IMultiSelectOption[],
2121
str = '',
2222
limit = 0,
23-
renderLimit = 0
23+
renderLimit = 0,
24+
searchFunction: (str: string) => RegExp,
2425
): IMultiSelectOption[] {
2526
str = str.toLowerCase();
2627

@@ -34,7 +35,7 @@ export class MultiSelectSearchFilter implements PipeTransform {
3435

3536
const filteredOpts = this._searchCache.hasOwnProperty(str)
3637
? this._searchCache[str]
37-
: this._doSearch(options, str, limit);
38+
: this._doSearch(options, str, limit, searchFunction);
3839

3940
const isUnderLimit = options.length <= limit;
4041

@@ -61,7 +62,7 @@ export class MultiSelectSearchFilter implements PipeTransform {
6162
return options;
6263
}
6364

64-
private _doSearch(options: IMultiSelectOption[], str: string, limit: number) {
65+
private _doSearch(options: IMultiSelectOption[], str: string, limit: number, searchFunction: (str: string) => RegExp) {
6566
const prevStr = str.slice(0, -1);
6667
const prevResults = this._searchCache[prevStr];
6768
const prevResultShift = this._prevSkippedItems[prevStr] || 0;
@@ -72,7 +73,7 @@ export class MultiSelectSearchFilter implements PipeTransform {
7273

7374
const optsLength = options.length;
7475
const maxFound = limit > 0 ? Math.min(limit, optsLength) : optsLength;
75-
const regexp = new RegExp(this._escapeRegExp(str), 'i');
76+
const regexp = searchFunction(str);
7677
const filteredOpts: IMultiSelectOption[] = [];
7778

7879
let i = 0, founded = 0, removedFromPrevResult = 0;
@@ -127,9 +128,4 @@ export class MultiSelectSearchFilter implements PipeTransform {
127128
private _limitRenderedItems<T>(items: T[], limit: number): T[] {
128129
return items.length > limit && limit > 0 ? items.slice(0, limit) : items;
129130
}
130-
131-
private _escapeRegExp(str: string): string {
132-
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
133-
}
134-
135131
}

0 commit comments

Comments
 (0)