-
Notifications
You must be signed in to change notification settings - Fork 2
/
br-cel-input.html
126 lines (103 loc) · 3.82 KB
/
br-cel-input.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input-behavior.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="../iron-input/iron-input.html">
<link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
<link rel="import" href="br-cel-validator.html">
<!--
`br-cel-input`
@demo demo/index.html
-->
<dom-module id="br-cel-input">
<template>
<style>
:host {
display: block;
}
</style>
<paper-input-container id="container"
auto-validate="[[autoValidate]]"
attr-for-value="bind-value"
disabled$="[[disabled]]"
no-label-float="[[noLabelFloat]]"
always-float-label="[[_computeAlwaysFloatLabel(alwaysFloatLabel,placeholder)]]"
invalid="[[invalid]]">
<label hidden$="[[!label]]">[[label]]</label>
<br-cel-validator></br-cel-validator>
<input is="iron-input" id="input"
aria-labelledby$="[[_ariaLabelledBy]]"
aria-describedby$="[[_ariaDescribedBy]]"
required$="[[required]]"
validator="br-cel-validator"
type="text"
allowed-pattern="[0-9\(\)\s-]"
prevent-invalid-input
maxlength="15"
bind-value="{{value}}"
name$="[[name]]"
disabled$="[[disabled]]"
invalid="{{invalid}}"
autofocus$="[[autofocus]]"
inputmode$="[[inputmode]]"
placeholder$="[[placeholder]]"
readonly$="[[readonly]]"
size$="[[size]]">
<template is="dom-if" if="[[errorMessage]]">
<paper-input-error id="error">[[errorMessage]]</paper-input-error>
</template>
</paper-input-container>
</template>
<script>
Polymer({
is: 'br-cel-input',
behaviors: [
Polymer.PaperInputBehavior,
Polymer.IronFormElementBehavior
],
properties: {
label: {
type: String,
value: "Celular"
}
},
observers: [
'_computeValue(value)'
],
_computeValue: function(value) {
value = String(value);
var start = this.$.input.selectionStart;
var previousCharASpaceOrDashOrParentheses = value.charAt(start - 1) === ' ' || value.charAt(start - 1) === '-' || value.charAt(start - 1) === '(' || value.charAt(start - 1) === ')';
// Remove any already-applied formatting.
value = value.replace(/[-()\s/]/g, '');
// Add a parentheses after the 0th character
if (value.length > 0) {
value = value.substr(0,0) + '(' + value.substr(0)
}
// Add a parentheses after the 3th character
if (value.length > 3) {
value = value.substr(0,3) + ')' + value.substr(3)
}
// Add a space after the 4th character
if (value.length > 4) {
value = value.substr(0,4) + ' ' + value.substr(4)
}
// Add a dash after the 10th character
if (value.length > 10) {
value = value.substr(0,10) + '-' + value.substr(10)
}
// Remove trailing numbers
if (value.length > 15) {
value = value.substr(0,15);
}
this.updateValueAndPreserveCaret(value.trim());
// If the character right before the selection is a newly inserted
// dot or dash, we need to advance the selection to maintain the caret position.
if (!previousCharASpaceOrDashOrParentheses && (value.charAt(start - 1) === ' ' || value.charAt(start - 1) === '-' || value.charAt(start - 1) === '(' || value.charAt(start - 1) === ')')) {
this.$.input.selectionStart = value.length;
this.$.input.selectionEnd = value.length;
}
}
});
</script>
</dom-module>