Skip to content

Commit

Permalink
full basket example
Browse files Browse the repository at this point in the history
  • Loading branch information
serikshaikamalov committed Jul 3, 2024
1 parent cb81320 commit d093cc3
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,59 @@ export class BasketStorage extends EventTarget {
const basketStorage = BasketStorage.getInstance();
export default basketStorage;
```

with lit component

```js
import { html, css, LitElement } from "https://cdn.jsdelivr.net/npm/lit@2/+esm";
import basketStorage from "./basketStorage.js";

export class Basket extends LitElement {
static get properties() {
return {
items: { type: Array },
};
}

constructor() {
super();
}

connectedCallback() {
super.connectedCallback();
basketStorage.addEventListener("updated", (i) => {
this.items = basketStorage.getItems();
});
}

submit(e) {
e.preventDefault();

const searchNode = this.renderRoot.querySelector("#search");
if (searchNode) {
const v = searchNode.value;
basketStorage.addItem(v);
}
searchNode.value = "";
}

render() {
return html`
<form @submit=${this.submit}>
<div>
<input type="text" id="search" />
<button type="submit">Submit</button>
</div>
<div>
${this.items
? html`${this.items.map((i) => {
return html`<div>${i}</div>`;
})}`
: html`<h2>No items</h2>`}
</div>
</form>
`;
}
}
customElements.define("app-basket", Basket);
```

0 comments on commit d093cc3

Please sign in to comment.