-
Notifications
You must be signed in to change notification settings - Fork 24
/
vaadin-number-field.html
199 lines (167 loc) · 4.9 KB
/
vaadin-number-field.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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../vaadin-themable-mixin/vaadin-themable-mixin.html">
<link rel="import" href="vaadin-text-field.html">
<dom-module id="vaadin-number-field-default-theme">
</dom-module>
<dom-module id="vaadin-number-field-template">
<template>
<style>
:host {
width: 120px;
}
:host([has-controls]) [part="value"] {
text-align: center;
}
/* Value control styles */
[part="decrease-button"],
[part="increase-button"] {
display: inline-block;
cursor: pointer;
padding: 0 8px;
opacity: 0.5;
}
[part="decrease-button"][hidden],
[part="increase-button"][hidden] {
display: none;
}
[part="decrease-button"]::before {
content: "−";
}
[part="increase-button"]::before {
content: "+";
}
/* Hide the native arrow icons */
[part="value"]::-webkit-outer-spin-button,
[part="value"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
[part="value"] {
-moz-appearance: textfield;
}
</style>
<div
part="decrease-button"
on-click="_decreaseValue"
hidden$="[[!hasControls]]">
</div>
<div
part="increase-button"
on-click="_increaseValue"
hidden$="[[!hasControls]]">
</div>
</template>
<script>
(function() {
/**
* `<vaadin-number-field>` is a Polymer 2 element for number field control in forms.
*
* ```html
* <vaadin-number-field label="Number">
* </vaadin-number-field>
* ```
*
* @memberof Vaadin
* @extends Vaadin.TextFieldElement
* @demo demo/index.html
*/
let memoizedTemplate;
class NumberFieldElement extends Vaadin.TextFieldElement {
static get is() {
return 'vaadin-number-field';
}
static get properties() {
return {
/**
* Set to true to display value increase/decrease controls.
*/
hasControls: {
type: Boolean,
value: false,
reflectToAttribue: true
},
/**
* The minimum value of the field.
*/
min: {
type: Number,
reflectToAttribue: true,
observer: '_minChanged'
},
/**
* The maximum value of the field.
*/
max: {
type: Number,
reflectToAttribue: true,
observer: '_maxChanged'
}
};
}
ready() {
super.ready();
this.$.input.type = 'number';
}
static get template() {
if (!memoizedTemplate) {
// Clone the superclass template
memoizedTemplate = super.template.cloneNode(true);
// Retrieve this element's dom-module template
const thisTemplate = Polymer.DomModule.import(this.is + '-template', 'template');
const decreaseButton = thisTemplate.content.querySelector('[part="decrease-button"]');
const increaseButton = thisTemplate.content.querySelector('[part="increase-button"]');
const styles = thisTemplate.content.querySelector('style');
// Add the buttons and styles to the text-field template
const inputField = memoizedTemplate.content.querySelector('[part="input-field"]');
const prefixSlot = memoizedTemplate.content.querySelector('[name="prefix"]');
inputField.insertBefore(decreaseButton, prefixSlot);
inputField.appendChild(increaseButton);
memoizedTemplate.content.appendChild(styles);
return memoizedTemplate;
}
}
_decreaseValue() {
if (this.disabled) {
return;
}
if (this.min) {
if (this.value > this.min) {
this.value--;
}
} else {
this.value--;
}
}
_increaseValue() {
if (this.disabled) {
return;
}
if (this.max) {
if (this.value < this.max) {
this.value++;
}
} else {
this.value++;
}
}
_minChanged() {
this.$.input.min = this.min;
}
_maxChanged() {
this.$.input.max = this.max;
}
}
window.customElements.define(NumberFieldElement.is, NumberFieldElement);
/**
* @namespace Vaadin
*/
window.Vaadin = window.Vaadin || {};
Vaadin.NumberFieldElement = NumberFieldElement;
})();
</script>
</dom-module>