Skip to content

Commit

Permalink
feat: close search button when clicking x (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsilvestri committed Oct 3, 2022
1 parent 0ccd222 commit fdc8e7b
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/SearchControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ interface SearchControl {
// [key: string]: any;
initialize(options: SearchControlProps): void;
onSubmit(result: Selection): void;
open(): void;
close(): void;
onClick(event: Event): void;
clearResults(event?: KeyboardEvent | null, force?: boolean): void;
autoSearch(event: KeyboardEvent): void;
Expand Down Expand Up @@ -200,7 +202,13 @@ const Control: SearchControl = {
{
text: '×',
href: '#',
onClick: () => this.clearResults(null, true),
onClick: () => {
if (this.searchElement.input.value === '') {
this.close();
} else {
this.clearResults(null, true);
}
},
},
);

Expand Down Expand Up @@ -284,18 +292,28 @@ const Control: SearchControl = {
return this;
},

open() {
const { container, input } = this.searchElement;
addClassName(container, 'active');
input.focus();
},

close() {
const { container } = this.searchElement;
removeClassName(container, 'active');
this.clearResults();
},

onClick(event: Event) {
event.preventDefault();
event.stopPropagation();

const { container, input } = this.searchElement;
const { container } = this.searchElement;

if (container.classList.contains('active')) {
removeClassName(container, 'active');
this.clearResults();
this.close();
} else {
addClassName(container, 'active');
input.focus();
this.open();
}
},

Expand Down

0 comments on commit fdc8e7b

Please sign in to comment.