Skip to content

Commit 4914bf6

Browse files
authored
Make autocomplete's clearInput method public (#421)
Library consumer might need it to reset the input state.
1 parent 6c07dc2 commit 4914bf6

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

projects/cps-ui-kit/src/lib/components/cps-autocomplete/cps-autocomplete.component.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,26 @@ describe('CpsAutocompleteComponent', () => {
403403
);
404404
expect(container).toBeTruthy();
405405
});
406+
407+
it('should clear input text and reset options when clearInput function is called', () => {
408+
component.options = [
409+
{ label: 'First Option', value: 'A' },
410+
{ label: 'Second Option', value: 'B' },
411+
{ label: 'Third Option', value: 'C' }
412+
];
413+
const event = new Event('input');
414+
Object.defineProperty(event, 'target', {
415+
value: { value: 'First' }
416+
});
417+
418+
component.autocompleteInput.nativeElement.dispatchEvent(event);
419+
420+
expect(component.inputText).toBe('First');
421+
expect(component.filteredOptions.length).toBe(1);
422+
423+
component.clearInput();
424+
425+
expect(component.inputText).toBe('');
426+
expect(component.filteredOptions.length).toBe(3);
427+
});
406428
});

projects/cps-ui-kit/src/lib/components/cps-autocomplete/cps-autocomplete.component.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ export class CpsAutocompleteComponent
537537
}
538538

539539
if (needClearInput) {
540-
this._clearInput();
540+
this.clearInput();
541541
}
542542
if (needFocusInput) {
543543
setTimeout(() => {
@@ -639,7 +639,7 @@ export class CpsAutocompleteComponent
639639
const val = this.multiple ? [] : this._getEmptyValue();
640640
this.updateValue(val);
641641
}
642-
this._clearInput();
642+
this.clearInput();
643643
this._dehighlightOption();
644644
setTimeout(() => {
645645
this.focusInput();
@@ -780,6 +780,15 @@ export class CpsAutocompleteComponent
780780
);
781781
}
782782

783+
clearInput(): void {
784+
this.filteredOptions = this.options;
785+
this.inputText = '';
786+
this.inputTextDebounced = '';
787+
this._inputChangeSubject$.next('');
788+
this.activeSingle = false;
789+
this.recalcVirtualListHeight();
790+
}
791+
783792
private _getEmptyValue() {
784793
const option = this.options[this.emptyOptionIndex];
785794
return !option
@@ -888,17 +897,8 @@ export class CpsAutocompleteComponent
888897
: '';
889898
}
890899

891-
private _clearInput() {
892-
this.filteredOptions = this.options;
893-
this.inputText = '';
894-
this.inputTextDebounced = '';
895-
this._inputChangeSubject$.next('');
896-
this.activeSingle = false;
897-
this.recalcVirtualListHeight();
898-
}
899-
900900
private _closeAndClear() {
901-
this._clearInput();
901+
this.clearInput();
902902
this._toggleOptions(false);
903903
this._dehighlightOption();
904904
}
@@ -998,7 +998,7 @@ export class CpsAutocompleteComponent
998998
}
999999
}
10001000

1001-
this._clearInput();
1001+
this.clearInput();
10021002
}
10031003

10041004
private _removeLastValue() {

0 commit comments

Comments
 (0)