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