-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add context to event trigger #7182
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d98f21a
Add context to event trigger
OnFreund 840b2f3
User picker instead of yaml
OnFreund f3ed5bd
Remove console.log
OnFreund 40afd5a
Fix lint
OnFreund 3979a48
More lint fixes
OnFreund 186c4fc
var->let
OnFreund 257d066
let-const
OnFreund 60de9ef
Fix propagation
OnFreund b51e330
Fix user picker
bramkragten 62b2f57
Remove user id when none selected
bramkragten fe954d3
Move fetching users to users picker
bramkragten 010c589
Only allows selecting user once
bramkragten 7cd4287
Finals tweaks and localize
bramkragten c1b9bb2
clean up
bramkragten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { | ||
| css, | ||
| CSSResult, | ||
| customElement, | ||
| html, | ||
| LitElement, | ||
| property, | ||
| TemplateResult, | ||
| } from "lit-element"; | ||
| import { fireEvent } from "../../common/dom/fire_event"; | ||
| import type { PolymerChangedEvent } from "../../polymer-types"; | ||
| import type { HomeAssistant } from "../../types"; | ||
| import { fetchUsers, User } from "../../data/user"; | ||
| import "./ha-user-picker"; | ||
| import { mdiClose } from "@mdi/js"; | ||
| import memoizeOne from "memoize-one"; | ||
| import { guard } from "lit-html/directives/guard"; | ||
|
|
||
| @customElement("ha-users-picker") | ||
| class HaUsersPickerLight extends LitElement { | ||
| @property({ attribute: false }) public hass?: HomeAssistant; | ||
|
|
||
| @property() public value?: string[]; | ||
|
|
||
| @property({ attribute: "picked-user-label" }) | ||
| public pickedUserLabel?: string; | ||
|
|
||
| @property({ attribute: "pick-user-label" }) | ||
| public pickUserLabel?: string; | ||
|
|
||
| @property({ attribute: false }) | ||
| public users?: User[]; | ||
|
|
||
| protected firstUpdated(changedProps) { | ||
| super.firstUpdated(changedProps); | ||
| if (this.users === undefined) { | ||
| fetchUsers(this.hass!).then((users) => { | ||
| this.users = users; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| protected render(): TemplateResult { | ||
| if (!this.hass || !this.users) { | ||
| return html``; | ||
| } | ||
|
|
||
| const notSelectedUsers = this._notSelectedUsers(this.users, this.value); | ||
| return html` | ||
| ${guard([notSelectedUsers], () => | ||
| this.value?.map( | ||
| (user_id, idx) => html` | ||
| <div> | ||
| <ha-user-picker | ||
| .label=${this.pickedUserLabel} | ||
| .noUserLabel=${this.hass?.localize( | ||
| "ui.components.user-picker.remove_user" | ||
| )} | ||
| .index=${idx} | ||
| .hass=${this.hass} | ||
| .value=${user_id} | ||
| .users=${this._notSelectedUsersAndSelected( | ||
| user_id, | ||
| this.users, | ||
| notSelectedUsers | ||
| )} | ||
| @value-changed=${this._userChanged} | ||
| ></ha-user-picker> | ||
| <mwc-icon-button .userId=${user_id} @click=${this._removeUser}> | ||
| <ha-svg-icon .path=${mdiClose}></ha-svg-icon> | ||
| </mwc-icon-button> | ||
| </div> | ||
| ` | ||
| ) | ||
| )} | ||
| <ha-user-picker | ||
| .noUserLabel=${this.pickUserLabel || | ||
| this.hass?.localize("ui.components.user-picker.add_user")} | ||
| .hass=${this.hass} | ||
| .users=${notSelectedUsers} | ||
| .disabled=${!notSelectedUsers?.length} | ||
| @value-changed=${this._addUser} | ||
| ></ha-user-picker> | ||
| `; | ||
| } | ||
|
|
||
| private _notSelectedUsers = memoizeOne( | ||
| (users?: User[], currentUsers?: string[]) => | ||
| currentUsers | ||
| ? users?.filter( | ||
| (user) => !user.system_generated && !currentUsers.includes(user.id) | ||
| ) | ||
| : users?.filter((user) => !user.system_generated) | ||
| ); | ||
|
|
||
| private _notSelectedUsersAndSelected = ( | ||
| userId: string, | ||
| users?: User[], | ||
| notSelected?: User[] | ||
| ) => { | ||
| const selectedUser = users?.find((user) => user.id === userId); | ||
| if (selectedUser) { | ||
| return notSelected ? [...notSelected, selectedUser] : [selectedUser]; | ||
| } | ||
| return notSelected; | ||
| }; | ||
|
|
||
| private get _currentUsers() { | ||
| return this.value || []; | ||
| } | ||
|
|
||
| private async _updateUsers(users) { | ||
| this.value = users; | ||
| fireEvent(this, "value-changed", { | ||
| value: users, | ||
| }); | ||
| } | ||
|
|
||
| private _userChanged(event: PolymerChangedEvent<string>) { | ||
| event.stopPropagation(); | ||
| const index = (event.currentTarget as any).index; | ||
| const newValue = event.detail.value; | ||
| const newUsers = [...this._currentUsers]; | ||
| if (newValue === "") { | ||
| newUsers.splice(index, 1); | ||
| } else { | ||
| newUsers.splice(index, 1, newValue); | ||
| } | ||
| this._updateUsers(newUsers); | ||
| } | ||
|
|
||
| private async _addUser(event: PolymerChangedEvent<string>) { | ||
| event.stopPropagation(); | ||
| const toAdd = event.detail.value; | ||
| (event.currentTarget as any).value = ""; | ||
| if (!toAdd) { | ||
| return; | ||
| } | ||
| const currentUsers = this._currentUsers; | ||
| if (currentUsers.includes(toAdd)) { | ||
| return; | ||
| } | ||
|
|
||
| this._updateUsers([...currentUsers, toAdd]); | ||
| } | ||
|
|
||
| private _removeUser(event) { | ||
| const userId = (event.currentTarget as any).userId; | ||
| this._updateUsers(this._currentUsers.filter((user) => user !== userId)); | ||
| } | ||
|
|
||
| static get styles(): CSSResult { | ||
| return css` | ||
| :host { | ||
| display: block; | ||
| } | ||
| div { | ||
| display: flex; | ||
| align-items: center; | ||
| } | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "ha-users-picker": HaUsersPickerLight; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@bramkragten this doesn't work, because
ha-user-pickeris doing this:And changing the value doesn't change the selected item. What's the best way to resolve this?
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.
That's actually weird, because it's comparing to
ev.detail.item.dataset.userId, yet setting toev.detail.value:Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, that doesn't seem correct. I think
newValueshould beev.detail.value?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.
This is really strange. So
ev.detail.valuedoesn't even exist (which means thatthis.value = ev.detail.value;is probably wrong), andev.newValueis alwaysundefined.It seems like
ev.detail.item.dataset.userIdis indeed the right way to grab the currently selected user, but it's unclear how to programmatically select a user...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.
This is a red herring - this method on
ha-user-pickerisn't even called by setting value to"", and it's unclear thatvalueeven has any meaning (I thought it was called, because it was called twice when picking a user, so I assumed one of those was a result of setting value to"").I'm completely stumped 🤷♂️
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.
I'll put this PR on my todo list :-)
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.
thanks
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.
Any chance we can get it in time for the beta? The core feature is already merged
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.
I'll try 🙈
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.
Should be fixed now