-
Notifications
You must be signed in to change notification settings - Fork 2
/
br-cpf-input.html
122 lines (99 loc) · 3.47 KB
/
br-cpf-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
<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-cpf-validator.html">
<!--
`br-cpf-input`
@demo demo/index.html
-->
<dom-module id="br-cpf-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-cpf-validator></br-cpf-validator>
<input is="iron-input" id="input"
aria-labelledby$="[[_ariaLabelledBy]]"
aria-describedby$="[[_ariaDescribedBy]]"
required$="[[required]]"
validator="br-cpf-validator"
type="text"
allowed-pattern="[0-9\-\.]"
prevent-invalid-input
maxlength="14"
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-cpf-input',
behaviors: [
Polymer.PaperInputBehavior,
Polymer.IronFormElementBehavior
],
properties: {
label: {
type: String,
value: "CPF"
}
},
observers: [
'_computeValue(value)'
],
_computeValue: function(value) {
value = String(value);
var start = this.$.input.selectionStart;
var previousCharADotOrDash = value.charAt(start - 1) === '.' || value.charAt(start - 1) === '-';
// Remove any already-applied formatting.
value = value.replace(/[-.]/g, '');
// Add a dot after the 3rd character
if (value.length > 3) {
value = value.substr(0,3) + '.' + value.substr(3)
}
// Add a dot after the 7th character
if (value.length > 7) {
value = value.substr(0,7) + '.' + value.substr(7)
}
// Add a dash after the 11th character
if (value.length > 11) {
value = value.substr(0,11) + '-' + value.substr(11)
}
// Remove trailing numbers
if (value.length > 14) {
value = value.substr(0,14);
}
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 (!previousCharADotOrDash && (value.charAt(start - 1) === '-' || value.charAt(start - 1) === '.')) {
this.$.input.selectionStart = start + 1;
this.$.input.selectionEnd = start + 1;
}
}
});
</script>
</dom-module>