-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathform-errory-summary.js
117 lines (101 loc) · 3.02 KB
/
form-errory-summary.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
import '../alert/alert.js';
import '../expand-collapse/expand-collapse-content.js';
import { css, html, LitElement, nothing } from 'lit';
import { linkStyles } from '../link/link.js';
import { LocalizeCoreElement } from '../../helpers/localize-core-element.js';
import { RtlMixin } from '../../mixins/rtl/rtl-mixin.js';
class FormErrorSummary extends LocalizeCoreElement(RtlMixin(LitElement)) {
static get properties() {
return {
errors: { type: Object, attribute: false },
_expanded: { type: Boolean, attribute: false }
};
}
static get styles() {
return [linkStyles, css`
:host {
display: block;
}
:host([hidden]) {
display: none;
}
.d2l-form-error-summary-header {
cursor: pointer;
display: flex;
justify-content: space-between;
padding: 0.3rem 0.3rem 0.3rem 1.2rem;
}
:host([dir="rtl"]) .d2l-form-error-summary-header {
padding-left: 0.3rem;
padding-right: 1.2rem;
}
.d2l-form-error-summary-text {
align-items: center;
display: flex;
}
.d2l-form-error-summary-error-list {
margin: 0 0 0 1.2rem;
padding-bottom: 0.6rem;
padding-top: 0.3rem;
}
:host([dir="rtl"]) .d2l-form-error-summary-error-list {
margin-left: 0;
margin-right: 1.2rem;
}
d2l-alert {
max-width: none;
}
`];
}
constructor() {
super();
this.errors = [];
this._expanded = true;
}
render() {
const errorSummary = html`
<d2l-alert type="critical" no-padding aria-live="polite">
<div class="d2l-form-error-summary-header" @click=${this._toggleExpandCollapse}>
<div class="d2l-form-error-summary-text">${this.localize('components.form-error-summary.errorSummary', { count: this.errors.length })}</div>
<d2l-button-icon
aria-expanded=${this._expanded ? 'true' : 'false'}
icon=${this._expanded ? 'tier1:arrow-collapse-small' : 'tier1:arrow-expand-small' }
text="${this.localize('components.form-error-summary.text')}"
@click=${this._toggleExpandCollapse}>
</d2l-button-icon>
</div>
<d2l-expand-collapse-content ?expanded=${this._expanded}>
<ul class="d2l-form-error-summary-error-list">
${this.errors.map(error => html`
<li>
<a class="d2l-link" href=${error.href} @click=${error.onClick}>${error.message}</a>
</li>
`)}
</ul>
</d2l-expand-collapse-content>
</d2l-alert>
`;
return this.errors.length > 0 ? errorSummary : nothing;
}
async focus() {
if (this.errors.length === 0) {
super.focus();
return;
}
let focusEleSelector;
if (this._expanded) {
await this.updateComplete;
await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
focusEleSelector = 'ul:first-child a';
} else {
focusEleSelector = 'd2l-button-icon';
}
const focusEle = this.shadowRoot && this.shadowRoot.querySelector(focusEleSelector);
if (focusEle) focusEle.focus();
}
_toggleExpandCollapse(e) {
e.stopPropagation();
this._expanded = !this._expanded;
}
}
customElements.define('d2l-form-error-summary', FormErrorSummary);