-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectMany.js
152 lines (140 loc) · 4.12 KB
/
SelectMany.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
html,
css,
LitElement
} from "lit";
import {
styleMap
} from "lit/directives/style-map.js";
export class SelectMany extends LitElement {
static get styles() {
return css`
:host {
display: inline-block;
position: relative;
min-width: 250px;
max-width: 350px;
font-size: 13.3333px;
}
:host,
.dropdown {
background: white;
border: 1px black solid;
}
.dropdown {
position: absolute;
left: 0;
right: 0;
max-height: 500px;
overflow: auto;
}
.dropdown[closed] {
display: none;
}
.option[data-selected] {
background: #5380cc;
color: white;
}
.option:not([data-selected]):hover {
background: lightgray;
}
.display,
.option {
box-sizing: border-box;
height: 21px;
padding: 2px 16px 2px 4px;
text-align: left;
}
`;
}
static get properties() {
return {
options: {
type: Object
},
open: {
type: Boolean
},
};
}
toggle = (val) => this.selected.delete(val) || this.selected.add(val);
onChange(e) {
e.preventDefault();
e.stopImmediatePropagation();
e.target.focus();
const val = e.target.getAttribute("data-value");
this.toggle(val);
this.requestUpdate();
this.value = Array.from(this.selected);
const event = new Event("change", {
composed: true
});
this.dispatchEvent(event);
}
willUpdate(changedProperties) {
if (changedProperties.has("options")) {
this.selected = new Set();
this.open = false;
}
}
constructor() {
super();
this.selected = new Set();
this.options = {};
this.open = false;
this.dropdownHeight = 0;
}
connectedCallback() {
super.connectedCallback();
this.addEventListener("focusout", () => {
this.open = false;
});
this.addEventListener("focusin", () => {
this.open = true;
});
}
getDisplayText() {
if (this.selected.size)
return Array.from(this.selected)
.map((k) => this.options[k])
.join(", ");
else return "-";
}
willUpdate() {
const dispalyRect = this.shadowRoot
.querySelector(".display")
?.getBoundingClientRect();
if (dispalyRect) {
const intViewportHeight = window.innerHeight;
this.bottomMargin = 24; // This is just because I think it looks nicer not right up against the bottom edge
let spaceBelow = Math.floor(intViewportHeight - dispalyRect.bottom - this.bottomMargin);
let autoHeight = this.options.length * 21 + 2;
this.dropdownHeight = Math.min(spaceBelow, autoHeight) + 'px';
}
}
render() {
return html`
<div class="display" tabindex="0">${this.getDisplayText()}</div>
<div
class="dropdown"
?closed="${!this.open}"
style=${styleMap({height: this.dropdownHeight})}
>
${Object.entries(this.options).map(
([val, name]) => html`
<div
class="option"
tabindex="-1"
data-value="${val}"
?data-selected=${this.selected.has(val)}
@click="${this.onChange}"
>
${name}
</div>
`
)}
</div>
`;
}
}
customElements.define("select-many", SelectMany);