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

fix(autocomplete): allow hiding icon #11239

Merged
merged 3 commits into from
Jan 9, 2025
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 @@ -428,6 +428,21 @@ describe("calcite-autocomplete", () => {
focusable("calcite-autocomplete");
});

it("should be able to remove icon", async () => {
const page = await newE2EPage();
await page.setContent(simpleHTML);

const autocomplete = await page.find("calcite-autocomplete");
const input = await page.find("calcite-autocomplete >>> calcite-input");

expect(await input.getProperty("icon")).toBe(true);

autocomplete.setProperty("icon", false);
await page.waitForChanges();

expect(await input.getProperty("icon")).toBe(false);
});

describe("keyboard navigation", () => {
let page: E2EPage;
beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ export const simple = (args: AutocompleteStoryArgs): string => html`
</div>
`;

export const customIcon = (): string => html`
<div style="width:350px">
<calcite-autocomplete icon="banana"></calcite-autocomplete>
</div>
`;

export const noIcon = (): string => html`
<div style="width:350px">
<calcite-autocomplete id="autocomplete"></calcite-autocomplete>
</div>
<script>
document.getElementById("autocomplete").icon = false;
</script>
`;

export const matchResults = (): string =>
html`<div style="width:350px; height: 600px;">
<calcite-autocomplete label="Item list" id="myAutocomplete" input-value="item" open>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { Validation } from "../functional/Validation";
import { createObserver } from "../../utils/observers";
import { styles } from "./autocomplete.scss";
import T9nStrings from "./assets/t9n/messages.en.json";
import { CSS, ICONS, IDS, SLOTS } from "./resources";
import { CSS, IDS, SLOTS } from "./resources";

const groupItemSelector = "calcite-autocomplete-item-group";
const itemSelector = "calcite-autocomplete-item";
Expand Down Expand Up @@ -654,12 +654,6 @@ export class Autocomplete
this.updateGroups();
}

private getIcon(): IconNameOrString {
const { icon } = this;

return icon === true ? ICONS.search : icon || ICONS.search;
}

private setReferenceEl(el: Input["el"]): void {
this.referenceEl = el;

Expand Down Expand Up @@ -804,7 +798,7 @@ export class Autocomplete
disabled={disabled}
enterKeyHint={enterKeyHint}
form={this.form}
icon={this.getIcon()}
icon={this.icon ?? true}
iconFlipRtl={this.iconFlipRtl}
id={inputId}
inputMode={inputMode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ export const SLOTS = {
contentTop: "content-top",
} as const;

export const ICONS = {
search: "search",
} as const;

export const CSS = {
inputContainer: "input-container",
input: "input",
Expand Down
24 changes: 16 additions & 8 deletions packages/calcite-components/src/utils/dom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,23 @@ describe("dom", () => {
}

describe("setRequestedIcon()", () => {
const iconObject = { exampleValue: "exampleReturnedValue" };
const matchedValue = "exampleValue";

it("returns the custom icon name if custom value is passed", () =>
expect(setRequestedIcon({ exampleValue: "exampleReturnedValue" }, "myCustomValue", "exampleValue")).toBe(
"myCustomValue",
));

it("returns the pre-defined icon name if custom value is not passed", () =>
expect(setRequestedIcon({ exampleValue: "exampleReturnedValue" }, "", "exampleValue")).toBe(
"exampleReturnedValue",
));
expect(setRequestedIcon(iconObject, "myCustomValue", matchedValue)).toBe("myCustomValue"));

it("returns the pre-defined icon name if custom value is true", () =>
expect(setRequestedIcon(iconObject, true, matchedValue)).toBe(iconObject[matchedValue]));

it("returns the pre-defined icon name if is an empty string", () =>
expect(setRequestedIcon(iconObject, "", matchedValue)).toBe(iconObject[matchedValue]));

it("returns undefined if custom value is undefined", () =>
expect(setRequestedIcon(iconObject, undefined, matchedValue)).toBe(undefined));

it("returns undefined if custom value is false", () =>
expect(setRequestedIcon(iconObject, false, matchedValue)).toBe(undefined));
});

describe("uniqueId", () => {
Expand Down
Loading