1 | /*
|
2 | Copyright 2013-2015 ASIAL CORPORATION
|
3 | Licensed under the Apache License, Version 2.0 (the "License");
|
4 | you may not use this file except in compliance with the License.
|
5 | You may obtain a copy of the License at
|
6 | http://www.apache.org/licenses/LICENSE-2.0
|
7 | Unless required by applicable law or agreed to in writing, software
|
8 | distributed under the License is distributed on an "AS IS" BASIS,
|
9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
10 | See the License for the specific language governing permissions and
|
11 | limitations under the License.
|
12 | */
|
13 |
|
14 | import onsElements from '../ons/elements.js';
|
15 | import BaseInputElement from './base/base-input.js';
|
16 | import contentReady from '../ons/content-ready.js';
|
17 | import util from '../ons/util.js';
|
18 |
|
19 | const scheme = {
|
20 | '.text-input': 'text-input--*',
|
21 | '.text-input__label': 'text-input--*__label'
|
22 | };
|
23 |
|
24 | /**
|
25 | * @element ons-input
|
26 | * @category form
|
27 | * @modifier material
|
28 | * [en]Displays a Material Design input.[/en]
|
29 | * [ja][/ja]
|
30 | * @modifier underbar
|
31 | * [en]Displays a horizontal line underneath a text input.[/en]
|
32 | * [ja][/ja]
|
33 | * @modifier transparent
|
34 | * [en]Displays a transparent input. Works for Material Design.[/en]
|
35 | * [ja][/ja]
|
36 | * @description
|
37 | * [en]
|
38 | * An input element. The `type` attribute can be used to change the input type. All text input types are supported.
|
39 | *
|
40 | * The component will automatically render as a Material Design input on Android devices.
|
41 | *
|
42 | * Most attributes that can be used for a normal `<input>` element can also be used on the `<ons-input>` element.
|
43 | * [/en]
|
44 | * [ja][/ja]
|
45 | * @tutorial vanilla/Reference/input
|
46 | * @seealso ons-checkbox
|
47 | * [en]The `<ons-checkbox>` element is used to display a checkbox.[/en]
|
48 | * [ja][/ja]
|
49 | * @seealso ons-radio
|
50 | * [en]The `<ons-radio>` element is used to display a radio button.[/en]
|
51 | * [ja][/ja]
|
52 | * @seealso ons-range
|
53 | * [en]The `<ons-range>` element is used to display a range slider.[/en]
|
54 | * [ja][/ja]
|
55 | * @seealso ons-switch
|
56 | * [en]The `<ons-switch>` element is used to display a draggable toggle switch.[/en]
|
57 | * [ja][/ja]
|
58 | * @seealso ons-select
|
59 | * [en]The `<ons-select>` element is used to display a select box.[/en]
|
60 | * [ja][/ja]
|
61 | * @guide theming.html#modifiers [en]More details about the `modifier` attribute[/en][ja]modifier属性の使い方[/ja]
|
62 | * @example
|
63 | * <ons-input placeholder="Username" float></ons-input>
|
64 | */
|
65 | export default class InputElement extends BaseInputElement {
|
66 |
|
67 | constructor() {
|
68 | super();
|
69 |
|
70 | this._boundOnInput = this._update.bind(this);
|
71 | this._boundOnFocusin = this._update.bind(this);
|
72 | }
|
73 |
|
74 | /* Inherited props */
|
75 |
|
76 | _update() {
|
77 | this._updateLabel();
|
78 | this._updateLabelClass();
|
79 | }
|
80 |
|
81 | get _scheme() {
|
82 | return scheme;
|
83 | }
|
84 |
|
85 | get _template() {
|
86 | return `
|
87 | <input type="${this.type}" class="text-input">
|
88 | <span class="text-input__label"></span>
|
89 | `;
|
90 | }
|
91 |
|
92 | get type() {
|
93 | const type = this.getAttribute('type');
|
94 | return (['checkbox', 'radio'].indexOf(type) < 0) && type || 'text';
|
95 | }
|
96 |
|
97 | set type(value) {
|
98 | this.setAttribute('type', value);
|
99 | }
|
100 |
|
101 | /* Own props */
|
102 |
|
103 | _updateLabel() {
|
104 | const label = this.getAttribute('placeholder') || '';
|
105 |
|
106 | if (typeof this._helper.textContent !== 'undefined') {
|
107 | this._helper.textContent = label;
|
108 | } else {
|
109 | this._helper.innerText = label;
|
110 | }
|
111 | }
|
112 |
|
113 | _updateLabelClass() {
|
114 | if (this.value === '') {
|
115 | this._helper.classList.remove('text-input--material__label--active');
|
116 | } else {
|
117 | this._helper.classList.add('text-input--material__label--active');
|
118 | }
|
119 | }
|
120 |
|
121 | get _helper() {
|
122 | return this.querySelector('span');
|
123 | }
|
124 |
|
125 | connectedCallback() {
|
126 | super.connectedCallback();
|
127 |
|
128 | contentReady(this, () => {
|
129 | this._input.addEventListener('input', this._boundOnInput);
|
130 | this._input.addEventListener('focusin', this._boundOnFocusin);
|
131 | });
|
132 |
|
133 | const type = this.getAttribute('type');
|
134 | if (['checkbox', 'radio'].indexOf(type) >= 0) {
|
135 | util.warn(`Warn: <ons-input type="${type}"> is deprecated since v2.4.0. Use <ons-${type}> instead.`);
|
136 | }
|
137 | }
|
138 |
|
139 | disconnectedCallback() {
|
140 | super.disconnectedCallback();
|
141 |
|
142 | contentReady(this, () => {
|
143 | this._input.removeEventListener('input', this._boundOnInput);
|
144 | this._input.removeEventListener('focusin', this._boundOnFocusin);
|
145 | });
|
146 | }
|
147 |
|
148 | static get observedAttributes() {
|
149 | return [...super.observedAttributes, 'type'];
|
150 | }
|
151 |
|
152 | attributeChangedCallback(name, last, current) {
|
153 | switch (name) {
|
154 | case 'type':
|
155 | contentReady(this, () => this._input.setAttribute('type', this.type));
|
156 | break;
|
157 | default:
|
158 | super.attributeChangedCallback(name, last, current);
|
159 | }
|
160 | }
|
161 |
|
162 | /**
|
163 | * @attribute placeholder
|
164 | * @type {String}
|
165 | * @description
|
166 | * [en]Placeholder text. In Material Design, this placeholder will be a floating label.[/en]
|
167 | * [ja][/ja]
|
168 | */
|
169 |
|
170 | /**
|
171 | * @attribute float
|
172 | * @description
|
173 | * [en]If this attribute is present, the placeholder will be animated in Material Design.[/en]
|
174 | * [ja]この属性が設定された時、ラベルはアニメーションするようになります。[/ja]
|
175 | */
|
176 |
|
177 | /**
|
178 | * @property float
|
179 | * @type {Boolean}
|
180 | * @description
|
181 | * [en]If this property is present, the placeholder will be animated in Material Design.[/en]
|
182 | * [ja]この属性が設定された時、ラベルはアニメーションするようになります。[/ja]
|
183 | */
|
184 |
|
185 | /**
|
186 | * @attribute type
|
187 | * @type {String}
|
188 | * @description
|
189 | * [en]
|
190 | * Specify the input type. This is the same as the "type" attribute for normal inputs. It expects strict text types such as `text`, `password`, etc. For checkbox, radio button, select or range, please have a look at the corresponding elements.
|
191 | *
|
192 | * Please take a look at [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-type) for an exhaustive list of possible values. Depending on the platform and browser version some of these might not work.
|
193 | * [/en]
|
194 | * [ja][/ja]
|
195 | */
|
196 |
|
197 | /**
|
198 | * @attribute input-id
|
199 | * @type {String}
|
200 | * @description
|
201 | * [en]Specify the "id" attribute of the inner `<input>` element. This is useful when using `<label for="...">` elements.[/en]
|
202 | * [ja][/ja]
|
203 | */
|
204 |
|
205 | /**
|
206 | * @property value
|
207 | * @type {String}
|
208 | * @description
|
209 | * [en]The current value of the input.[/en]
|
210 | * [ja][/ja]
|
211 | */
|
212 |
|
213 | /**
|
214 | * @property disabled
|
215 | * @type {Boolean}
|
216 | * @description
|
217 | * [en]Whether the input is disabled or not.[/en]
|
218 | * [ja]無効化されている場合に`true`。[/ja]
|
219 | */
|
220 |
|
221 | /**
|
222 | * @method focus
|
223 | * @signature focus()
|
224 | * @description
|
225 | * [en]Focuses the input.[/en]
|
226 | * [ja][/ja]
|
227 | */
|
228 |
|
229 | /**
|
230 | * @method blur
|
231 | * @signature blur()
|
232 | * @description
|
233 | * [en]Removes focus from the input.[/en]
|
234 | * [ja][/ja]
|
235 | */
|
236 | }
|
237 |
|
238 | util.defineBooleanProperties(InputElement, ['float']);
|
239 |
|
240 | onsElements.Input = InputElement;
|
241 | customElements.define('ons-input', InputElement);
|