-
Notifications
You must be signed in to change notification settings - Fork 332
/
paper-form.js
60 lines (52 loc) · 1.73 KB
/
paper-form.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
/**
* @module ember-paper
*/
import { not, and } from '@ember/object/computed';
import Component from '@ember/component';
import { computed } from '@ember/object';
import layout from '../templates/components/paper-form';
import ParentMixin from 'ember-paper/mixins/parent-mixin';
import { invokeAction } from 'ember-invoke-action';
/**
* @class PaperForm
* @extends Ember.Component
* @uses ParentMixin
*/
export default Component.extend(ParentMixin, {
layout,
tagName: 'form',
inputComponent: 'paper-input',
submitButtonComponent: 'paper-button',
selectComponent: 'paper-select',
autocompleteComponent: 'paper-autocomplete',
isValid: not('isInvalid'),
isInvalid: computed('childComponents.@each.isInvalid', function() {
return this.get('childComponents').isAny('isInvalid');
}),
isTouched: computed('childComponents.@each.isTouched', function() {
return this.get('childComponents').isAny('isTouched');
}),
isInvalidAndTouched: and('isInvalid', 'isTouched'),
submit() {
this.send('onSubmit');
return false;
},
actions: {
onValidityChange() {
if (this.get('lastIsValid') !== this.get('isValid') || this.get('lastIsTouched') !== this.get('isTouched')) {
invokeAction(this, 'onValidityChange', this.get('isValid'), this.get('isTouched'), this.get('isInvalidAndTouched'));
this.set('lastIsValid', this.get('isValid'));
this.set('lastIsTouched', this.get('isTouched'));
}
},
onSubmit() {
if (this.get('isInvalid')) {
this.get('childComponents').setEach('isTouched', true);
invokeAction(this, 'onInvalid');
} else {
invokeAction(this, 'onSubmit');
this.get('childComponents').setEach('isTouched', false);
}
}
}
});