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 16
/
core-input.html
148 lines (117 loc) · 3.46 KB
/
core-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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!--
Copyright (c) 2014 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
-->
<!--
`core-input` is an unstyled single-line input field. It extends the native
`input` element.
Example:
<input is="core-input">
The input's value is considered "committed" if the user hits the "enter" key
or blurs the input after changing the value. The committed value is stored in
the `committedValue` property.
If the input has `type = number`, this element will also prevent non-numeric characters
from being typed into the text field.
Accessibility
-------------
The following ARIA attributes are set automatically:
- `aria-label`: set to the `placeholder` attribute
- `aria-disabled`: set if `disabled` is true
@group Polymer Core Elements
@element core-input
@extends input
@homepage github.io
-->
<link href="../polymer/polymer.html" rel="import">
<style shim-shadowdom>
/* FIXME consider theming */
html /deep/ input[is=core-input] {
width: 20em;
font: inherit;
margin: 0;
padding: 0;
background-color: transparent;
border: 0;
outline: none;
}
</style>
<polymer-element name="core-input" extends="input">
<script>
Polymer('core-input', {
publish: {
/**
* The "committed" value of the input, ie. the input value when the user
* hits the "enter" key or blurs the input after changing the value. You
* can bind to this value instead of listening for the "change" event.
* Setting this property has no effect on the input value.
*
* @attribute committedValue
* @type string
* @default ''
*/
committedValue: '',
/**
* Set to true to prevent invalid input from being entered.
*
* @attribute preventInvalidInput
* @type boolean
* @default false
*/
preventInvalidInput: false
},
previousValidInput: '',
eventDelegates: {
input: 'inputAction',
change: 'changeAction'
},
ready: function() {
/* set ARIA attributes */
this.disabledHandler();
this.placeholderHandler();
},
attributeChanged: function(attr, old) {
if (this[attr + 'Handler']) {
this[attr + 'Handler'](old);
}
},
disabledHandler: function() {
if (this.disabled) {
this.setAttribute('aria-disabled', '');
} else {
this.removeAttribute('aria-disabled');
}
},
placeholderHandler: function() {
if (this.placeholder) {
this.setAttribute('aria-label', this.placeholder);
} else {
this.removeAttribute('aria-label');
}
},
/**
* Commits the `value` to `committedValue`
*
* @method commit
*/
commit: function() {
this.committedValue = this.value;
},
changeAction: function() {
this.commit();
},
inputAction: function(e) {
if (this.preventInvalidInput) {
if (!e.target.validity.valid) {
e.target.value = this.previousValidInput;
} else {
this.previousValidInput = e.target.value;
}
}
}
});
</script>
</polymer-element>