-
Notifications
You must be signed in to change notification settings - Fork 332
/
paper-input.js
189 lines (164 loc) · 5.87 KB
/
paper-input.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @module ember-paper
*/
import { or, bool, and } from '@ember/object/computed';
import Component from '@ember/component';
import { computed } from '@ember/object';
import { isEmpty } from '@ember/utils';
import { run } from '@ember/runloop';
import { assert } from '@ember/debug';
import layout from '../templates/components/paper-input';
import FocusableMixin from 'ember-paper/mixins/focusable-mixin';
import ColorMixin from 'ember-paper/mixins/color-mixin';
import ChildMixin from 'ember-paper/mixins/child-mixin';
import ValidationMixin from 'ember-paper/mixins/validation-mixin';
import { invokeAction } from 'ember-invoke-action';
/**
* @class PaperInput
* @extends Ember.Component
* @uses FocusableMixin
* @uses ChildMixin
* @uses ColorMixin
* @uses ValidationMixin
*/
export default Component.extend(FocusableMixin, ColorMixin, ChildMixin, ValidationMixin, {
layout,
tagName: 'md-input-container',
classNames: ['md-default-theme'],
classNameBindings: [
'hasValue:md-input-has-value',
'isInvalidAndTouched:md-input-invalid',
'hasLeftIcon:md-icon-left',
'hasRightIcon:md-icon-right',
'focused:md-input-focused',
'block:md-block'
],
type: 'text',
autofocus: false,
tabindex: null,
hideAllMessages: false,
isTouched: false,
iconComponent: 'paper-icon',
// override validation mixin `isInvalid` to account for the native input validity
isInvalid: or('hasErrorMessages', 'isNativeInvalid'),
hasValue: computed('value', 'isNativeInvalid', function() {
let value = this.get('value');
let isNativeInvalid = this.get('isNativeInvalid');
return !isEmpty(value) || isNativeInvalid;
}),
shouldAddPlaceholder: computed('label', 'focused', function() {
// if has label, only add placeholder when focused
return isEmpty(this.get('label')) || this.get('focused');
}),
inputElementId: computed('elementId', function() {
return `input-${this.get('elementId')}`;
}),
renderCharCount: computed('value', function() {
let currentLength = this.get('value') ? this.get('value').length : 0;
return `${currentLength}/${this.get('maxlength')}`;
}),
hasLeftIcon: bool('icon'),
hasRightIcon: bool('iconRight'),
isInvalidAndTouched: and('isInvalid', 'isTouched'),
validationProperty: 'value', // property that validations should be run on
// Lifecycle hooks
didReceiveAttrs() {
this._super(...arguments);
assert('{{paper-input}} requires an `onChange` action or null for no action.', this.get('onChange') !== undefined);
let { value, errors } = this.getProperties('value', 'errors');
let { _prevValue, _prevErrors } = this.getProperties('_prevValue', '_prevErrors');
if (value !== _prevValue || errors !== _prevErrors) {
this.notifyValidityChange();
}
this._prevValue = value;
this._prevErrors = errors;
},
didInsertElement() {
this._super(...arguments);
if (this.get('textarea')) {
this._growTextareaOnResize = run.bind(this, this.growTextarea);
window.addEventListener('resize', this._growTextareaOnResize);
}
},
didRender() {
this._super(...arguments);
// setValue below ensures that the input value is the same as this.value
this.setValue(this.get('value'));
this.growTextarea();
},
willDestroyElement() {
this._super(...arguments);
if (this.get('textarea')) {
window.removeEventListener('resize', this._growTextareaOnResize);
this._growTextareaOnResize = null;
}
},
growTextarea() {
if (this.get('textarea')) {
let inputElement = this.element.querySelector('input, textarea');
inputElement.classList.add('md-no-flex');
inputElement.setAttribute('rows', 1);
let minRows = this.get('passThru.rows');
let height = this.getHeight(inputElement);
if (minRows) {
if (!this.lineHeight) {
inputElement.style.minHeight = 0;
this.lineHeight = inputElement.clientHeight;
inputElement.style.minHeight = null;
}
if (this.lineHeight) {
height = Math.max(height, this.lineHeight * minRows);
}
let proposedHeight = Math.round(height / this.lineHeight);
let maxRows = this.get('passThru.maxRows') || Number.MAX_VALUE;
let rowsToSet = Math.min(proposedHeight, maxRows);
inputElement.style.height = `${this.lineHeight * rowsToSet}px`;
inputElement.setAttribute('rows', rowsToSet);
if (proposedHeight >= maxRows) {
inputElement.classList.add('md-textarea-scrollable');
} else {
inputElement.classList.remove('md-textarea-scrollable');
}
} else {
inputElement.style.height = 'auto';
inputElement.scrollTop = 0;
let height = this.getHeight(inputElement);
if (height) {
inputElement.style.height = `${height}px`;
}
}
inputElement.classList.remove('md-no-flex');
}
},
getHeight(inputElement) {
let { offsetHeight } = inputElement;
let line = inputElement.scrollHeight - offsetHeight;
return offsetHeight + (line > 0 ? line : 0);
},
setValue(value) {
if (this.element.querySelector('input, textarea').value !== value && value) {
this.element.querySelector('input, textarea').value = value;
}
},
actions: {
handleInput(e) {
invokeAction(this, 'onChange', e.target.value);
// setValue below ensures that the input value is the same as this.value
run.next(() => {
if (this.isDestroyed) {
return;
}
this.setValue(this.get('value'));
});
this.growTextarea();
let inputElement = this.element.querySelector('input');
this.set('isNativeInvalid', inputElement && inputElement.validity && inputElement.validity.badInput);
this.notifyValidityChange();
},
handleBlur(e) {
invokeAction(this, 'onBlur', e);
this.set('isTouched', true);
this.notifyValidityChange();
}
}
});