Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACS-8998] Added validation to search input to disallow certain special characters #4253

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
mat-icon-button
matPrefix
class="app-search-button"
(click)="searchSubmit(searchTerm)"
(click)="searchSubmit()"
[title]="'SEARCH.BUTTON.TOOLTIP' | translate"
>
<mat-icon [attr.aria-label]="'SEARCH.BUTTON.ARIA-LABEL' | translate">search</mat-icon>
Expand All @@ -17,15 +17,14 @@
[attr.aria-label]="'SEARCH.INPUT.ARIA-LABEL' | translate"
[type]="inputType"
id="app-control-input"
[(ngModel)]="searchTerm"
(ngModelChange)="inputChange($event)"
(keyup.enter)="searchSubmit($event)"
[formControl]="searchFieldFormControl"
(keyup.enter)="searchSubmit()"
[placeholder]="'SEARCH.INPUT.PLACEHOLDER' | translate"
autocomplete="off"
/>
<div matSuffix>
<button mat-icon-button (click)="clear()">
<mat-icon *ngIf="searchTerm.length" class="app-suffix-icon">clear</mat-icon>
<mat-icon *ngIf="searchFieldFormControl.value.length" class="app-suffix-icon">clear</mat-icon>
</button>
</div>
</mat-form-field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,29 @@ describe('SearchInputControlComponent', () => {
});

it('should emit submit event on searchSubmit', () => {
const keyboardEvent = { target: { value: 'a' } };
component.searchTerm = 'mock-search-term';

let eventArgs = null;
component.submit.subscribe((args) => (eventArgs = args));
let submittedSearchTerm = '';
component.submit.subscribe((searchTerm) => (submittedSearchTerm = searchTerm));

component.searchSubmit(keyboardEvent);
expect(eventArgs).toBe(keyboardEvent);
component.searchSubmit();
expect(submittedSearchTerm).toBe('mock-search-term');
});

it('should emit searchChange event on inputChange', () => {
const searchTerm = 'b';
let emittedSearchTerm = '';
component.searchChange.subscribe((searchTerm) => (emittedSearchTerm = searchTerm));
component.searchTerm = 'mock-search-term';

let eventArgs = null;
component.searchChange.subscribe((args) => (eventArgs = args));

component.inputChange(searchTerm);
expect(eventArgs).toBe(searchTerm);
expect(emittedSearchTerm).toBe('mock-search-term');
});

it('should emit searchChange event on clear', () => {
let eventArgs = null;
component.searchChange.subscribe((args) => (eventArgs = args));
let emittedSearchTerm: string = null;
component.searchChange.subscribe((searchTerm) => (emittedSearchTerm = searchTerm));

component.clear();
expect(eventArgs).toBe('');
expect(emittedSearchTerm).toBe('');
});

it('should clear searchTerm', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,28 @@
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/

import { Component, ElementRef, EventEmitter, Input, Output, ViewChild, ViewEncapsulation } from '@angular/core';
import { Component, EventEmitter, Input, Output, ViewEncapsulation, ViewChild, ElementRef, OnInit, inject, DestroyRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TranslateModule } from '@ngx-translate/core';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { FormsModule } from '@angular/forms';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

@Component({
standalone: true,
imports: [CommonModule, TranslateModule, MatButtonModule, MatIconModule, MatFormFieldModule, MatInputModule, FormsModule],
imports: [CommonModule, TranslateModule, MatButtonModule, MatIconModule, MatFormFieldModule, MatInputModule, FormsModule, ReactiveFormsModule],
selector: 'app-search-input-control',
templateUrl: './search-input-control.component.html',
styleUrls: ['./search-input-control.component.scss'],
encapsulation: ViewEncapsulation.None,
host: { class: 'app-search-control' }
})
export class SearchInputControlComponent {
export class SearchInputControlComponent implements OnInit {
private readonly destroyRef = inject(DestroyRef);

/** Type of the input field to render, e.g. "search" or "text" (default). */
@Input()
inputType = 'text';
Expand All @@ -63,14 +66,27 @@ export class SearchInputControlComponent {
@ViewChild('searchInput', { static: true })
searchInput: ElementRef;

searchTerm = '';
searchFieldFormControl = new FormControl('');

get searchTerm(): string {
return this.searchFieldFormControl.value.replace('text:', 'TEXT:');
}

set searchTerm(value: string) {
this.searchFieldFormControl.setValue(value);
}

searchSubmit(event: any) {
this.submit.emit(event);
ngOnInit() {
this.searchFieldFormControl.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((searchTermValue) => {
this.searchFieldFormControl.markAsTouched();
this.searchChange.emit(searchTermValue);
});
}

inputChange(event: any) {
this.searchChange.emit(event);
searchSubmit() {
if (!this.searchFieldFormControl.errors) {
this.submit.emit(this.searchTerm);
}
}

clear() {
Expand Down
Loading