-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathmd-input-field.js
More file actions
57 lines (51 loc) · 1.55 KB
/
md-input-field.js
File metadata and controls
57 lines (51 loc) · 1.55 KB
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
import Component from '@ember/component';
import { computed } from '@ember/object';
import { isPresent } from '@ember/utils';
import jQuery from 'jquery';
export default Component.extend({
classNames: ['input-field'],
bindAttributes: ['disabled', 'readonly', 'autofocus'],
validate: false,
_wasTouched: false,
isValid: computed('_wasTouched', 'value', 'validate', 'errors', 'errors.[]', function() {
return (
(isPresent(this.get('value')) || this.get('_wasTouched')) &&
this.get('validate') &&
this.get('errors') &&
this.get('errors.length') === 0
);
}),
isInvalid: computed('_wasTouched', 'value', 'validate', 'errors', 'errors.[]', function() {
return (
(isPresent(this.get('value')) || this.get('_wasTouched')) &&
this.get('validate') &&
this.get('errors') &&
this.get('errors.length') > 0
);
}),
didInsertElement() {
this._super(...arguments);
// pad the errors element when an icon is present
if (isPresent(this.get('icon'))) {
jQuery('> span').css('padding-left', '3rem');
}
},
id: computed('elementId', function() {
return `${this.get('elementId')}-input`;
}),
_setupLabel() {
const $label = jQuery('> label');
if (isPresent(this.get('value')) && !$label.hasClass('active')) {
$label.addClass('active');
}
},
_errorString: computed('errors.[]', function() {
return (this.get('errors') || []).join('. ');
}),
actions: {
inputFocusIn(evt) {
this.set('_wasTouched', true);
this.sendAction('focusIn', evt);
}
}
});