Skip to content

Commit 9d8d87c

Browse files
author
MHA
committed
feat: refactor maxHeight to max_options
1 parent ced2b9d commit 9d8d87c

File tree

7 files changed

+63
-57
lines changed

7 files changed

+63
-57
lines changed

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge)](https://github.com/custom-components/hacs)
44

5-
Display an input_select entity as a list
5+
Display the options of an input_select entity as a clickable list card.
6+
In other words: the content of the dropdown menu is displayed as a card.
7+
8+
Some use cases:
9+
* Select with (too) many options
10+
* Options with long titles
11+
* Have all options directly shown
12+
* You dont't want the extra click to open the dropdown menu
613

714
## Usage
815

@@ -25,14 +32,14 @@ entity: input_select.scenes
2532
2633
## Options
2734
28-
| Name | Type | Requirement | Description | Default |
29-
| ----------------- | ------- | ------------ | ------------------------------------------- | ------------------- |
30-
| type | string | **Required** | `custom:select-list-card` |
31-
| entity | string | **Required** | Home Assistant input_select ID. | `none` |
32-
| name | string | **Optional** | Card name | `` |
33-
| truncate | boolean | **Optional** | Truncate option text | `true` |
34-
| scrollInToView | boolean | **Optional** | Scroll active item in to view | `true` |
35-
| maxHeight | string | **Optional** | Max height of the list | `350` |
35+
| Name | Type | Requirement | Description | Default |
36+
| ------------------ | ------- | ------------ | ------------------------------------------- | ------------------- |
37+
| type | string | **Required** | `custom:select-list-card` | |
38+
| entity | string | **Required** | Entity id of an input_select | |
39+
| name | string | **Optional** | Card name | `` |
40+
| truncate | boolean | **Optional** | Truncate option text | `true` |
41+
| scroll_to_selected | boolean | **Optional** | Scroll to selected option | `true` |
42+
| max_options | number | **Optional** | Max options before scroll bar shows | `5` |
3643

3744

3845
## Installation

dist/select-list-card.js

Lines changed: 22 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "select-list-card",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Lovelace select-list-card",
55
"keywords": [
66
"home-assistant",

src/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const CARD_VERSION = '0.2.0';
1+
export const CARD_VERSION = '0.2.1';

src/editor.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
5252
return true;
5353
}
5454

55-
get _scrollInToView(): boolean {
55+
get _scroll_to_selected(): boolean {
5656
if (this._config) {
57-
return this._config.scrollInToView || true;
57+
return this._config.scroll_to_selected || true;
5858
}
5959

6060
return true;
6161
}
6262

63-
get _maxHeight(): string {
63+
get _max_options(): number {
6464
if (this._config) {
65-
return this._config.maxHeight || '350';
65+
return this._config.max_options || 5;
6666
}
67-
return '350';
67+
return 5;
6868
}
6969

7070
protected render(): TemplateResult | void {
@@ -103,9 +103,9 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
103103
</div>
104104
<div class="row">
105105
<paper-input
106-
label="Max height (0 = no max)"
107-
.value=${this._maxHeight}
108-
.configValue=${'maxHeight'}
106+
label="Max options (0 = show all)"
107+
.value=${this._max_options}
108+
.configValue=${'max_options'}
109109
type="number"
110110
@value-changed=${this._valueChanged}
111111
></paper-input>
@@ -121,11 +121,11 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
121121
</div>
122122
<div class="row">
123123
<ha-switch
124-
aria-label=${`Toggle scroll in to view ${this._truncate ? 'off' : 'on'}`}
125-
.checked=${this._scrollInToView}
126-
.configValue=${'scrollInToView'}
124+
aria-label=${`Toggle scroll to selected ${this._truncate ? 'off' : 'on'}`}
125+
.checked=${this._scroll_to_selected}
126+
.configValue=${'scroll_to_selected'}
127127
@change=${this._valueChanged}
128-
>Scroll selected in to view?</ha-switch
128+
>Scroll to selected?</ha-switch
129129
>
130130
</div>
131131
</div>
@@ -151,16 +151,17 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
151151
return;
152152
}
153153
const target = ev.target;
154-
if (this[`_${target.configValue}`] === target.value) {
154+
const value = target.configValue === 'max_options' ? parseInt(target.value) : target.value;
155+
if (this[`_${target.configValue}`] === value) {
155156
return;
156157
}
157158
if (target.configValue) {
158-
if (target.value === '') {
159+
if (value === '') {
159160
delete this._config[target.configValue];
160161
} else {
161162
this._config = {
162163
...this._config,
163-
[target.configValue]: target.checked !== undefined ? target.checked : target.value,
164+
[target.configValue]: target.checked !== undefined ? target.checked : value,
164165
};
165166
}
166167
}
@@ -170,11 +171,10 @@ export class SelectListCardEditor extends LitElement implements LovelaceCardEdit
170171
static get styles(): CSSResult {
171172
return css`
172173
.row {
173-
margin-bottom: 15px;
174+
margin-bottom: 5px;
174175
}
175176
.values {
176177
padding-left: 0px;
177-
background: var(--secondary-background-color);
178178
}
179179
paper-dropdown-menu {
180180
width: 100%;

src/select-list-card.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export class SelectListCard extends LitElement {
5353
this.config = {
5454
name: '',
5555
truncate: true,
56-
scrollInToView: true,
56+
scroll_to_selected: true,
57+
max_options: 5,
5758
...config,
5859
};
5960
}
@@ -65,7 +66,7 @@ export class SelectListCard extends LitElement {
6566
protected render(): TemplateResult | void {
6667
const selected = this.hass.states[this.config.entity].state;
6768
const options = this.hass.states[this.config.entity].attributes.options;
68-
const style = this.config.maxHeight !== '0' ? `max-height: ${this.config.maxHeight}px` : '';
69+
const style = this.config.max_options === 0 ? '' : `max-height: ${(this.config.max_options || 5) * 48}px`;
6970
return html`
7071
<ha-card .header=${this.config.name} aria-label=${`Select list card: ${this.config.entity}`}>
7172
<paper-listbox
@@ -109,13 +110,12 @@ export class SelectListCard extends LitElement {
109110
private async _selectedOptionChanged(ev: any): Promise<any> {
110111
const option = ev.detail.item.innerText.trim();
111112
const selected = this.hass.states[this.config.entity].state;
112-
if (this.config.scrollInToView) {
113+
if (this.config.scroll_to_selected) {
113114
ev.path[0].scrollTop = ev.detail.item.offsetTop - (ev.path[0].offsetTop + 8);
114115
}
115116
if (option === selected) {
116117
return;
117118
}
118-
console.log('EV', ev);
119119
await this.setInputSelectOption(this.hass, this.config.entity, option);
120120
}
121121

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface SelectListCardConfig extends LovelaceCardConfig {
55
name?: string;
66
entity: string;
77
truncate?: boolean;
8-
scrollInToView?: boolean;
9-
maxHeight: string;
8+
scroll_to_selected?: boolean;
9+
max_options?: number;
1010
test_gui?: boolean;
1111
}

0 commit comments

Comments
 (0)