Skip to content

Commit

Permalink
Merge pull request #2802 from Geoffrey0/search-expandable-clear
Browse files Browse the repository at this point in the history
fix: allow esc and clear to work with the expandable search
  • Loading branch information
zvonimirfras authored Mar 1, 2024
2 parents 703e6a3 + 62fc717 commit daa7a31
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
53 changes: 53 additions & 0 deletions src/search/search.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,59 @@ describe("Search", () => {
expect(component.value).toEqual("");
});

it("should clear the input when the clear button is clicked on the expandable component", () => {
component.expandable = true;
component.value = "TextToClear";
fixture.detectChanges();
clearButtonElement = fixture.debugElement.query(By.css("button")).nativeElement;
clearButtonElement.click();
fixture.detectChanges();
expect(component.value).toEqual("");
});

it("should clear the input when the escape key is pressed", () => {
clearButtonElement = fixture.debugElement.query(By.css("button")).nativeElement;
component.value = "TextToClear";
fixture.detectChanges();
expect(clearButtonElement.className.includes("cds--search-close--hidden")).toEqual(false);
component.keyDown(new KeyboardEvent("keydown", {
"key": "Escape"
}));
fixture.detectChanges();
expect(component.value).toBe("");
expect(clearButtonElement.className.includes("cds--search-close--hidden")).toEqual(true);
});

it("should clear the input and keep the expanded state open when the escape key is pressed", () => {
component.expandable = true;
component.active = true;
containerElement = fixture.debugElement.query(By.css(".cds--search")).nativeElement;
component.value = "TextToClear";
fixture.detectChanges();
expect(containerElement.className.includes("cds--search--expanded")).toEqual(true);
component.keyDown(new KeyboardEvent("keydown", {
"key": "Escape"
}));
fixture.detectChanges();
expect(component.value).toBe("");
expect(containerElement.className.includes("cds--search--expanded")).toEqual(true);
});

it("should close the expandable search component when esc is pressed when content is empty", () => {
component.expandable = true;
component.active = true;
containerElement = fixture.debugElement.query(By.css(".cds--search")).nativeElement;
component.value = "";
fixture.detectChanges();
expect(containerElement.className.includes("cds--search--expanded")).toEqual(true);
component.keyDown(new KeyboardEvent("keydown", {
"key": "Escape"
}));
fixture.detectChanges();
expect(component.active).toEqual(false);
expect(containerElement.className.includes("cds--search--expanded")).toEqual(false);
});

it("should have dark and light theme", () => {
containerElement = fixture.debugElement.query(By.css(".cds--search")).nativeElement;
component.theme = "dark";
Expand Down
15 changes: 13 additions & 2 deletions src/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export class Search implements ControlValueAccessor {
*/
static searchCount = 0;

@HostBinding("class.cds--form-item") get containerClass() { return !(this.toolbar || this.expandable); }
@HostBinding("class.cds--form-item") get containerClass() {
return !(this.toolbar || this.expandable);
}

/**
* @deprecated since v5 - Use `cdsLayer` directive instead
Expand Down Expand Up @@ -221,11 +223,20 @@ export class Search implements ControlValueAccessor {
keyDown(event: KeyboardEvent) {
if (this.toolbar || this.expandable) {
if (event.key === "Escape") {
this.active = false;
if (this.value === "") {
this.active = false;
this.open.emit(this.active);
}
} else if (event.key === "Enter") {
this.openSearch();
}
}

if (event.key === "Escape") {
if (this.value !== "") {
this.clearSearch();
}
}
}

@HostListener("focusout", ["$event"])
Expand Down
7 changes: 6 additions & 1 deletion src/search/search.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default {
autocomplete: "on"
},
argTypes: {
expandable: {
type: "boolean",
defaultValue: false
},
size: {
options: ["sm", "md", "lg"],
control: "radio"
Expand Down Expand Up @@ -52,7 +56,8 @@ const Template = (args) => ({
[disabled]="disabled"
[size]="size"
(valueChange)="valueChange($event)"
(clear)="clear()">
(clear)="clear()"
[expandable]="expandable">
</cds-search>
`
});
Expand Down

0 comments on commit daa7a31

Please sign in to comment.