This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropdown-select.html
265 lines (242 loc) · 6.72 KB
/
dropdown-select.html
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-listbox/paper-listbox.html">
<!--
`dropdown-select`
An easy to use, works as expected dropdown menu. Add slotted items like follows:
<dropdown-select
allow-outside-scroll
always-float-label
dynamic-align
error-message="Required."
horizontal-align="left"
label="Select an item."
no-animations
no-label-float
placeholder="none"
restore-focus-on-close
vertical-align="bottom"
vertical-offset="10">
<paper-item value="100">100 things</paper-item>
<paper-item value="1000">Another value</paper-item>
<paper-item value="10">Value is 10, but you will see this text</paper-item>
</dropdown-select>
@demo demo/index.html
-->
<dom-module id="dropdown-select">
<template>
<style>
:host {
display: block;
}
paper-listbox ::slotted(paper-item) {
display: block;
width: 100%;
min-height: 2em;
vertical-align: text-top;
line-height: 2em;
@apply --dropdown-select-items;
}
paper-listbox paper-listbox {
@apply --dropdown-listbox;
}
</style>
<paper-dropdown-menu id="menu"
allow-outside-scroll$="[[allowOutsideScroll]]"
always-float-label$="[[alwaysFloatLabel]]"
dynamic-align$="[[dynamicAlign]]"
error-message$="[[errorMessage]]"
horizontal-align$="[[horizontalAlign]]"
label$="[[label]]"
no-animations$="[[noAnimations]]"
no-label-float$="[[noLabelFloat]]"
on-selected-item-changed="_getSelectedValue"
placeholder$="[[placeholder]]"
restore-focus-on-close$="[[restoreFocusOnClose]]"
vertical-align$="[[verticalAlign]]"
vertical-offset$="[[verticalOffset]]">
<paper-listbox id="listbox" slot="dropdown-content" class="dropdown-content">
<slot id="content"></slot>
</paper-listbox>
</paper-dropdown-menu>
</template>
<script>
Polymer({
is: 'dropdown-select',
listeners: {
'paper-dropdown-open': '_onOpen',
'paper-dropdown-close': '_onClose'
},
properties: {
/**
* Set to true in order to prevent scroll from being constrained
* to the dropdown when it opens.
*/
allowOutsideScroll: {
type: Boolean,
value: false
},
/**
* Set to true to always float the label.
*/
alwaysFloatLabel: {
type: Boolean,
value: false,
},
/**
* If true, the `horizontalAlign` and `verticalAlign` properties will
* be considered preferences instead of strict requirements when
* positioning the dropdown and may be changed if doing so reduces
* the area of the dropdown falling outside of `fitInto`.
*/
dynamicAlign: {
type: Boolean
},
/**
* The error message to display when invalid.
*/
errorMessage: {
type: String
},
/**
* The orientation against which to align the menu dropdown
* horizontally relative to the dropdown trigger.
*/
horizontalAlign: {
type: String,
value: 'right'
},
/**
* The label of the select menu
*/
label: {
type: String,
value: 'Select an option.',
},
/**
* Set to true to disable animations when opening and closing the
* dropdown.
*/
noAnimations: {
type: Boolean,
value: false
},
/**
* Set to true to disable the floating label.
*/
noLabelFloat: {
type: Boolean,
value: false,
},
/**
* True if the dropdown is open. Otherwise, false.
*/
opened: {
type: Boolean,
value: false,
},
/**
* The placeholder for the dropdown.
*/
placeholder: {
type: String
},
/**
* Whether focus should be restored to the dropdown when the menu closes.
*/
restoreFocusOnClose: {
type: Boolean,
value: true
},
/**
* The last selected item.
*/
selectedItem: {
type: Object
},
/**
* The index of the selected item
*/
selectedItemIndex: {
type: Number,
value: null
},
/**
* The label of the selected item
*/
selectedItemLabel: {
type: String,
value: null
},
/**
* The default value
*/
value: {
type: String,
value: null
},
/**
* The orientation against which to align the menu dropdown
* vertically relative to the dropdown trigger.
*/
verticalAlign: {
type: String,
value: 'top'
},
/**
* Overrides the vertical offset computed in
* _computeMenuVerticalOffset.
*/
verticalOffset: {
type: Number
},
},
/**
* Get the value of the selected item.
*/
_getSelectedValue: function(e) {
if (e.detail.value !== null){
this.value = e.detail.value.getAttribute('value');
this._setSelectedValues();
this.fire('change', {'value': this.value}); //support for old version
this.fire('dropdown-select-changed', this);
}
},
/**
* Sets the opened property to true
*/
_onOpen: function(e) {
this.opened = true;
},
/**
* Sets the opened property to false
*/
_onClose: function(e) {
this.opened = false;
},
/**
* Get the value of the selected item.
*/
_setSelectedValues: function() {
this.selectedItem = this.$.menu.selectedItem;
this.selectedItemLabel = this.$.menu.selectedItemLabel;
this.selectedItemIndex = this.$.listbox.selected;
},
/**
* Set the index of the selected item.
*/
attached: function() {
let children = this.$.listbox.querySelectorAll('paper-item');
if(children !== undefined && children !== null){
for (let i=0;i<children.length;i++) {
if(this.value === children[i].getAttribute('value')) {
this.$.listbox.selected = i;
this._setSelectedValues();
}
}
}
},
});
</script>
</dom-module>