-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add checked item section to shopping-list-card #2005
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
Changes from all commits
efaad8b
be89d96
b4969f1
00314b3
1596f71
82b5b53
55416dc
0799f70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,40 +22,36 @@ interface Config extends LovelaceConfig { | |
|
|
||
| class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement) | ||
| implements LovelaceCard { | ||
| private _hass?: HomeAssistant; | ||
| public hass?: HomeAssistant; | ||
| private _config?: Config; | ||
| private _items?: ShoppingListItem[]; | ||
| private _uncheckedItems?: ShoppingListItem[]; | ||
| private _checkedItems?: ShoppingListItem[]; | ||
| private _unsubEvents?: Promise<() => Promise<void>>; | ||
|
|
||
| static get properties() { | ||
| return { | ||
| _config: {}, | ||
| _items: {}, | ||
| _uncheckedItems: {}, | ||
| _checkedItems: {}, | ||
| }; | ||
| } | ||
|
|
||
| set hass(hass: HomeAssistant) { | ||
| this._hass = hass; | ||
| } | ||
|
|
||
| public getCardSize(): number { | ||
| return ( | ||
| (this._config ? (this._config.title ? 1 : 0) : 0) + | ||
| (this._items ? this._items.length : 3) | ||
| ); | ||
| return (this._config ? (this._config.title ? 1 : 0) : 0) + 3; | ||
| } | ||
|
|
||
| public setConfig(config: Config): void { | ||
| this._config = config; | ||
| this._items = []; | ||
| this._uncheckedItems = []; | ||
| this._checkedItems = []; | ||
| this._fetchData(); | ||
| } | ||
|
|
||
| public connectedCallback(): void { | ||
| super.connectedCallback(); | ||
|
|
||
| if (this._hass) { | ||
| this._unsubEvents = this._hass.connection.subscribeEvents( | ||
| if (this.hass) { | ||
| this._unsubEvents = this.hass.connection.subscribeEvents( | ||
| () => this._fetchData(), | ||
| "shopping_list_updated" | ||
| ); | ||
|
|
@@ -72,7 +68,7 @@ class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement) | |
| } | ||
|
|
||
| protected render(): TemplateResult { | ||
| if (!this._config || !this._hass) { | ||
| if (!this._config || !this.hass) { | ||
| return html``; | ||
| } | ||
|
|
||
|
|
@@ -81,7 +77,7 @@ class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement) | |
| <ha-card .header="${this._config.title}"> | ||
| ${ | ||
| repeat( | ||
| this._items!, | ||
| this._uncheckedItems!, | ||
| (item) => item.id, | ||
| (item, index) => | ||
| html` | ||
|
|
@@ -97,7 +93,7 @@ class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement) | |
| <paper-item-body> | ||
| <paper-input | ||
| no-label-float | ||
| .value="${item.name}" | ||
| value="${item.name}" | ||
| .itemId="${item.id}" | ||
| @change="${this._saveEdit}" | ||
| ></paper-input> | ||
|
|
@@ -106,6 +102,47 @@ class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement) | |
| ` | ||
| ) | ||
| } | ||
| ${ | ||
| this._checkedItems!.length > 0 | ||
| ? html` | ||
| <div class="divider"></div> | ||
| <div class="label"> | ||
| ${ | ||
| this.localize( | ||
| "ui.panel.lovelace.cards.shopping-list.checked_items" | ||
| ) | ||
| } | ||
| </div> | ||
| ${ | ||
| repeat( | ||
| this._checkedItems!, | ||
| (item) => item.id, | ||
| (item, index) => | ||
| html` | ||
| <div class="editRow"> | ||
| <paper-checkbox | ||
| slot="item-icon" | ||
| id="${index}" | ||
| ?checked="${item.complete}" | ||
| .itemId="${item.id}" | ||
| @click="${this._completeItem}" | ||
| tabindex="0" | ||
| ></paper-checkbox> | ||
| <paper-item-body> | ||
| <paper-input | ||
| no-label-float | ||
| value="${item.name}" | ||
| .itemId="${item.id}" | ||
| @change="${this._saveEdit}" | ||
| ></paper-input> | ||
| </paper-item-body> | ||
| </div> | ||
| ` | ||
| ) | ||
| } | ||
| ` | ||
| : "" | ||
| } | ||
| </ha-card> | ||
| `; | ||
| } | ||
|
|
@@ -133,24 +170,46 @@ class HuiShoppingListCard extends hassLocalizeLitMixin(LitElement) | |
| position: relative; | ||
| top: 1px; | ||
| } | ||
| .label { | ||
| color: var(--primary-color); | ||
| margin-left: 17px; | ||
| margin-bottom: 11px; | ||
| margin-top: 11px; | ||
| } | ||
| .divider { | ||
| height: 1px; | ||
| background-color: var(--divider-color); | ||
| margin: 10px; | ||
| } | ||
| </style> | ||
| `; | ||
| } | ||
|
|
||
| private async _fetchData(): Promise<void> { | ||
| if (this._hass) { | ||
| this._items = await fetchItems(this._hass); | ||
| if (this.hass) { | ||
| const checkedItems: ShoppingListItem[] = []; | ||
| const uncheckedItems: ShoppingListItem[] = []; | ||
| const items = await fetchItems(this.hass); | ||
| for (const key in items) { | ||
| if (items[key].complete) { | ||
| checkedItems.push(items[key]); | ||
| } else { | ||
| uncheckedItems.push(items[key]); | ||
| } | ||
| } | ||
| this._checkedItems = checkedItems; | ||
| this._uncheckedItems = uncheckedItems; | ||
| } | ||
| } | ||
|
|
||
| private _completeItem(ev): void { | ||
| completeItem(this._hass!, ev.target.itemId, ev.target.checked).catch(() => | ||
| completeItem(this.hass!, ev.target.itemId, ev.target.checked).catch(() => | ||
| this._fetchData() | ||
| ); | ||
| } | ||
|
|
||
| private _saveEdit(ev): void { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Param Type
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ech. I don't know how to handle this. It's a MouseEvent...but the target is then a paper-checkbox type with an additional field
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually _saveEdit would be a KeyboardEvent or a MouseEvent...but only applies when you either click out or hit enter...I don't know what to do... |
||
| saveEdit(this._hass!, ev.target.itemId, ev.target.value).catch(() => | ||
| saveEdit(this.hass!, ev.target.itemId, ev.target.value).catch(() => | ||
| this._fetchData() | ||
| ); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Param Type