-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add multi select component to ha-form #4247
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
6 commits
Select commit
Hold shift + click to select a range
cfd169d
Add multi select component
bramkragten a543e05
Apply suggestions from code review
bramkragten 8b3cede
Comments
bramkragten c85cd8a
update
bramkragten 6c842f3
Fix
bramkragten 5b23c50
Refactor to dropdown menu
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import "@polymer/paper-checkbox/paper-checkbox"; | ||
| import "@polymer/paper-menu-button/paper-menu-button"; | ||
| import "@polymer/paper-input/paper-input"; | ||
| import "@polymer/paper-item/paper-icon-item"; | ||
| import "@polymer/paper-listbox/paper-listbox"; | ||
| import "@polymer/paper-ripple/paper-ripple"; | ||
| import { | ||
| customElement, | ||
| html, | ||
| LitElement, | ||
| property, | ||
| query, | ||
| TemplateResult, | ||
| CSSResult, | ||
| css, | ||
| } from "lit-element"; | ||
| import { fireEvent } from "../../common/dom/fire_event"; | ||
| import { | ||
| HaFormElement, | ||
| HaFormMultiSelectData, | ||
| HaFormMultiSelectSchema, | ||
| } from "./ha-form"; | ||
|
|
||
| @customElement("ha-form-multi_select") | ||
| export class HaFormMultiSelect extends LitElement implements HaFormElement { | ||
| @property() public schema!: HaFormMultiSelectSchema; | ||
| @property() public data!: HaFormMultiSelectData; | ||
| @property() public label!: string; | ||
| @property() public suffix!: string; | ||
| @property() private _init = false; | ||
| @query("paper-menu-button") private _input?: HTMLElement; | ||
|
|
||
| public focus(): void { | ||
| if (this._input) { | ||
| this._input.focus(); | ||
| } | ||
| } | ||
|
|
||
| protected render(): TemplateResult { | ||
| const options = Array.isArray(this.schema.options) | ||
| ? this.schema.options | ||
| : Object.entries(this.schema.options!); | ||
|
|
||
| return html` | ||
| <paper-menu-button horizontal-align="right" vertical-offset="8"> | ||
| <div class="dropdown-trigger" slot="dropdown-trigger"> | ||
| <paper-ripple></paper-ripple> | ||
| <paper-input | ||
| id="input" | ||
| type="text" | ||
| readonly | ||
| value=${this.data | ||
| .map((value) => this.schema.options![value] || value) | ||
| .join(", ")} | ||
| label=${this.label} | ||
| input-role="button" | ||
| input-aria-haspopup="listbox" | ||
| autocomplete="off" | ||
| > | ||
| <iron-icon | ||
| icon="paper-dropdown-menu:arrow-drop-down" | ||
| suffix | ||
| slot="suffix" | ||
| ></iron-icon> | ||
| </paper-input> | ||
| </div> | ||
| <paper-listbox | ||
| multi | ||
| slot="dropdown-content" | ||
| attr-for-selected="item-value" | ||
| .selectedValues=${this.data} | ||
| @selected-items-changed=${this._valueChanged} | ||
| @iron-select=${this._onSelect} | ||
| > | ||
| ${// TS doesn't work with union array types https://github.com/microsoft/TypeScript/issues/36390 | ||
| // @ts-ignore | ||
| options.map((item: string | [string, string]) => { | ||
| const value = this._optionValue(item); | ||
| return html` | ||
| <paper-icon-item .itemValue=${value}> | ||
| <paper-checkbox | ||
| .checked=${this.data.includes(value)} | ||
| slot="item-icon" | ||
| ></paper-checkbox> | ||
| ${this._optionLabel(item)} | ||
| </paper-icon-item> | ||
| `; | ||
| })} | ||
| </paper-listbox> | ||
| </paper-menu-button> | ||
| `; | ||
| } | ||
|
|
||
| protected firstUpdated() { | ||
| this.updateComplete.then(() => { | ||
| const input = (this.shadowRoot?.querySelector("paper-input") | ||
| ?.inputElement as any)?.inputElement; | ||
| if (input) { | ||
| input.style.textOverflow = "ellipsis"; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private _optionValue(item: string | string[]): string { | ||
| return Array.isArray(item) ? item[0] : item; | ||
| } | ||
|
|
||
| private _optionLabel(item: string | string[]): string { | ||
| return Array.isArray(item) ? item[1] || item[0] : item; | ||
| } | ||
|
|
||
| private _onSelect(ev: Event) { | ||
| ev.stopPropagation(); | ||
| } | ||
|
|
||
| private _valueChanged(ev: CustomEvent): void { | ||
| if (!ev.detail.value || !this._init) { | ||
| // ignore first call because that is the init of the component | ||
| this._init = true; | ||
| return; | ||
| } | ||
|
|
||
| fireEvent( | ||
| this, | ||
| "value-changed", | ||
| { | ||
| value: ev.detail.value.map((element) => element.itemValue), | ||
| }, | ||
| { bubbles: false } | ||
| ); | ||
| } | ||
|
|
||
| static get styles(): CSSResult { | ||
| return css` | ||
| paper-menu-button { | ||
| display: block; | ||
| padding: 0; | ||
| --paper-item-icon-width: 34px; | ||
| } | ||
| paper-ripple { | ||
| top: 12px; | ||
| left: 0px; | ||
| bottom: 8px; | ||
| right: 0px; | ||
| } | ||
| paper-input { | ||
| text-overflow: ellipsis; | ||
| } | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "ha-form-multi_select": HaFormMultiSelect; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.