@progress/kendo-angular-dropdowns
Version:
A wide variety of native Angular dropdown components including AutoComplete, ComboBox, DropDownList, DropDownTree, MultiColumnComboBox, MultiSelect, and MultiSelectTree
135 lines (134 loc) • 5.48 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { Directive, Input } from '@angular/core';
import { FilterableComponent } from './filterable-component';
import { getter, isArray, isPresent } from '../util';
import * as i0 from "@angular/core";
import * as i1 from "./filterable-component";
const DEFAULT_FILTER_SETTINGS = {
caseSensitive: false,
operator: 'startsWith'
};
/**
* Implements an event handler for the `filterChange` event of a DropDowns component
* which performs simple data filtering.
*
* @example
* ```ts
* _@Component({
* selector: 'my-app',
* template: `
* <kendo-autocomplete
* [data]="data"
* kendoDropDownFilter
* placeholder="e.g. Andorra"
* >
* </kendo-autocomplete>
* `
* })
* class AppComponent {
* public data: Array<string> = ["Albania", "Andorra", "Armenia", "Austria", "Azerbaijan"];
* }
* ```
* > Currently, the built-in filtering does not work with [grouped data]({% slug api_kendo-data-query_groupby %}).
*/
export class FilterDirective {
component;
/**
* The initial data that will be used as a source array for the filtering operations.
*/
set data(data) {
this._data = data || [];
}
get data() {
return this._data;
}
/**
* The configuration object which sets the behavior of the `kendoDropDownFilter` directive.
* If no [DropDownFilterSettings]({% slug api_dropdowns_dropdownfiltersettings %}) object is provided to this input, the directive will use the default interface settings.
*/
rawSettings;
/**
* @hidden
*
* Sets whether the filtering functionality is enabled on component init.
*/
filterable = true;
_data = [];
filterChangeSubscription;
constructor(component) {
this.component = component;
}
ngOnInit() {
this.component.filterable = this.filterable;
this.filterChangeSubscription = this.component.filterChange
.subscribe(this.handleFilterChange.bind(this));
}
ngOnDestroy() {
if (isPresent(this.filterChangeSubscription)) {
this.filterChangeSubscription.unsubscribe();
}
}
handleFilterChange(query) {
this.component.data = this.data.filter(item => this.matchesAnyField(item, query));
}
matchesAnyField(item, query) {
const normalizedQuery = this.normalizeValue(query);
const { fields } = this.filterSettings;
// if no filter fields are present, we are dealing with primitive data
if (fields.length === 0) {
return this.checkItem(item, normalizedQuery);
}
return fields.some(field => this.checkItem(getter(item, field), normalizedQuery));
}
checkItem(target, query) {
target = this.normalizeValue(target);
if (this.filterSettings.operator === 'contains') {
return target.indexOf(query) !== -1;
}
else {
return target.indexOf(query) === 0;
}
}
normalizeValue(value) {
const normalizedValue = isPresent(value) ? value.toString() : '';
return this.filterSettings.caseSensitive ? normalizedValue : normalizedValue.toLowerCase();
}
getFilterFields(providedFields) {
// ignore provided fields if the component deals with primitive data
if (!this.component.textField && !this.component.valueField) {
return [];
}
if (isArray(providedFields) && providedFields.length > 0) {
return providedFields;
}
else {
// the autocomplete uses `valueField` for text extraction
const textField = this.component.textField || this.component.valueField;
return [textField];
}
}
get filterSettings() {
const settings = this.rawSettings;
const providedFields = isPresent(settings) && typeof settings === 'object' ? settings.fields : [];
return Object.assign({}, DEFAULT_FILTER_SETTINGS, settings, { fields: this.getFilterFields(providedFields) });
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: FilterDirective, deps: [{ token: i1.FilterableComponent }], target: i0.ɵɵFactoryTarget.Directive });
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: FilterDirective, isStandalone: true, selector: "[kendoDropDownFilter]", inputs: { data: "data", rawSettings: ["kendoDropDownFilter", "rawSettings"], filterable: "filterable" }, ngImport: i0 });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: FilterDirective, decorators: [{
type: Directive,
args: [{
selector: '[kendoDropDownFilter]',
standalone: true
}]
}], ctorParameters: function () { return [{ type: i1.FilterableComponent }]; }, propDecorators: { data: [{
type: Input
}], rawSettings: [{
type: Input,
args: ['kendoDropDownFilter']
}], filterable: [{
type: Input
}] } });