Skip to content

Commit

Permalink
feat(input): display type icons (#713)
Browse files Browse the repository at this point in the history
I decided that'd be a good idea to display icons depending on the type
of the input.

> `icon` attribute overrides the default icon.

<img width="844" alt="image"
src="https://github.com/Trendyol/baklava/assets/47941171/3025aee8-5357-45c1-946f-01ab7fa5b680">

```html
<bl-input type="search" />
<bl-input type="date" />
<bl-input type="date" icon="academy" />
<bl-input type="time" />
```

Additionally, this PR adds input type of `search`, it could be good idea
to review #712 first.

---------

Co-authored-by: Aykut Saraç <aykut.sarac@trendyol.com>
  • Loading branch information
AykutSarac and Aykut Saraç authored Sep 29, 2023
1 parent 3b8fbe8 commit b7d9faf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/components/input/bl-input.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ input:disabled {
cursor: not-allowed;
}

input::-webkit-calendar-picker-indicator {
display: none;
}

input::-moz-calendar-picker-indicator {
display: none;
}

input:autofill {
background-color: var(--autofill-bg-color);

Expand Down
18 changes: 18 additions & 0 deletions src/components/input/bl-input.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ Input also supports slot icons for more complex use cases. You can use `icon` sl
</Story>
</Canvas>

Inputs with type of date, time, datetime-local, month, week and search have default icons. You can override these icons with `icon` attribute.

<Canvas>
<Story name="Date Input With Default Icon" args={{ type: "date" }}>
{SingleInputTemplate.bind({})}
</Story>
<Story name="Time Input With Default Icon" args={{ type: "time" }}>
{SingleInputTemplate.bind({})}
</Story>
<Story name="Search Input With Default Icon" args={{ type: "search" }}>
{SingleInputTemplate.bind({})}
</Story>
<Story name="Date Input With Custom Icon" args={{ type: "date", icon: "star" }}>
{SingleInputTemplate.bind({})}
</Story>
</Canvas>


## Input Validation

Input supports native HTML validation rules like `required`, `minlength`, `maxlength`, `min` and `max`. Other validation rules will come soon.
Expand Down
19 changes: 19 additions & 0 deletions src/components/input/bl-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ describe("bl-input", () => {
});

describe("input with icon", () => {
it("should show default icon", async () => {
const el = await fixture<BlInput>(html`<bl-input type="time"></bl-input>`);
const defaultIcon = el.shadowRoot?.querySelector('bl-icon[name="clock"]');

expect(defaultIcon).to.exist;
expect(defaultIcon?.getAttribute("name")).to.equal("clock");
expect(el.shadowRoot?.querySelector(".has-icon")).to.exist;
});

it("should override default icon when custom icon is set", async () => {
const el = await fixture<BlInput>(html`<bl-input type="time" icon="academy"></bl-input>`);
const defaultIcon = el.shadowRoot?.querySelector('bl-icon[name="clock"]');
const customIcon = el.shadowRoot?.querySelector('bl-icon[name="academy"]');

expect(defaultIcon).to.not.exist;
expect(customIcon).to.exist;
expect(el.shadowRoot?.querySelector(".has-icon")).to.exist;
});

it("should show custom icon", async () => {
const el = await fixture<BlInput>(html`<bl-input icon="info"></bl-input>`);
const customIcon = el.shadowRoot?.querySelector('bl-icon[name="info"]');
Expand Down
15 changes: 13 additions & 2 deletions src/components/input/bl-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ export type InputType =
| "password"
| "number"
| "tel"
| "url";
| "url"
| "search";

const inputTypeIcons: Partial<Record<InputType, BaklavaIcon>> = {
"date": "calendar",
"datetime-local": "calendar",
"month": "calendar",
"week": "calendar",
"time": "clock",
"search": "search",
};

export type InputSize = "small" | "medium" | "large";
/**
Expand Down Expand Up @@ -53,7 +63,7 @@ export default class BlInput extends FormControlMixin(LitElement) {
name?: string;

/**
* Type of the input. It's used to set `type` attribute of native input inside. Only `text`, `number` and `password` is supported for now.
* Type of the input. It's used to set `type` attribute of native input inside.
*/
@property({ reflect: true })
type: InputType = "text";
Expand Down Expand Up @@ -284,6 +294,7 @@ export default class BlInput extends FormControlMixin(LitElement) {

firstUpdated() {
this.setValue(this.value);
if (!this.icon) this.icon = inputTypeIcons[this.type];
}

protected async updated(changedProperties: PropertyValues) {
Expand Down

0 comments on commit b7d9faf

Please sign in to comment.