This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
paper-char-counter.html
120 lines (93 loc) · 3.2 KB
/
paper-char-counter.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
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
The complete set of authors may be found at http://polymer.github.io/AUTHORS
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
-->
<!--
Material Design: <a href="http://www.google.com/design/spec/components/text-fields.html#text-fields-character-counter">Character counter</a>
`paper-char-counter` adds a character counter for paper input fields with a character restriction in place.
Example:
<paper-input-decorator>
<input id="input1" is="core-input" maxlength="5">
<paper-char-counter class="footer" target="input1"></paper-char-counter>
</paper-input-decorator>
Theming
-------
`paper-char-counter` uses `paper-input-decorator`'s error `core-style` for global theming.
@group Paper Elements
@element paper-char-counter
@homepage github.io
-->
<link href="../polymer/polymer.html" rel="import">
<link href="../core-style/core-style.html" rel="import">
<core-style id="paper-char-counter">
:host(.invalid) {
color: {{g.paperInput.invalidColor}};
}
</core-style>
<polymer-element name="paper-char-counter">
<template>
<style>
:host {
display: inline-block;
float: right;
color: #757575;
font-size: 0.75em;
padding: 0.5em 0 0.5em 0.5em;
}
</style>
<core-style ref="paper-char-counter"></core-style>
<div class="counter-text" aria-hidden="true" hidden?="{{!showCounter || !_maxChars}}">
<span>{{_charCount}} / {{_maxChars}}</span>
</div>
</template>
<script>
Polymer({
publish: {
/**
* The id of the textinput or textarea that should be monitored.
*
* @attribute target
* @type string
* @default null
*/
target: null,
/**
* If false, don't show the character counter. Used in conjunction with
* `paper-input-decorator's` `error` field.
*
* @attribute showCounter
* @type boolean
* @default true
*/
showCounter: true
},
/* Number of characters in the current input */
_charCount: 0,
/* Equal to the target element's maxLength attribute. */
_maxChars: 0,
/* True if the number of characters in the input exceeds _maxChars */
_isCounterInvalid: false,
ready: function() {
if (!this.target)
return;
var targetElement = document.getElementById(this.target);
this._maxChars = targetElement.maxLength;
targetElement.addEventListener('input', this.inputAction.bind(this));
},
inputAction: function(e) {
this._charCount = e.target.value.length;
this._isCounterInvalid = this._maxChars && this._charCount >= this._maxChars;
},
_isCounterInvalidChanged: function() {
this.classList.toggle('invalid', this._isCounterInvalid);
this.fire('char-counter-error',
{"hasError": this._isCounterInvalid,
"hideErrorIcon": this.showCounter});
}
});
</script>
</polymer-element>