forked from vaadin/vaadin-combo-box
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvaadin-combo-box.html
389 lines (327 loc) · 9.98 KB
/
vaadin-combo-box.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!--
@license
Copyright (c) 2015 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<!--
`<vaadin-combo-box>` is a combo box element combining a dropdown list with an
input field for filtering the list of items. If you want to replace the default
input field with a custom implementation, you should use the
[`<vaadin-combo-box-light>`](#vaadin-combo-box-light) element.
Items in the dropdown list must be provided as a list of `String` values.
Defining the items is done using the `items` property, which can be assigned
with data-binding, using an attribute or directly with the JavaScript property.
```html
<vaadin-combo-box
label="Fruit"
items="[[data]]">
</vaadin-combo-box>
```
```js
combobox.items = ['apple', 'orange', 'banana'];
```
When the selected `value` is changed, a `value-changed` event is triggered.
This element is also extended with the `IronFormElementBehavior` to
enable usage within an `iron-form`.
### Item Template
`<vaadin-combo-box>` supports using custom item template provided in the light
DOM:
```html
<vaadin-combo-box items='[{"label": "Hydrogen", "value": "H"}]'>
<template>
[[index]]: [[item.label]] <b>[[item.value]</b>
</template>
</vaadin-combo-box>
```
The following properties are available for item template bindings:
Property name | Type | Description
--------------|------|------------
`index`| Number | Index of the item in the `items` array
`item` | String or Object | The item reference
`selected` | Boolean | True when item is selected
`focused` | Boolean | True when item is focused
See the [Item Template Live Demos](demo/item-template.html) for more examples.
### Styling
There are custom properties and mixins you can use to style the component:
Custom property | Description | Default
----------------|-------------|-------------
`--vaadin-combo-box-overlay-max-height` | Property that determines the max height of overlay | `65vh`
@element vaadin-combo-box
@demo demo/index.html
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-input/iron-input.html">
<link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.html">
<link rel="import" href="../iron-a11y-announcer/iron-a11y-announcer.html">
<link rel="import" href="../paper-input/paper-input-container.html">
<link rel="import" href="../paper-input/paper-input-error.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="vaadin-combo-box-behavior.html">
<link rel="import" href="vaadin-combo-box-overlay.html">
<link rel="import" href="vaadin-combo-box-shared-styles.html">
<link rel="import" href="vaadin-combo-box-icons.html">
<dom-module id="vaadin-combo-box">
<template>
<style include="vaadin-combo-box-shared-styles">
:host {
display: block;
}
:host > #overlay {
display: none;
}
paper-input-container {
position: relative;
padding: 0;
}
paper-icon-button.toggle-button,
:host ::content paper-icon-button.toggle-button {
position: absolute;
top: 10px;
right: 8px;
margin: 0;
padding: 0;
width: 6px;
height: 4px;
line-height: 0;
color: #676767;
cursor: pointer;
}
paper-input-container paper-icon-button:hover,
paper-input-container ::content paper-icon-button:hover,
:host([opened]) paper-input-container paper-icon-button,
:host([opened]) paper-input-container ::content paper-icon-button {
color: rgba(0, 0, 0, .54);
}
:host([opened]) paper-input-container ::content paper-icon-button:hover,
:host([opened]) paper-input-container paper-icon-button:hover {
color: rgba(0, 0, 0, .86);
}
:host([opened]) paper-input-container {
/* Keep the paper-input-container above the dropdown. */
z-index: 20;
}
#input::-ms-clear {
display: none;
}
#input {
box-sizing: border-box;
color: #444444;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 12px;
padding-left: 8px;
padding-right: 8px;
}
#input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
color: #444444;
font-size: 10px;
}
#input::-moz-placeholder { /* Firefox 19+ */
color: #444444;
font-size: 10px;
}
#input:-ms-input-placeholder { /* IE 10+ */
color: #444444;
font-size: 10px;
}
#input:-moz-placeholder { /* Firefox 18- */
color: #444444;
font-size: 10px;
}
:host([opened][has-value]) #input {
margin-right: -32px;
}
paper-input-container {
--paper-input-container-underline: {
display: none;
};
}
paper-input-container {
--paper-input-container-underline-focus: {
display: none
};
}
</style>
<paper-input-container
id="inputContainer"
disabled$="[[disabled]]"
no-label-float="[[noLabelFloat]]"
always-float-label="[[_computeAlwaysFloatLabel(alwaysFloatLabel,placeholder)]]"
auto-validate$="[[autoValidate]]"
invalid="[[invalid]]">
<label on-down="_preventDefault" id="label" hidden$="[[!label]]" aria-hidden="true" on-tap="_openAsync">[[label]]</label>
<content select="[prefix]"></content>
<input
is="iron-input"
id="input"
type="text"
role="combobox"
autocomplete="off"
autocapitalize="none"
bind-value="{{_inputElementValue}}"
aria-label$="[[label]]"
aria-expanded$="[[_getAriaExpanded(opened)]]"
aria-autocomplete="list"
disabled$="[[disabled]]"
invalid="{{invalid}}"
prevent-invalid-input="[[preventInvalidInput]]"
allowed-pattern="[[allowedPattern]]"
pattern$="[[pattern]]"
required$="[[required]]"
autofocus$="[[autofocus]]"
inputmode$="[[inputmode]]"
name$="[[name]]"
placeholder$="[[placeholder]]"
readonly$="[[readonly]]"
size$="[[size]]"
on-input="_inputValueChanged"
on-blur="_onBlur"
on-change="_stopPropagation"
key-event-target>
<content select="[suffix]"></content>
<content select=".toggle-button">
<paper-icon-button
noink
id="toggleIcon"
tabindex="-1"
src="images/down-arrow.svg"
aria-label="Toggle"
aria-expanded$="[[_getAriaExpanded(opened)]]"
class="toggle-button rotate-on-open">
</paper-icon-button>
</content>
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error>[[errorMessage]]</paper-input-error>
</template>
</paper-input-container>
<vaadin-combo-box-overlay
id="overlay"
opened$=[[opened]]
position-target="[[_getPositionTarget()]]"
_focused-index="[[_focusedIndex]]"
_item-label-path="[[itemLabelPath]]"
on-down="_onOverlayDown"
loading="[[loading]]"
on-mousedown="_preventDefault"
vertical-offset="-2">
</vaadin-combo-box-overlay>
</template>
</dom-module>
<script>
Polymer({
is: 'vaadin-combo-box',
behaviors: [
Polymer.IronValidatableBehavior,
vaadin.elements.combobox.ComboBoxBehavior
],
properties: {
/**
* The label for this element.
*/
label: {
type: String,
reflectToAttribute: true
},
/**
* Set to true to disable the floating label.
*/
noLabelFloat: {
type: Boolean,
value: false
},
/**
* Set to true to always float the label.
*/
alwaysFloatLabel: {
type: Boolean,
value: false
},
/**
* Set to true to auto-validate the input value.
*/
autoValidate: {
type: Boolean,
value: false
},
/**
* Set to true to disable this input.
*/
disabled: {
type: Boolean,
value: false
},
/**
* Set to true to prevent the user from entering invalid input.
*/
preventInvalidInput: {
type: Boolean
},
/**
* Set this to specify the pattern allowed by `preventInvalidInput`.
*/
allowedPattern: {
type: String
},
/**
* A pattern to validate the `input` with.
*/
pattern: {
type: String
},
/**
* Set to true to mark the input as required.
*/
required: {
type: Boolean,
value: false
},
/**
* The error message to display when the input is invalid.
*/
errorMessage: {
type: String
},
autofocus: {
type: Boolean
},
inputmode: {
type: String
},
name: {
type: String
},
/**
* A placeholder string in addition to the label. If this is set, the label will always float.
*/
placeholder: {
type: String,
// need to set a default so _computeAlwaysFloatLabel is run
value: ''
},
readonly: {
type: Boolean,
value: false
},
size: {
type: Number
}
},
attached: function() {
this._setInputElement(this.$.input);
// Use the default toggle/clear or one replaced in light DOM.
this._toggleElement = Polymer.dom(this).querySelector('.toggle-button') || this.$.toggleIcon;
this._preventInputBlur();
},
detached: function() {
this._restoreInputBlur();
},
_computeAlwaysFloatLabel: function(alwaysFloatLabel, placeholder) {
return placeholder || alwaysFloatLabel;
},
_getPositionTarget: function() {
return this.$.inputContainer;
},
_getAriaExpanded: function(value) {
return value.toString();
}
});
</script>