Skip to content

Commit

Permalink
feat(input): add clear button for search input (Trendyol#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzergul committed Feb 3, 2025
1 parent 92536ea commit c8249d5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/calendar/bl-calendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@

.grid-item {
width: 93.33px;

--bl-size-3xs: 15px;
}

Expand Down
6 changes: 5 additions & 1 deletion src/components/input/bl-input.css
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ input::-webkit-calendar-picker-indicator {
display: none;
}

input::-webkit-search-cancel-button {
display: none;
}

input::-moz-calendar-picker-indicator {
display: none;
}
Expand Down Expand Up @@ -193,7 +197,7 @@ input:-webkit-autofill {
margin-inline-end: var(--label-padding);
}

bl-icon:not(.reveal-icon),
bl-icon:not(.reveal-icon, .clear-icon),
::slotted(bl-icon) {
font-size: var(--icon-size);
color: var(--icon-color);
Expand Down
28 changes: 28 additions & 0 deletions src/components/input/bl-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ describe("bl-input", () => {

expect(revealButton).to.have.style("display", "none");
});

it("should hide clear button for empty or non-search inputs", async () => {
const emptySearchEl = await fixture<BlInput>(html`<bl-input type="search" value=""></bl-input>`);
const emptySearchCloseIcon = emptySearchEl?.shadowRoot?.querySelector('bl-icon[name="close"]');

expect(emptySearchCloseIcon).to.not.exist;

const textInputEl = await fixture<BlInput>(html`<bl-input type="text" value="test"></bl-input>`);
const textInputCloseIcon = textInputEl?.shadowRoot?.querySelector('bl-icon[name="close"]');

expect(textInputCloseIcon).to.not.exist;
});
});

describe("validation", () => {
Expand Down Expand Up @@ -319,6 +331,22 @@ describe("bl-input", () => {
expect(input).to.attr("type", "text");
});

it("should show clear button and clear value on clear button click", async () => {
const el = await fixture<BlInput>(html`<bl-input type="search" value="test"></bl-input>`);
const closeIcon = el?.shadowRoot?.querySelector('bl-icon[name="close"]') as HTMLElement | null;
const input = el?.shadowRoot?.querySelector("input");

expect(input).to.attr("type", "search");
expect(closeIcon).to.exist;
expect(el.value).to.equal("test");

closeIcon?.click();
await elementUpdated(el);

expect(el.value).to.equal("");
});


it("should fire bl-input event when input value changes", async () => {
const el = await fixture<BlInput>(html`<bl-input></bl-input>`);
const input = el.shadowRoot?.querySelector("input");
Expand Down
22 changes: 21 additions & 1 deletion src/components/input/bl-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ export default class BlInput extends FormControlMixin(LitElement) {
this.passwordVisible = !this.passwordVisible;
}

private async clearSearch() {
this.value = "";

await this.clearCustomError();
this.validationTarget.focus();
}

showPicker() {
if ("showPicker" in HTMLInputElement.prototype) {
this.validationTarget.showPicker();
Expand Down Expand Up @@ -391,6 +398,19 @@ export default class BlInput extends FormControlMixin(LitElement) {
</bl-button>`
: "";

const clearButton =
this.type === "search" && this.value !== "" && this.value !== null
? html`<bl-button
size="small"
kind="neutral"
variant="tertiary"
aria-label="Clear search"
@bl-click="${this.clearSearch}"
>
<bl-icon class="clear-icon" slot="icon" name="close"></bl-icon>
</bl-button>`
: "";

const hasCustomIcon = this.icon || this._hasIconSlot;
const classes = {
"wrapper": true,
Expand Down Expand Up @@ -430,7 +450,7 @@ export default class BlInput extends FormControlMixin(LitElement) {
aria-describedby=${ifDefined(this.helpText ? "helpText" : undefined)}
aria-errormessage=${ifDefined(this.checkValidity() ? undefined : "errorMessage")}
/>
<div class="icon">${revealButton} ${icon}</div>
<div class="icon">${revealButton} ${clearButton} ${icon}</div>
</fieldset>
<div class="hint">${invalidMessage} ${helpMessage}</div>
</div>`;
Expand Down

0 comments on commit c8249d5

Please sign in to comment.