Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | /**
* @module module:lib/components/filter/filter.component
* @description Filter component module.
* @summary Provides `FilterComponent` which builds advanced, multi-step filter
* queries (index → condition → value) and emits filter events for data querying.
* It supports responsive behavior, suggestions and integration with `SearchbarComponent`.
*
* @link {@link FilterComponent}
*/
import {
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewChild,
} from '@angular/core';
import { TranslatePipe } from '@ngx-translate/core';
import { FormsModule } from '@angular/forms';
import { debounceTime, fromEvent, shareReplay, Subscription, takeUntil } from 'rxjs';
import { IonButton, IonChip, IonIcon, IonSelect, IonSelectOption } from '@ionic/angular/standalone';
import {
chevronDownOutline,
trashOutline,
closeOutline,
searchOutline,
arrowDownOutline,
arrowUpOutline,
chevronUpOutline,
} from 'ionicons/icons';
import { addIcons } from 'ionicons';
import { Model } from '@decaf-ts/decorator-validation';
import { OrderDirection } from '@decaf-ts/core';
import { NgxComponentDirective } from '../../engine/NgxComponentDirective';
import { Dynamic } from '../../engine/decorators';
import { IFilterQuery, IFilterQueryItem } from '../../engine/interfaces';
import { getWindowWidth } from '../../utils/helpers';
import { SearchbarComponent } from '../searchbar/searchbar.component';
import { Constructor } from '@decaf-ts/decoration';
import { IconComponent } from '../icon/icon.component';
import { NavigationStart } from '@angular/router';
/**
* @description Advanced filter component for creating dynamic search filters with step-by-step construction.
* @summary This component provides a comprehensive filtering interface that allows users to build
* complex search criteria using a three-step approach: select index → select condition → enter value.
* It supports filtering by multiple field indexes, comparison conditions, and values, displaying
* selected filters as removable chips. The component is responsive and includes auto-suggestions
* with keyboard navigation support.
*
* @example
* ```html
* <ngx-decaf-filter
* [indexes]="['name', 'email', 'department', 'status']"
* [conditions]="['Equal', 'Contains', 'Greater Than', 'Less Than']"
* [sort]="['createdAt', 'updatedAt']"
* [disableSort]="false"
* (filterEvent)="onFiltersChanged($event)">
* </ngx-decaf-filter>
* ```
*
* @mermaid
* sequenceDiagram
* participant U as User
* participant F as FilterComponent
* participant P as Parent Component
*
* U->>F: Focus input field
* F->>F: handleFocus() - Show available indexes
* U->>F: Select index (e.g., "name")
* F->>F: addFilter() - Step 1 completed
* F->>F: Show available conditions
* U->>F: Select condition (e.g., "Contains")
* F->>F: addFilter() - Step 2 completed
* F->>F: Show value input prompt
* U->>F: Enter value and press Enter
* F->>F: addFilter() - Step 3 completed
* F->>F: Create complete filter object
* F->>P: Emit filterEvent with new filter array
* F->>F: Reset to step 1 for next filter
*
* @memberOf ForAngularCommonModule
*/
@Dynamic()
@Component({
selector: 'ngx-decaf-filter',
templateUrl: './filter.component.html',
styleUrls: ['./filter.component.scss'],
imports: [
FormsModule,
TranslatePipe,
IonChip,
IonIcon,
IonButton,
IonSelect,
IonSelectOption,
IonIcon,
SearchbarComponent,
IconComponent,
],
standalone: true,
host: { '[attr.id]': 'uid' },
})
export class FilterComponent extends NgxComponentDirective implements OnInit, OnDestroy {
/**
* @description Reference to the dropdown options container element.
* @summary ViewChild reference used to access and manipulate the dropdown options element
* for highlighting filtered items and managing visual feedback during option selection.
* This element contains the filterable suggestions that users can interact with.
*
* @type {ElementRef}
* @memberOf FilterComponent
*/
@ViewChild('optionsFilterElement', { read: ElementRef, static: false })
optionsFilterElement!: ElementRef;
/**
* @description Allows multiple filters to be added.
* @summary When true, users can add multiple filters; when false, only one filter is allowed.
*
* @type {boolean}
* @default true
* @memberOf FilterComponent
*/
@Input()
multiple: boolean = true;
/**
* @description Available comparison conditions for filters.
* @summary Defines the list of comparison operators that can be used when creating filters.
* These conditions determine how the filter value is compared against the field value.
* Common conditions include equality, containment, and numerical comparison operations.
*
* @type {string[]}
* @default []
* @memberOf FilterComponent
*/
@Input()
conditions: string[] = [
'Equal',
'Contains',
'Not Contains',
'Greater Than',
'Less Than',
'Not Equal',
];
/**
* @description Available sorting options for the filtered data.
* @summary Defines the list of field names that can be used for sorting the filtered results.
* When disableSort is false, this array is automatically merged with the indexes array
* to provide comprehensive sorting capabilities.
*
* @type {string[]}
* @default []
* @memberOf FilterComponent
*/
@Input()
sortOptions: string[] = [];
/**
* @description Controls whether sorting functionality is disabled.
* @summary When set to true, prevents the automatic merging of sort and indexes arrays,
* effectively disabling sorting capabilities. This is useful when you want to provide
* filtering without sorting options.
*
* @type {boolean}
* @default false
* @memberOf FilterComponent
*/
@Input()
disableSort: boolean = false;
/**
* @description Current window width for responsive behavior.
* @summary Stores the current browser window width in pixels. This value is updated
* on window resize events to enable responsive filtering behavior and layout adjustments
* based on available screen space.
*
* @type {number}
* @memberOf FilterComponent
*/
windowWidth!: number;
/**
* @description Available options for the current filter step.
* @summary Contains the list of options available for selection in the current step.
* This array changes dynamically based on the current step: indexes → conditions → empty for value input.
*
* @type {string[]}
* @default []
* @memberOf FilterComponent
*/
options: string[] = [];
/**
* @description Filtered options based on user input.
* @summary Contains the subset of options that match the current user input for real-time
* filtering. This array is updated as the user types to show only relevant suggestions
* in the dropdown menu.
*
* @type {string[]}
* @default []
* @memberOf FilterComponent
*/
filteredOptions: string[] = [];
/**
* @description Complete filter objects created by the user.
* @summary Array of complete filter objects, each containing index, condition, and value properties.
* These represent the active filters that can be applied to data operations.
*
* @type {KeyValue[]}
* @default []
* @memberOf FilterComponent
*/
filterValue: IFilterQueryItem[] = [];
/**
* @description Current filter being constructed.
* @summary Temporary object that accumulates filter properties (index, condition, value)
* during the three-step filter creation process. Gets added to filterValue when complete.
*
* @type {KeyValue}
* @default {}
* @memberOf FilterComponent
*/
lastFilter: IFilterQueryItem = {};
/**
* @description Current step in the filter creation process.
* @summary Tracks the current step of filter creation: 1 = index selection, 2 = condition selection,
* 3 = value input. Automatically resets to 1 after completing a filter.
*
* @type {number}
* @default 1
* @memberOf FilterComponent
*/
step: number = 1;
/**
* @description Controls dropdown visibility state.
* @summary Boolean flag that determines whether the options dropdown is currently visible.
* Used to manage the dropdown's open/close state and coordinate with focus/blur events.
*
* @type {boolean}
* @default false
* @memberOf FilterComponent
*/
dropdownOpen: boolean = false;
/**
* @description Current input field value.
* @summary Stores the current text input value that the user is typing. This value is
* bound to the input field and is cleared after each successful filter step completion.
*
* @type {string}
* @default ''
* @memberOf FilterComponent
*/
override value: string = '';
/**
* @description Current sorting field value.
* @summary Stores the field name currently selected for sorting operations.
* This value determines which field is used to order the filtered results.
* Defaults to 'id' and can be changed through the sort dropdown selection.
*
* @type {string}
* @default 'id'
* @memberOf FilterComponent
*/
sortValue: string = 'id';
/**
* @description Subscription for window resize events.
* @summary RxJS subscription that listens for window resize events with debouncing
* to update the windowWidth property. This enables responsive behavior and prevents
* excessive updates during resize operations.
*
* @type {Subscription}
* @memberOf FilterComponent
*/
windowResizeSubscription!: Subscription;
/**
* @description Event emitter for filter changes.
* @summary Emits filter events when the user creates, modifies, or clears filters.
* The emitted value contains an array of complete filter objects or undefined when
* filters are cleared. Parent components listen to this event to update their data display.
*
* @type {EventEmitter<KeyValue[] | undefined>}
* @memberOf FilterComponent
*/
@Output()
filterEvent: EventEmitter<IFilterQuery | undefined> = new EventEmitter<
IFilterQuery | undefined
>();
/**
* @description Event emitter for search events.
* @summary Emits search events when the user interacts with the searchbar.
* @type {EventEmitter<string>}
* @memberOf FilterComponent
*/
@Output()
searchEvent: EventEmitter<string> = new EventEmitter<string>();
inputElement!: HTMLInputElement;
/**
* @description Constructor for FilterComponent.
* @summary Initializes a new instance of the FilterComponent.
* Calls the parent constructor with the component name to establish base locale string generation
* and internationalization support.
*
* @memberOf FilterComponent
*/
constructor() {
super('FilterComponent');
addIcons({
chevronDownOutline,
trashOutline,
closeOutline,
searchOutline,
arrowDownOutline,
arrowUpOutline,
chevronUpOutline,
});
}
/**
* @description Initializes the component after Angular first displays the data-bound properties.
* @summary Sets up the component by initializing window width tracking, setting up resize event
* subscriptions with debouncing, configuring sorting options, and calling the base initialization.
* This method prepares the component for user interaction and responsive behavior.
*
* @mermaid
* sequenceDiagram
* participant A as Angular Lifecycle
* participant F as FilterComponent
* participant W as Window
* participant R as RxJS
*
* A->>F: ngOnInit()
* F->>W: getWindowWidth()
* W-->>F: Return current width
* F->>R: Setup resize subscription with debounce
* R-->>F: Subscription created
* alt disableSort is false
* F->>F: Merge sort and indexes arrays
* end
* F->>F: Call initialize()
*
* @returns {Promise<void>}
* @memberOf FilterComponent
*/
async ngOnInit(): Promise<void> {
this.windowWidth = getWindowWidth() as number;
this.windowResizeSubscription = fromEvent(window, 'resize')
.pipe(debounceTime(300), takeUntil(this.destroySubscriptions$), shareReplay(1))
.subscribe(() => {
this.windowWidth = getWindowWidth() as number;
});
this.router.events.pipe(takeUntil(this.destroySubscriptions$)).subscribe(async (event) => {
Iif (event instanceof NavigationStart) await this.ngOnDestroy();
});
this.getIndexes();
this.initialize();
}
/**
* @description Retrieves and configures available indexes for filtering and sorting.
* @summary Extracts field indexes from the model if available and merges them with
* sorting options when sorting is enabled. This method sets up the available field
* options for both filtering and sorting operations based on the model structure.
*
* @returns {void}
* @memberOf FilterComponent
*/
override getIndexes(): string[] {
const indexes = super.getIndexes(this.model as Model);
Iif (!this.disableSort) {
this.sortOptions = [...this.sortOptions, ...indexes];
Iif (this.repository) {
const pk = Model.pk(this.repository.class as Constructor<Model>);
this.sortValue = pk || this.sortValue;
}
}
return indexes;
}
/**
* @description Cleanup method called when the component is destroyed.
* @summary Unsubscribes from window resize events to prevent memory leaks.
* This is essential for proper cleanup of RxJS subscriptions when the component
* is removed from the DOM.
*
* @returns {void}
* @memberOf FilterComponent
*/
override async ngOnDestroy(): Promise<void> {
await super.ngOnDestroy();
Iif (this.windowResizeSubscription) this.windowResizeSubscription.unsubscribe();
this.clear();
}
/**
* @description Handles input events from the text field.
* @summary Processes user input and filters the available options based on the typed value.
* This method provides real-time filtering of suggestions as the user types in the input field.
*
* @param {InputEvent} event - The input event containing the new value
* @returns {void}
* @memberOf FilterComponent
*/
handleInput(event: InputEvent): void {
const { value } = event.target as HTMLInputElement;
this.filteredOptions = this.filterOptions(value);
}
/**
* @description Handles focus events on the input field.
* @summary Sets up the available options when the input field receives focus and opens the dropdown.
* If no options are provided, automatically determines the appropriate options based on current step.
* This method initializes the dropdown with contextually relevant suggestions.
*
* @param {string[]} options - Optional array of options to display
* @returns {void}
* @memberOf FilterComponent
*/
handleFocus(options: string[] = []): void {
Iif (!options.length) options = this.getOptions();
this.filteredOptions = this.options = options;
this.dropdownOpen = true;
}
/**
* @description Handles blur events on the input field with delayed closing.
* @summary Manages the dropdown closing behavior with a delay to allow for option selection.
* Uses a two-phase approach to prevent premature closing when users click on dropdown options.
*
* @param {boolean} close - Internal flag to control the closing phase
* @returns {void}
* @memberOf FilterComponent
*/
handleBlur(close: boolean = false): void {
if (!close) {
this.dropdownOpen = false;
setTimeout(() => {
this.handleBlur(true);
}, 100);
} else {
Iif (!this.dropdownOpen && this.options.length) {
setTimeout(() => {
this.options = [];
this.dropdownOpen = false;
}, 50);
}
}
}
/**
* @description Determines the appropriate options based on the current filter step.
* @summary Returns the contextually relevant options for the current step in the filter creation process.
* Step 1 shows indexes, Step 2 shows conditions, Step 3 shows no options (value input).
*
* @returns {string[]} Array of options appropriate for the current step
* @memberOf FilterComponent
*/
getOptions(): string[] {
switch (this.step) {
case 1:
this.options = this.indexes;
break;
case 2:
this.options = this.conditions;
break;
case 3:
this.options = [];
break;
}
return this.options;
}
/**
* @description Adds a filter step or completes filter creation through a three-step process.
* @summary Core method for building filters step by step: Step 1 (Index) → Step 2 (Condition) → Step 3 (Value).
* When all steps are complete, creates a complete filter object and adds it to the filter collection.
* Handles both keyboard events (Enter to submit) and programmatic calls.
*
* @param {string} value - The value to add for the current step
* @param {CustomEvent} event - Optional event (KeyboardEvent triggers submission when value is empty)
* @returns {void}
*
* @mermaid
* sequenceDiagram
* participant U as User
* participant F as FilterComponent
*
* U->>F: addFilter(value, event)
* F->>F: Trim and validate value
* alt KeyboardEvent && empty value
* F->>F: submit() - Send current filters
* else Valid value or step 3
* alt Step 1 (Index)
* F->>F: lastFilter.index = value
* F->>F: options = conditions
* else Step 2 (Condition)
* F->>F: lastFilter.condition = value
* F->>F: options = []
* else Step 3 (Value)
* F->>F: lastFilter.value = value
* F->>F: Add complete filter to filterValue
* F->>F: Reset step to 1
* end
* F->>F: Increment step
* F->>F: Clear input & focus
* F->>F: Show next options
* end
*
* @memberOf FilterComponent
*/
async addFilter(value: string, event?: CustomEvent): Promise<void> {
value = value.trim();
Iif (!this.inputElement) this.inputElement = this.component.nativeElement.querySelector('input');
if (event instanceof KeyboardEvent && !value) {
this.inputElement.blur();
await this.submit();
} else {
Iif ((value && !(event instanceof KeyboardEvent)) || this.step === 3) {
const filter = this.lastFilter;
switch (this.step) {
case 1:
filter['index'] = value;
this.options = this.conditions;
break;
case 2:
filter['condition'] = value;
this.options = [];
break;
case 3:
filter['value'] = value;
this.options = this.indexes;
break;
}
if (!this.filterValue.length) {
this.filterValue.push(filter);
} else {
Iif (this.step === 1) this.filterValue.push(filter);
}
Iif (this.step === 3) {
this.step = 0;
this.filterValue[this.filterValue.length - 1] = filter;
this.lastFilter = {};
this.inputElement.value = '';
Iif (!this.multiple) {
Iif (this.inputElement) this.inputElement.disabled = true;
this.inputElement.blur();
return await this.submit();
}
}
this.step++;
this.value = '';
Iif (this.options.length) this.handleFocus(this.options);
this.component.nativeElement.querySelector('#dcf-filter-field').focus();
}
}
}
/**
* @description Selects an option from the dropdown suggestions.
* @summary Handles option selection when a user clicks on a suggestion in the dropdown.
* This method acts as a bridge between dropdown clicks and the main addFilter logic.
*
* @param {CustomEvent} event - The click event from the dropdown option
* @param {string} value - The selected option value
* @returns {void}
* @memberOf FilterComponent
*/
selectOption(value: string): void {
this.addFilter(value);
}
/**
* @description Determines if a filter option can be individually removed.
* @summary Checks whether a filter component should display a close icon for removal.
* Only value options can be removed individually; index and condition options are part
* of the complete filter structure and cannot be removed separately.
*
* @param {string} option - The filter option text to check
* @returns {boolean} True if the option can be cleared individually, false otherwise
* @memberOf FilterComponent
*/
allowClear(option: string): boolean {
return this.indexes.indexOf(option) === -1 && this.conditions.indexOf(option) === -1;
}
/**
* @description Removes a complete filter from the collection based on filter value.
* @summary Removes a complete filter by matching the provided value against filter values
* in the collection. Uses string normalization to handle accents and case differences.
* After removal, resets the interface to show available indexes for new filter creation.
*
* @param {string} filter - The filter value to remove (matches against filter.value property)
* @returns {void}
*
* @mermaid
* sequenceDiagram
* participant U as User
* participant F as FilterComponent
*
* U->>F: removeFilter(filterValue)
* F->>F: cleanString(filterValue)
* F->>F: Filter out matching filter objects
* F->>F: Clear input value
* F->>F: handleFocus(indexes) - Reset to index selection
* Note over F: Filter removed and UI reset
*
* @memberOf FilterComponent
*/
removeFilter(filter: string): void {
function cleanString(filter: string): string {
return filter
.toLowerCase() // convert all characters to lowercase
.normalize('NFD') // separate accent marks from characters
.replace(/[\u0300-\u036f]/g, '') // remove accent marks
.replace(/\s+/g, ''); // remove all whitespace
}
this.value = '';
this.filterValue = this.filterValue.filter(
(item) => item?.['value'] && cleanString(item?.['value']) !== cleanString(filter),
);
Iif (this.filterValue.length === 0) {
this.step = 1;
this.lastFilter = {};
}
Iif (this.inputElement) {
this.inputElement.disabled = false;
this.handleFocus(this.indexes);
}
}
/**
* @description Resets the component to its initial state.
* @summary Clears all filter data, options, and resets the step counter to 1.
* This method provides a clean slate for new filter creation without emitting events.
*
* @returns {void}
* @memberOf FilterComponent
*/
reset(submit: boolean = true): void {
this.options = this.filteredOptions = this.filterValue = [];
this.step = 1;
this.lastFilter = {};
this.value = '';
Iif (this.inputElement) this.inputElement.disabled = false;
Iif (submit) {
setTimeout(() => {
this.submit();
}, 100);
}
}
/**
* @description Clears all filters and notifies parent components.
* @summary Resets the component state and emits undefined to notify parent components
* that all filters have been cleared. This triggers any connected data refresh logic.
*
* @param {string} value - Optional parameter (currently unused)
* @returns {void}
* @memberOf FilterComponent
*/
clear(value?: string): void {
this.filterValue = [];
Iif (!value) this.reset();
}
/**
* @description Submits the current filter collection to parent components.
* @summary Emits the current filter array to parent components when filters are ready
* to be applied. Only emits if there are active filters. Clears options after submission.
*
* @returns {Promise<void>}
* @memberOf FilterComponent
*/
override async submit(): Promise<void> {
this.filterEvent.emit({
query: this.filterValue.length > 0 ? this.filterValue : undefined,
sort: {
value: this.sortValue,
direction: this.sortDirection,
},
} as IFilterQuery);
Iif (this.filterValue.length === 0) this.options = [];
}
/**
* @description Toggles the sort direction between ascending and descending.
* @summary Handles sort direction changes by toggling between ASC and DSC values.
* When the direction changes, automatically triggers a submit to apply the new
* sorting configuration to the filtered results.
*
* @returns {void}
* @memberOf FilterComponent
*/
handleSortDirectionChange(): void {
const direction =
this.sortDirection === OrderDirection.ASC ? OrderDirection.DSC : OrderDirection.ASC;
Iif (direction !== this.sortDirection) {
this.sortDirection = direction;
this.submit();
}
}
/**
* @description Handles sort field selection changes from the dropdown.
* @summary Processes sort field changes when users select a different field
* from the sort dropdown. Updates the sortValue property and triggers
* a submit to apply the new sorting configuration if the value has changed.
*
* @param {CustomEvent} event - The select change event containing the new sort field value
* @returns {void}
* @memberOf FilterComponent
*/
handleSortChange(event: CustomEvent): void {
const target = event.target as HTMLIonSelectElement;
const value = target.value;
Iif (value !== this.sortValue) {
this.sortValue = value as string;
this.submit();
}
}
/**
* @description Filters available options based on user input with visual highlighting.
* @summary Performs real-time filtering of available options based on user input.
* Also handles visual highlighting of matching options in the dropdown. Returns all
* options if input is less than 2 characters for performance optimization.
*
* @param {string | null | undefined} value - The search value to filter by
* @returns {string[]} Array of filtered options that match the input
*
* @mermaid
* sequenceDiagram
* participant U as User
* participant F as FilterComponent
* participant D as DOM
*
* U->>F: filterOptions(inputValue)
* alt inputValue < 2 characters
* F->>D: Remove existing highlights
* F-->>U: Return all options
* else inputValue >= 2 characters
* F->>D: Query all option elements
* F->>D: Add highlight to first matching option
* F->>F: Filter options by substring match
* F-->>U: Return filtered options
* end
*
* @memberOf FilterComponent
*/
filterOptions(value: string | null | undefined): string[] {
const optionsElement = this.optionsFilterElement.nativeElement;
Iif (!value?.length || !value || value.length < 2) {
const filteredOption = optionsElement.querySelector('.dcf-filtering-item');
Iif (filteredOption) filteredOption.classList.remove('dcf-filtering-item');
return this.options;
}
const options = optionsElement.querySelectorAll('.dcf-item');
for (const option of options) {
const isActive = option.textContent?.toLowerCase().includes(value.toLowerCase());
Iif (isActive) {
option.classList.add('dcf-filtering-item');
break;
}
}
return this.options.filter((option: string) =>
option.toLowerCase().includes(value.toLowerCase() as string),
);
}
/**
* @description Handles search events from the integrated searchbar component.
* @summary Processes search input from the searchbar and emits search events
* to parent components. This method acts as a bridge between the internal
* searchbar component and external search event listeners.
*
* @param {string | undefined} value - The search value entered by the user
* @returns {void}
* @memberOf FilterComponent
*/
handleSearch(value: string | undefined): void {
this.searchEvent.emit(value);
}
}
|