-
Notifications
You must be signed in to change notification settings - Fork 1
/
time-field-input.js
132 lines (108 loc) · 2.9 KB
/
time-field-input.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
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import '@vaadin/vaadin-text-field/vaadin-text-field.js';
import '@vaadin/vaadin-button/vaadin-button.js';
import '@polymer/iron-icons/iron-icons.js';
import './time-field-styles.js';
/**
* `time-field-input`
*
* A number input based on the `vaadin-text-field`.
*/
class TimeNumberInput extends PolymerElement {
static get template() {
return html`
<style include="time-field-styles">
:host {
display: flex;
min-width: 0;
}
[part="time-input"] {
min-width: 0;
}
[part="decrease"],
[part="increase"] {
margin: 0;
padding: 0;
min-width: 0;
}
[part="decrease"]:not([disabled]),
[part="increase"]:not([disabled]) {
color: var(--time-field-controls-color, var(--time-field-controls-default-color));
}
</style>
<vaadin-text-field
part="time-input"
value="{{value}}"
pattern="[0-9]{0,2}"
on-blur="_checkValue"
disabled="[[disabled]]"
prevent-invalid-input>
<vaadin-button
part="decrease"
theme="icon tertiary"
slot="prefix"
disabled="[[disabled]]"
on-click="_decreaseValue">
<iron-icon icon="icons:remove"></iron-icon>
</vaadin-button>
<vaadin-button
part="increase"
theme="icon tertiary"
slot="suffix"
disabled="[[disabled]]"
on-click="_increaseValue">
<iron-icon icon="icons:add"></iron-icon>
</vaadin-button>
</vaadin-text-field>
`;
}
static get properties() {
return {
value: {
type: String,
notify: true
},
min: {
type: Number,
value: 0
},
max: {
type: Number,
value: 0
},
disabled: {
type: Boolean,
reflectToAttribute: true
}
};
}
_checkValue() {
if (!this.value) {
return;
}
let intValue = parseInt(this.value, 10);
if (isNaN(intValue)) {
intValue = this.min;
}
if (intValue < this.min) {
intValue = this.min;
} else if (intValue > this.max) {
intValue = this.max;
}
this.value = this._zeroPadValue(intValue);
}
_decreaseValue() {
const intValue = parseInt(this.value, 10);
const newValue = isNaN(intValue) || intValue <= this.min ? this.max : intValue - 1;
this.value = this._zeroPadValue(newValue);
}
_increaseValue() {
const intValue = parseInt(this.value, 10);
const newValue = isNaN(intValue) || intValue >= this.max ? this.min : intValue + 1;
this.value = this._zeroPadValue(newValue);
}
_zeroPadValue(intValue) {
return intValue < 10 ? '0' + intValue : intValue.toString();
}
}
window.customElements.define('time-field-input', TimeNumberInput);