-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxenon-masked-numeric.html
82 lines (74 loc) · 3.17 KB
/
xenon-masked-numeric.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
<!--
@demo demo/index.html
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input.html" />
<link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
<dom-module id="xenon-masked-numeric">
<template>
<style>
:host{display:block;}
</style>
<paper-input-container id="container" invalid="{{invalid}}" always-float-label="[[alwaysFloatLabel]]">
<label hidden$="[[!label]]">[[label]]</label>
<div class="horizontal layout">
<input is="iron-input"
id="input"
class="flex"
aria-labelledby$="[[_ariaLabelledBy]]"
aria-describedby$="[[_ariaDescribedBy]]"
required$="[[required]]"
bind-value="{{value}}"
name$="[[name]]"
disabled$="[[disabled]]"
invalid="{{invalid}}"
autofocus$="[[autofocus]]"
inputmode$="[[inputmode]]"
placeholder$="[[placeholder]]"
readonly$="[[readonly]]"
pattern$="[[regex]]"
maxlength$="[[length]]"
type="tel">
</div>
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error id="error">[[errorMessage]]</paper-input-error>
</template>
</paper-input-container>
</template>
<script>
Polymer({
is: 'xenon-masked-numeric',
behaviors: [
Polymer.PaperInputBehavior,
Polymer.IronValidatableBehavior,
Polymer.IronFormElementBehavior
],
properties: {
value: { type: String, notify: true, observer: '_valueChanged' },
mask: { type: String },
length: { type: Number },
regex: { type: String, value: null }
},
validate: function () {
return this.$.input.validate();
},
_valueChanged: function (newValue, oldValue) {
if (newValue == null) return;
// remove all incoming non-numeric characters
newValue = newValue.replace(/[^0-9]/g, "");
// splice the separators back into the string
for (var i = 0; i < this.mask.length; i++) {
if (i > newValue.length) break;
var char = this.mask.charAt(i);
if (char != "#")
newValue = newValue.slice(0, i) + char + newValue.slice(i);
}
// if the new string is longer than the mask, trim it
if (newValue.length > this.mask.length)
newValue = newValue.slice(0, this.mask.length)
// if this string ends in a separator, trim it
this.value = newValue.replace(/[^0-9]$/, "");
}
})
</script>
</dom-module>