Skip to content

Commit c568d6a

Browse files
committed
[NAE-1745] Autocomplete new filter "include"
- implement
1 parent 67bd507 commit c568d6a

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

projects/netgrif-components-core/src/lib/data-fields/enumeration-field/enumeration-autocomplete-select-field/abstract-enumeration-autocomplete-select-field.component.ts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {EnumerationField, EnumerationFieldValidation, EnumerationFieldValue} fro
66
import {WrappedBoolean} from '../../data-field-template/models/wrapped-boolean';
77
import {TranslateService} from '@ngx-translate/core';
88
import {MatAutocompleteSelectedEvent} from '@angular/material/autocomplete';
9+
import {MultichoiceFieldValue} from '../../multichoice-field/models/multichoice-field';
910

1011
@Component({
1112
selector: 'ncc-abstract-enumeration-autocomplete-field',
@@ -44,18 +45,6 @@ export abstract class AbstractEnumerationAutocompleteSelectFieldComponent implem
4445
this.filteredOptions = undefined;
4546
}
4647

47-
/**
48-
* Function to filter out matchless options without accent and case-sensitive differences
49-
* @param value to compare matching options
50-
* @return return matched options
51-
*/
52-
private _filter(value: string): Array<EnumerationFieldValue> {
53-
const filterValue = value?.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '');
54-
55-
return this.enumerationField.choices.filter(option => option.value.toLowerCase().normalize('NFD')
56-
.replace(/[\u0300-\u036f]/g, '').indexOf(filterValue) === 0);
57-
}
58-
5948
change() {
6049
if (this.text.value !== undefined) {
6150
this.filteredOptions = of(this._filter(this.text.value));
@@ -71,6 +60,50 @@ export abstract class AbstractEnumerationAutocompleteSelectFieldComponent implem
7160
return !this.formControlRef.disabled && !this.formControlRef.valid && this.text.control.touched;
7261
}
7362

63+
protected checkPropertyInComponent(property: string): boolean {
64+
return !!this.enumerationField.component && !!this.enumerationField.component.properties && property in this.enumerationField.component.properties;
65+
}
66+
67+
protected filterType(): string | undefined {
68+
if (this.checkPropertyInComponent('filter')) {
69+
return this.enumerationField.component.properties.filter;
70+
}
71+
}
72+
73+
protected _filter(value: string): Array<EnumerationFieldValue> {
74+
let filterType = this.filterType().toLowerCase()
75+
switch (filterType) {
76+
case "include":
77+
return this._filterInclude(value);
78+
case "indexof":
79+
return this._filterIndexOf(value);
80+
default:
81+
return this._filterIndexOf(value);
82+
}
83+
}
84+
85+
protected _filterInclude(value: string): Array<EnumerationFieldValue> {
86+
const filterValue = value?.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '');
87+
return this.enumerationField.choices.filter(option =>
88+
option.value.toLowerCase()
89+
.normalize('NFD')
90+
.replace(/[\u0300-\u036f]/g, '')
91+
.includes(filterValue));
92+
}
93+
94+
95+
/**
96+
* Function to filter out matchless options without accent and case-sensitive differences
97+
* @param value to compare matching options
98+
* @return return matched options
99+
*/
100+
private _filterIndexOf(value: string): Array<EnumerationFieldValue> {
101+
const filterValue = value?.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '');
102+
103+
return this.enumerationField.choices.filter(option => option.value.toLowerCase().normalize('NFD')
104+
.replace(/[\u0300-\u036f]/g, '').indexOf(filterValue) === 0);
105+
}
106+
74107
public renderSelection = (key) => {
75108
if (key !== undefined && key !== '' && key !== null) {
76109
if (this.enumerationField.choices.find(choice => choice.key === key)) {

projects/netgrif-components-core/src/lib/data-fields/multichoice-field/multichoice-autocomplete-field/abstract-multichoice-autocomplete-field-component.component.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,37 @@ export abstract class AbstractMultichoiceAutocompleteFieldComponentComponent imp
6565
}
6666
}
6767

68-
private _filter(value: string): Array<MultichoiceFieldValue> {
68+
protected checkPropertyInComponent(property: string): boolean {
69+
return !!this.multichoiceField.component && !!this.multichoiceField.component.properties && property in this.multichoiceField.component.properties;
70+
}
71+
72+
protected filterType(): string | undefined {
73+
if (this.checkPropertyInComponent('filter')) {
74+
return this.multichoiceField.component.properties.filter;
75+
}
76+
}
77+
78+
protected _filter(value: string): Array<MultichoiceFieldValue> {
79+
let filterType = this.filterType().toLowerCase()
80+
switch (filterType) {
81+
case "include":
82+
return this._filterInclude(value);
83+
case "indexof":
84+
return this._filterIndexOf(value);
85+
default:
86+
return this._filterIndexOf(value);
87+
}
88+
}
89+
90+
protected _filterInclude(value: string): Array<MultichoiceFieldValue> {
91+
if (Array.isArray(value)) {
92+
value = '';
93+
}
94+
const filterValue = value?.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '');
95+
return this.multichoiceField.choices.filter(option => option.value.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '').includes(filterValue));
96+
}
97+
98+
protected _filterIndexOf(value: string): Array<MultichoiceFieldValue> {
6999
if (Array.isArray(value)) {
70100
value = '';
71101
}

0 commit comments

Comments
 (0)