-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtz-wizard-step-errors-behavior.html
93 lines (87 loc) · 3.28 KB
/
mtz-wizard-step-errors-behavior.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="mtz-wizard-step-behavior.html">
<script>
window.mtz = window.mtz || {};
/**
* `mtz.WizardStepErrorsBehavior`
* Extends the mtz.WizardStepBehavior to allow setting an errors array that will perform a lookup for the form
* elements associated with each error then set their error message and state.
*
* @polymerBehavior mtz.WizardStepErrorsBehavior
* @demo demo/mtz-wizard-step.html
*/
mtz.WizardStepErrorsBehavior = [mtz.WizardStepBehavior, {
properties: {
/* The attribute to assign the error-message to */
errorMessageAttribute: {
type: String,
value: 'error-message'
},
/**
* The list of all errors to associate with the form
* @type {Object.<{step: String, field: String, code: String, message: String}>}
*/
errors: Array,
/* The attribute to key off of when doing the lookup for fields within the form */
keyAttribute: {
type: String,
value: 'name'
}
},
observers: [
'__setErrorState(errors)'
],
/**
* Sets up bound functions for event bindings.
*/
ready() {
this.__revertErrorMessage = this.__revertErrorMessage.bind(this);
},
/**
* Reverts the error message back to it's original state in order to preserve UI error messages.
* @private
*
* @param {CustomEvent} event
*/
__revertErrorMessage(event) {
const field = Polymer.dom(event).rootTarget;
const originalMessage = field.originalErrorMessage;
// Remove the event listener
field.removeEventListener('invalid-changed', this.__revertErrorMessage);
field.errorMessage = originalMessage;
// Required to get the UI to update properly
if (originalMessage) field.setAttribute('error-message', originalMessage);
else field.removeAttribute('error-message');
delete field.originalErrorMessage;
},
/**
* Looks up each element based on the keyAttr for the associated error then sets the error message based on
* messageAttr and toggles on the invalid flag.
* @private
*
* @param {Object[]} errors
* @param {String} [messageAttr = this.errorMessageAttribute] - the attribute to set the message on
* @param {String} [keyAttr = this.keyAttribute] - the attribute to lookup on
*/
__setErrorState(errors, messageAttr = this.errorMessageAttribute, keyAttr = this.keyAttribute) {
if (!errors) return;
errors.forEach((error) => {
// Errors should be filtered just to this step, however we want to sanity check them
if (error.step === this.name) {
const field = this.form.querySelector(`[${keyAttr}="${error.field}"]`);
if (!field) return;
// Store the original state for later
field.originalErrorMessage = field[messageAttr];
// Update our invalid state and associated messaging
field[messageAttr] = error.message;
field.setAttribute(messageAttr, error.message);
field.invalid = true;
field.setAttribute('invalid', '');
field.addEventListener('invalid-changed', this.__revertErrorMessage);
}
});
this.set('errors', null);
this.invalid = true;
},
}];
</script>